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
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
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
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
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,757
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
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,877
28 votes
1 answer
942 views

When is ##@@@##& more appropriate than ##@@##&?

When assigning attachment operators, I often run into confusing statements like ##@@##&, ##@@@##&, or even ...
Roman's user avatar
  • 47.5k
24 votes
1 answer
843 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
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
21 votes
2 answers
928 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.6k
20 votes
2 answers
660 views

Access last variable in a pure function

# is the first argument of a pure function, #n the nth, ## are all variables and ...
riddleculous's user avatar
18 votes
3 answers
815 views

Combining # & pure functions with post-fix notation

I can't find the link anymore but there was a talk at a Wolfram conference (referencing v7 I think) where the authors highlight that stringing together functions like this: ...
Joe's user avatar
  • 1,467
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
16 votes
4 answers
2k views

Can you plot pure function without specifying variable?

Given a pure function, e.g. f=Sin[#]&, it is possible to plot it with introducing an arbitrary local variable, e.g., ...
JeffDror's user avatar
  • 1,880
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
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
14 votes
1 answer
121 views

Bug when combining Dataset with a scoping construct and Map

The following fails in v11: ...
Mohammed AlQuraishi's user avatar
13 votes
2 answers
1k views

How to programmatically covert a function-like rule to a pure function?

Is there a programmatic way to convert a Mathematica rule that acts like a typical mathematical function of one variable into the equivalent pure function? For example, the code should convert ...
ibeatty's user avatar
  • 2,533
12 votes
2 answers
243 views

The use of SlotSequence in If[#1 > #2, ##] &

The below function when applied to a list of two elements, will output the maximum between the two: If[#1 > #2, ##] & For example: ...
WeiShan Ng's user avatar
12 votes
1 answer
287 views

Practice notebook for Pure Functions & Rules-Patterns

Are there practice notebooks or exercises for practising using pure functions and rule-patterns? I've completed "An Elementary Introduction to the Wolfram Language" and "Fast Introduction for ...
I should change my Username's user avatar
11 votes
4 answers
721 views

Using predefined expressions as free variables in a pure function [duplicate]

I want to have a function value of an expression where some variables are solutions to some set of equations, with some values of parameters. I had an idea to use pure functions for that. However, ...
Picek's user avatar
  • 125
11 votes
1 answer
800 views

What's the meaning of #0? [closed]

Here's a line of code from a handbook written by Stephen Wolfram, which turns out to be very complicated for me. ...
Shawn's user avatar
  • 113
11 votes
2 answers
602 views

Performance of Nest vs. Do

When I am iterating the logistic function using a pure function, comparing Do and Nest: ...
Stitch's user avatar
  • 4,225
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
11 votes
2 answers
845 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
10 votes
5 answers
1k views

Pure function of lists

I need to create a function with two arguments f(s, l): s is a list of three integer like: s={1,-3,+5} and l is a list of lists of naturals of same length: ...
Giovanni Russo's user avatar
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.5k
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
470 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
9 votes
3 answers
590 views

Functional equivalent of a For loop referring to consecutive elements in a list

I have implemented this function which smooths the values in a list by, if it encounters a zero, replacing this entry by the average of its two neighbours (apart from the first and last values of the ...
ben18785's user avatar
  • 3,167
8 votes
5 answers
3k views

How to elegantly map multiple functions over a matrix?

...
hengxin's user avatar
  • 840
8 votes
5 answers
702 views

Slot # corresponding to subset of a list in select

Is it possible to use # in the select function preserving the structure of the list? Here an example of what I mean. Let's say I want to select all the pairs where the first element is 1: ...
Fraccalo's user avatar
  • 6,057
8 votes
3 answers
497 views

How can I create currying functions using pure function syntax?

For example how can I write In: pureFunctionSyntax[myF] /@ {7, 3} Out: {myF[7, Log[7]], myF[3, Log[3]]}
user's user avatar
  • 1,877
8 votes
4 answers
560 views

Pure function on pure function

Given t1 = {2, 4, 8, 16}; t2 = {1, 5, 9}; First[Select[t1, # > 1 &]] First[Select[t1, # > 5 &]] First[Select[t1, # > 9 &]] can somehow be ...
user57467's user avatar
  • 2,728
8 votes
3 answers
579 views

Have I found a big difference between using the short form and the long form of a pure function?

By referring to the Mathematica documentation, I learned to use ##2 to represent all the arguments but the first one. This is a brief way to represent a pattern ...
Life's user avatar
  • 660
8 votes
2 answers
742 views

Creating a pure function that does operations with pure functions

Suppose I have two functions defined as follows: a = #+3& b = #^2 + 3& So when I call a[2] and ...
Vahagn Tumanyan's user avatar
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
8 votes
1 answer
314 views

How to supply argument to pure function when slot is already defined?

Here's an example of a pure function: 3^# > 100 & I can supply any value I please to the function, by placing that value after the prefix. E.g.: ...
theorist's user avatar
  • 3,633
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
8 votes
1 answer
346 views

Mma10 mystery: Failure of pure function substitution with Interpretation?

Bug introduced in 10.0.0 and fixed in 10.0.1 I had code to display with subscripts from Displaying index as subscript on output: e.g. C[i] -> C_i with Notation[...] or Interpretation[..]? which is ...
jlperla's user avatar
  • 967
8 votes
0 answers
279 views

Performance of pure function - Best way to define a function?

In general, to define a function, we often use f[x_]. Another way is to use pure function (full form and shorthand). I want to test the performance of these ...
Nam Nguyen's user avatar
  • 1,761
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
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
7 votes
5 answers
218 views

How to apply the multiple composite functions to a list?

A minimum example is this: ls = {1 + I, 2 - I}; Max@Re[ls] Min@Im[ls] #[ls] & /@ {Max@Re, Min@Im} (*want {2, -1}*) The result is: ...
an offer can't refuse's user avatar
7 votes
5 answers
504 views

An anonymous function with constraints [duplicate]

I'm not very good at working with anonymous functions, a fact that has now come back to bite me. Suppose I have a function defined as follows: ...
user26718's user avatar
  • 366
7 votes
3 answers
706 views

How to use multiple pure functions correctly?

I have used Mathematica for two years.And usually,I can use single pure function(#&) rightly.However,for the use of multiple pure function,I always make all ...
user avatar
7 votes
2 answers
831 views

Replacement Rule for Functions

Attempting to write a rule that will substitute an undetermined function of several variables by some other undeterminate functions (also, of the same several variables). I can do it in an inelegant ...
user3755605's user avatar
7 votes
1 answer
333 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
7 votes
0 answers
201 views

Can Wolfram Language enforce pure functions?

Is there a feature in Wolfram Language that supports or enforces pure functions? WL has something that it calls "pure functions" in its documentation, but these are most emphatically not pure ...
orome's user avatar
  • 12.8k

1
2 3 4 5 6