Questions tagged [functional-style]

Questions about Mathematica's functional programming style, including the use of pure functions (Function[], #, &) and functions such as Map, Apply, Nest, and Through.

Mathematica's functional programming features allow users to treat functions as expressions in the same way that data are expressions. This functionality is usually more efficient than the use of Do and For loops that are more familiar to users of procedural programming languages like C or Fortran. By using dedicated built-in Mathematica functions and pure functions, one can write code where the output of a function is set to be the direct input of a next function, without ever introducing temporary variables at all.

An important part of this functionality is the use of pure functions, known in some other languages as "anonymous functions". These are usually distinguishable through their use of Function (&) and the (optional) accompanying use of Slot (#) and/or SlotSequence (##), in place of arguments.

Questions on how to use and construct functions should use the tag .

Useful links:

468 questions
37
votes
4 answers

FoldWhile and FoldWhileList

Mathematica has had NestWhile and NestWhileList for some time. But, to date, it has not implemented a built-in FoldWhile or a FoldWhileList. So, since these constructs seem useful to me, I have tried to brew my own. Here are my current…
Seth Chandler
  • 3,132
  • 14
  • 25
24
votes
5 answers

Pseudo-currying in one line

Often when I'm writing OOP code using an object-manager association I find myself doing something akin to currying the arguments to some form of delegate object or head. (Building a one-argument chained call as opposed to returning functions of one…
b3m2a1
  • 46,870
  • 3
  • 92
  • 239
19
votes
2 answers

Intuition behind FoldPair and SequenceFold?

In trawling through the documentation I've found the functions FoldPairList and SequenceFoldList. Examples are given of how to use FoldPairList: Partition a list into sublists of different lengths using TakeDrop Break an amount of money into bills…
pdmclean
  • 1,368
  • 10
  • 19
18
votes
4 answers

Is there a more elegant and efficient way to write brainf*** style loops in Mathematica?

I'm having some fun writing a brainf*** interpreter in Mathematica. See this wiki article for more info. It works nicely but I'd like to find an elegant, preferably functional, way to handle the looping structures. This is a simple example of what…
Andy Ross
  • 19,320
  • 2
  • 61
  • 93
15
votes
1 answer

What makes mathematica a "functional programming language"?

According to the website of mathematica itself, mathematica is a "functional progrmaming language". I am trying to understand what "functional programming" is. I don't think I have ever worked with "functional programming" before (though I'm not…
user56834
  • 545
  • 3
  • 10
11
votes
2 answers

From iterative to functional

How to write this small piece in a functional way (ie. without state variables)?: test[oldJ_List, newJ_List] := Total[Abs[oldJ - newJ]] > 1; relax[j_List, x_?NumericQ] := Mean[Nearest[j, x, 4]]; j = Range[100]; (* any numeric list *) j1 = j/2;…
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
9
votes
4 answers

How to evaluate a function on a list until stable?

I need to evaluate a function func on a range of integers (NOT recursively, just sequentially) until the result is stable enough. I can write a not very clever step-by-step program using While: Module[{variation,list={},j=1}, …
Ziofil
  • 2,470
  • 16
  • 30
9
votes
3 answers

Map over list of functions

I would like to convert a list of $n$ complex equations to a list of $2n$ real ones. At the moment I am doing it like this: eqs = {a + I b == 0, c + I d == 0} Flatten[{ComplexExpand[Re[First[#]]] == 0 & /@ eqs, ComplexExpand[Im[First[#]]]…
Andrei
  • 901
  • 5
  • 17
8
votes
1 answer

How to solve this iteration in the functional programming way?

I am trying to come up with a functional programming method to calculate a list of replacements, that depend on previous replacements. Since there is always a finite number of steps, I would like to use Fold, Nest, ComposeList or any related…
István Zachar
  • 47,032
  • 20
  • 143
  • 291
8
votes
10 answers

Elegant functional equivalent to a nested loop?

I have a square $n \times n$ matrix $m$, and need to apply a function $f$ to all elements on and above the diagonal. This is of course easy to do using a nested table: Table[Table[f @ m[[i,j]],{i,1,n}],{j,i,n}] Is there a more elegant functional…
verse
  • 1,287
  • 6
  • 18
8
votes
5 answers

Help going from iterative to functional

The following code segment shows what I'd like to do. I'm a procedural programmer trying to learn the Mathematica functional style. Any help on this would be appreciated. B = {{1, 3}, {1, 5}, {4, 2}, {5, 2}, {5, 5}} u = SparseArray[{{1, 2} -> 5,…
user10831
  • 179
  • 3
7
votes
1 answer

Nested apply function at a list

Given a function f and a list {a,b,c,d,e} how can I compactly tell mathematica to return f[a,f[b,f[c,f[d,e]]]]? It seems that Apply can do the job but I cannot make it work as I want.
tst
  • 953
  • 6
  • 13
6
votes
5 answers

When will $1^2+2^2+3^2+5^2+7^2+11^2+\cdots\cdots + p^2$ (where $p$ is prime) be greater than $10^{10}$?

How to solve the problem: When will $1^2+2^2+3^2+5^2+7^2+11^2+13^2+\cdots\cdots + p^2$ (where every base and $p$ are prime, except the base of the first term $1$) be greater than $10^{10}$? (Namely, solve for such smallest $p$) I can come up with…
Eric
  • 1,191
  • 7
  • 20
6
votes
3 answers

Replace Table by functional programming

Just for learning purposes I try to create a list that looks like this: {{{3, 9}, {4, 16}}, {{5, 25}, {6, 36}}} One way to do it is by using Table: Table[{j + 2 i, (j + 2 i)^2}, {i, 0, 1}, {j, 3, 4}] Now I try it using functional programming, but I…
GambitSquared
  • 2,311
  • 15
  • 23
5
votes
4 answers

How to transform Do loop to more efficient codes?

I'm new to Mathematica. Here is my original program: How to rewrite the Do part? Clear["Global`*"]; da = {}; dt = 0.02; endtime = 2; Q = dQ = {0, 0, 0}; G = {0, -9.8, 0}; Do[ ddQ = G - Q; dQ = dQ + ddQ*dt; Q = Q + dQ*dt; da = Append[da,…
novice
  • 2,325
  • 1
  • 24
  • 30
1
2 3