Skip to main content

Questions tagged [pure-function]

Questions about implementing or applying functions which: (1) always yield the same result for the same input, (2) are independent of outside information, and (3) produce no side effects.

Filter by
Sorted by
Tagged with
50 votes
5 answers
3k views

Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs

Using global variables the following turns an "expression" into a Function: expr = 2 x; Function[x, Evaluate[expr]] Of course ...
einbandi's user avatar
  • 4,024
24 votes
2 answers
1k views

Pure function with attributes of arbitrary number of arguments: Is it possible?

Mathematica allows to define pure function, like Function[{a, b},Length[Unevaluated@a]{b}][1+2,2+3] (* ==> {0} *) Pure functions in Mathematica can take an ...
celtschk's user avatar
  • 19.1k
30 votes
5 answers
5k views

Second level depth pure function?

I have the following working construction: Select[ln125, # == Nearest[ln125, 551.748][[1]] &] Here, ln125 is a one ...
DimG's user avatar
  • 608
34 votes
3 answers
3k views

Is there any way to define pure functions with optional arguments?

For example consider the following function: Function[{u,v},u^2+v^4] Is there anyway to define default values for the variables ...
M6299's user avatar
  • 1,471
21 votes
2 answers
933 views

Using several anonymous functions mixed together

This works: list // BarChart[#[[2]], ChartLabels -> DateString @@@ #[[1]], ChartStyle -> "Pastel"] & This doesn't: ...
C. E.'s user avatar
  • 70.7k
15 votes
3 answers
465 views

Using Through to evaluate complex expressions

Suppose we're given the expression f*g+h, where f,g,h are all pure functions. How can we evaluate this expression on some ...
Nilay Kumar's user avatar
49 votes
2 answers
3k views

Memoization with pure functions?

Is this possible? If I have a simple function, say: f=If[#>0,1,2]& then for each value of # this will re-evaluate f right? Is it possible to define a ...
maria's user avatar
  • 595
32 votes
2 answers
2k views

What are the cool kids talking about when they use ##&[]? [duplicate]

All the cool kids are apparently using ##&[] for Unevaluated @ Sequence[] but I have no idea what either means. ...
orome's user avatar
  • 12.8k
24 votes
1 answer
845 views

Why does the documentation call functions "pure"?

Clearly, Functions in mathematica are not pure functions according to the definition on Wikipedia (no side effects - basically implementabe with table lookups) the ...
masterxilo's user avatar
  • 5,739
15 votes
2 answers
343 views

Is anonymous pure function a scoping construct?

In recent thread was raised the question: why anonymous pure functions Function[body] (or body &) do not rename symbols in ...
Alexey Popkov's user avatar
11 votes
2 answers
474 views

Unexpected variable renaming depending on form of a pure function

I encountered unexpected difference in variable renaming behavior depending on whether a pure function with a named parameter, or a pure function with a slot is used: ...
Vladimir Reshetnikov's user avatar
6 votes
2 answers
611 views

How to condition arguments of a pure function?

I would like to exclude the point {x=0,y=0} in the function definition ...
Ulrich Neumann's user avatar
5 votes
4 answers
2k views

Cases vs Select, Pattern vs Pure function [duplicate]

What is the difference between having Cases with selection condition presented by pattern and a with pure function as condition? ...
Al Guy's user avatar
  • 1,610
31 votes
9 answers
3k views

Using patterns in pure functions

Pure functions may be handy if you don't want to assign your function a name. For example I would calculate $x(x-1)$ for some numbers $x$ by ...
sjdh's user avatar
  • 7,777
29 votes
1 answer
2k views

Is it possible to reference "self" from a pure function?

This would calculate the factorial of 6, if the "self" was the function itself. Is there something that could allow this? ...
user's user avatar
  • 1,897
17 votes
2 answers
1k views

What happens when you divide by ##?

I've been playing around with sequences a bit. In particular with using ## with unary and binary operators. Let's start simple, the following all make some kind of ...
Martin Ender's user avatar
  • 8,774
11 votes
2 answers
851 views

Combining pure functions

Is there an easy way to combine pure functions into a single pure function? For example, say I have f = #1^2 &; g = #1 - 2 &; and I want to define a new ...
tparker's user avatar
  • 1,856
7 votes
4 answers
2k views

How to find all symbols in an expression and perform an operation on them?

========== motivation =========== Suppose, for example, I have an incoming expression like m = (3-I)x + 4(x y)/(Cos[y]) I want to find all the symbols in ...
Steve's user avatar
  • 1,153
5 votes
2 answers
764 views

Multiplying expressions within a list of pure functions

Let's say I have a list of pure functions that are nice algebraic expressions: say l = {(#1 - 1)&, (#1^2 + #1)&, (#1^3 - 1)&} What's an easy way to ...
dvitek's user avatar
  • 295
4 votes
3 answers
1k views

Subtracting elements in a nested list

I have a list containing 500 rows, each of 37 elements; i.e., a 500 x 37 matrix. My goal is to substract in each row each element from the first. My approach in mind is to call each list by a ...
jollyjumper's user avatar
8 votes
1 answer
2k views

nested use of Apply/Map/MapThread in pure functions

1. My main question I have a function with two argument slots. I wan't to apply this function to 2 lists with different length's. I thought in this solution: ...
João Cortes's user avatar
7 votes
5 answers
1k views

How to write an operator that acts on pure function (to give another pure function)

I would like to write an operator oper that acts on a pure function f with an undetermined number of arguments, with the syntax <...
QuantumDot's user avatar
  • 19.7k
5 votes
2 answers
243 views

Conflict with variables in pure functions

Consider the following snippet of code. Clear[f, g] f[x_] := Function[{i}, x]; g[h_] := Module[{Z}, Z[i_] := Print[h]; Print[h]; Z[1]; ]; When ...
anon's user avatar
  • 53
5 votes
1 answer
116 views

Condition inside pure function

Why condition /; not work inside pure function? Suppose we have: ...
lesobrod's user avatar
  • 1,699
4 votes
5 answers
321 views

Using Through with SlotSequence

I have an expression consisting of a few pure functions added together like so: f+g+h I want to add the bodies of these functions together and make that a pure ...
Myridium's user avatar
  • 1,089
3 votes
2 answers
837 views

Outputting expressions in the order in which they're entered

Background For display purposes, I sometimes find it desirable to defeat Mathematica's canonical ordering of variables, and instead have it output terms in the order in which I type them. E.g., ...
theorist's user avatar
  • 3,633
2 votes
1 answer
485 views

Requiring NumericQ in Pure Function

Potential duplicate, but doesn't seem to solve my problem: Using patterns in pure functions I have a function that returns an InterpolatingFunction, and it works ...
polyglot's user avatar
  • 775
10 votes
3 answers
534 views

Operate over list of pure functions

I have list of pure functions (All functions are InterpolatingFunction) i.e {{a, b}, {c, d}, {e, f}, ...} and I would like ...
rhermans's user avatar
  • 36.7k
10 votes
1 answer
184 views

What are the scoping rules for function parameters shadowing System` symbols?

Here are some very contrived code snippets, highly unlikely to appear in real code, but still I am curious why they behave like this: ...
Vladimir Reshetnikov's user avatar
9 votes
4 answers
471 views

Construct a pure function containing built-in 'Sequence'

I guessed the output for (Sequence[#, 7]&)[3] should be Sequence[3,7] But my guess was wrong. See the screenshot: And I ...
imida k's user avatar
  • 4,295
8 votes
1 answer
926 views

Pure functions are not functions?

According to the manual: "HoldAll is an attribute which specifies that all arguments to a function are to be maintained in an unevaluated form." The following code yields, as expected, ...
Hector's user avatar
  • 6,438
7 votes
1 answer
334 views

Using the list as the argument of the pure function

I know some function is listable, such that Sin@{1,2,3,4,5} gives {Sin[1], Sin[2], Sin[3], Sin[4], Sin[5]} By reading some ...
Ice0cean's user avatar
  • 813
6 votes
1 answer
244 views

Select with test function that depends on # [duplicate]

I'm trying to teach myself to use pure functions and slots as much as possible. Here is my problem. I have a list: ...
BlacKow's user avatar
  • 6,438
5 votes
2 answers
184 views

Slots inside nested pure functions

I'd like /@ instead of Table or Map. But let we have it inside pure function: ...
lesobrod's user avatar
  • 1,699
5 votes
3 answers
201 views

Function incorrectly renaming variables in Rule Pattern

Consider the simple pure Function containing a Rule with the named pattern q. ...
Anti Earth's user avatar
  • 1,211
4 votes
2 answers
484 views

Defining a sequence slot programmatically

This question is related to: Composition of functions using slots `Slot` (#) interfering with evaluation Consider a toy example: ...
Pam's user avatar
  • 1,867
4 votes
4 answers
647 views

Pure function, one slot refers to List-position

I used all my google-fu trying to find how to make this work; read the docs as well. What have I missed? I feel sure this can't be a new question, but I can't find anything Here is is: ...
Paul_A's user avatar
  • 487
3 votes
1 answer
114 views

Soft question: Is it feasible to have argument names with the shorthand (&) pure function syntax?

Nested pure functions can be messy, code length wise and syntax wise. It seems that the ampersand (&) in pure functions is always either proceeded by nothing: ...
user avatar
3 votes
1 answer
192 views

Pure functions failing in algebra

f[equation_] := Module[{}, ContourPlot[equation[x, y] == 1, {x, -2, 2}, {y, -2, 2}] ] equation = (#1^2 + #2^2) &; f[equation] The above code draws a circle....
feynman's user avatar
  • 229
3 votes
3 answers
512 views

How to exchange two arguments?

Recently, I have being using Mathematica to write my undergraduate paper. I want to use two pattern lists such as { A -> a, B -> b, c -> C, ....} and ...
user avatar
2 votes
1 answer
145 views

Minimize Sum of Hamming Distances

Problem: Given an $n \times m$ matrix with binary values $(0,1)$, find a binary vector that minimizes the sum of Hamming Distances to the rows. Example: If the matrix is {{$1,1,1$},{$0,0,0$},{$1,0,1$}}...
B A's user avatar
  • 35
2 votes
0 answers
229 views

Generate a list of variable names [duplicate]

How can I generate a list of variable-names like {p0, p1, p2, p3} etc... I tried: mylist = p # &@Range[1, 4] But then the result is: {p, 2 p, 3 p, 4 p}
GambitSquared's user avatar
1 vote
2 answers
245 views

`Slot` (#) interfering with evaluation

In the following I am using one function makeFunc to assemble two functions a[t] and b[t] to ...
Andy Mobley's user avatar
0 votes
2 answers
873 views

How combine pure functions of several slots? [duplicate]

How can one define in a functional way a 1st-order linear differential operator involving several independent variables that can then be applied to a function of that many variables? Consider an ...
murray's user avatar
  • 11.9k
0 votes
1 answer
779 views

How to define a pure function with a Module?

I was not able to use a self defined (using Module and For loops) piecewise function to fit data. It seems that the problem is that I have to define a pure function. I would like to learn how to ...
user1541776's user avatar
-5 votes
1 answer
1k views

Split dataset based on the first column [duplicate]

I have a dataset which I want to split based on the first column. The data looks like this ...
ALEXANDER's user avatar
  • 1,239