Search Results
Search type | Search syntax |
---|---|
Tags | [tag] |
Exact | "words here" |
Author |
user:1234 user:me (yours) |
Score |
score:3 (3+) score:0 (none) |
Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
Views | views:250 |
Code | code:"if (foo != bar)" |
Sections |
title:apples body:"apples oranges" |
URL | url:"*.example.com" |
Saves | in:saves |
Status |
closed:yes duplicate:no migrated:no wiki:no |
Types |
is:question is:answer |
Exclude |
-[tag] -apples |
For more details on advanced search visit our help page |
Questions about Mathematica's functional programming style, including the use of pure functions (Function[], #, &) and functions such as Map, Apply, Nest, and Through.
3
votes
Accepted
How to create a list of variable names to some function of that variable
Print[{ToString@#, f @@ #}] & /@ HoldForm /@ Unevaluated@{a, b, c}
(* or *)
Print[{ToString@#, f @@ #}] & /@ Defer/@ Unevaluated@{a, b, c}
to get
{"a", f[1]}
{"b", f[2]}
{"c", f[3]}
printed. Remo …
2
votes
From iterative to functional
This also works:
fold = Function[{lst},Fold[(ReplacePart[#1, #2 ->relax[#1, #1[[#2]]]]) &,
lst, Range@Length@lst]];
fxpnt = FixedPoint[fold, #, SameTest -> (Not[test[#1, #2]] &)] &;
fxpnt@j
1
vote
find position of nearest nonzero element in an array (functionally)
nrstNZP[l_] := With[{nF = Nearest[Flatten@SparseArray[l]["NonzeroPositions"]]},
With[{nrst = nF[#, 2]}, DeleteCases[nrst, #][[1]]] & /@ #] &
Example:
SeedRandom[1]
sel = RandomInteger[{0, 2} …
5
votes
Replace Table by functional programming
Array[{#, #^2} & @@ {#2 + 2 #} &, {2, 2}, {0, 3}] (* or *)
Outer[{#, #^2} & @@ {#2 + 2 #} &, {0, 1}, Range[3, 4]]
{{{3, 9}, {4, 16}}, {{5, 25}, {6, 36}}}
Removing hard-coded parameters:
f1 = M …
4
votes
Accepted
Apply after with arguments
λ^2 - 2 λ Cosh[z] + Cosh[z]^2 - Sinh[z]^2 == 0 // Solve[#, λ] &
λ^2 - 2 λ Cosh[z] + Cosh[z]^2 - Sinh[z]^2 == 0 // Function[Solve[#, λ]]
λ^2 - 2 λ Cosh[z] + Cosh[z]^2 - Sinh[z]^2 == 0 // Function[{x …
5
votes
Pseudo-currying in one line
Another old function Compose (superseeded by Composition but does some stuff that its supersessor doesn't):
pseudoCurry = Fold[Compose, #[[0]], #]&;
pseudoCurry @ c[a1, a2, a3, a4, a5]
c[a1][a2 …
8
votes
Elegant functional equivalent to a nested loop?
mapAboveDiagonal1 = With[{dim = Dimensions[#2]},
MapAt[#, #2, Join @@ Table[{i, j}, {i, dim[[1]]}, {j, i, dim[[2]]}]]] &
or
mapAboveDiagonal2 = MapAt[#, #2,
SparseArray[UpperTriangularize[
…
2
votes
Accepted
Question on using Array
If you have to use Array:
Array[Through @ {x, y, z} @ vl[[#]] &, Length @ vl]
{{x[6], y[6], z[6]}, {x[9], y[9], z[9]}, {x[10], y[10], z[10]}}
Also:
f = Through /@ # /@ #2 &;
f[{x, y, z}, vl]
…
11
votes
Accepted
Can this code be written in a more functional style
Two more ways:
parti1[a_, p_] := SortBy[a, {Sign[# - p] &, # == # &}]
or
parti2[a_, p_] := Join[Select[a, # < p &], {p}, Select[a, # >= p &]]
With
a = {3, 5, 6, 7, 2, 1, 2}; (* and *) p …
2
votes
All values for a function with two arguments without Outer
ListConvolve
l1 = {a1, a2, a3}; l2 = {b1, b2, b3};
Union @@ ListConvolve[l1, l2, 1, l2, f, List]
(* {f[a1, b1], f[a1, b2], f[a1, b3],
f[a2, b1], f[a2, b2], f[a2, b3],
f[a3, b1], f[a3, …
2
votes
Working with calculations that depend on the previous value in a list
Perhaps
stream = {1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1};
LengthWhile[stream, # <= 5 &]
(* 5 *)
TakeWhile[stream, # <= 5 &]
(* {1,2,3,4,5} *)
or
First[Split[stream , #1 < 5 &]]
(* {1, …
4
votes
Transforming a list of the form {{{x, y}, z}, ... } into the form {{x, y, z}, ... }
♭ = ## & @@@ {##} & @@@ # &;
♭ @ {{{x1, y1}, z1}, {{x2, y2}, z2}}
{{x1, y1, z1}, {x2, y2, z2}}
7
votes
Accepted
Applying a Function within a Function
ClearAll[SquareSum]
SquareSum[n_Integer] /; n > 0 := Fold[myfunc, Range[n]]
SquareSum[4]
21904
to include myfunc in the function definition rather than as an external definition:
ClearAll[S …
3
votes
Accepted
Changing one argument of a multivariable function at each step of Fold
You can use Fold as follows:
Fold[F[#2, #, c, d] &, b, Array[a, 4]]
F[a[4], F[a[3], F[a[2], F[a[1], b, c, d], c, d], c, d], c, d]
5
votes
Through: how to use it with subtraction of functions?
You can compose Minus with g use it with Plus:
Through[(f + Minus @* g)[x]]
x - x^2
Other ways:
Through[(f + (-g @ # &))[x]]
x - x^2
Through[(f + (-#&) @* g)[x]]
x - x^2