A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.

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
47
votes
4answers
2k 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 ...
47
votes
2answers
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 ...
32
votes
3answers
2k 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 ...
31
votes
2answers
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. ...
28
votes
8answers
2k 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 ...
26
votes
6answers
3k views

Second level depth pure function?

I have the following working construction: Select[ln125, # == Nearest[ln125, 551.748][[1]] &] Here, ln125 is a one ...
26
votes
1answer
1k 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? ...
21
votes
2answers
801 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 ...
20
votes
2answers
663 views

Using several anonymous functions mixed together

This works: list // BarChart[#[[2]], ChartLabels -> DateString @@@ #[[1]], ChartStyle -> "Pastel"] & This doesn't: ...
19
votes
2answers
568 views

Access last variable in a pure function

# is the first argument of a pure function, #n the nth, ## are all variables and ...
17
votes
2answers
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 ...
15
votes
4answers
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., ...
14
votes
3answers
560 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: ...
14
votes
3answers
361 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 ...
14
votes
1answer
115 views

Bug when combining Dataset with a scoping construct and Map

The following fails in v11: ...
14
votes
2answers
243 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 ...
11
votes
4answers
541 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, ...
11
votes
1answer
565 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): ...
11
votes
1answer
728 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. ...
11
votes
2answers
197 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: ...
11
votes
2answers
494 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 ...
11
votes
1answer
235 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 ...
10
votes
2answers
999 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 ...
10
votes
3answers
469 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 ...
10
votes
2answers
418 views

Performance of Nest vs. Do

When I am iterating the logistic function using a pure function, comparing Do and Nest: ...
10
votes
1answer
165 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: ...
9
votes
3answers
548 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 ...
8
votes
5answers
412 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: ...
8
votes
3answers
478 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]]}
8
votes
3answers
559 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 ...
8
votes
2answers
554 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 ...
8
votes
1answer
819 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, ...
8
votes
2answers
342 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: ...
8
votes
1answer
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: ...
8
votes
1answer
336 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 ...
7
votes
5answers
2k views

How to elegantly map multiple functions over a matrix?

...
7
votes
5answers
654 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 <...
7
votes
2answers
197 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 ...
7
votes
1answer
193 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 ...
6
votes
3answers
607 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 ...
6
votes
1answer
88 views

How to use a function as a value in an association? [closed]

I just started learning Mathematica. I was wondering if it is possible to use a function as an value in an association. Ideally, it would be something like this: ...
6
votes
3answers
455 views

Simplifying the body of a pure function

After solving a differential equation I obtain a pure function which I wish to simplify. I am able to simplify the corresponding expression ...
6
votes
1answer
167 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: ...
5
votes
4answers
1k 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 ...
5
votes
1answer
517 views

Why does #0[[1]] &[] output 0?

When a pure function accesses the first element of itself, 0 is output. What is the logic behind this, or is there any?
5
votes
1answer
245 views

How can I make a “self-referential” replacement operation self-contained?

Fairly often I find use for replacement rules that call themselves on the right-hand side of the rule, e.g.: ...
5
votes
3answers
230 views

pure Function[ ] named formal parameters

I am trying to setup a bunch of pure Functions in particular with an identical rahter long parameter list. ...
5
votes
4answers
369 views

Syntax for Table with a pure function

I have: g = #1^2 + a #2^3 / #1 & Now I wish to make a Table evaluating this from 1 ...
5
votes
1answer
386 views

Pure functions instead of For loop

How to get following loop output using pure functions such as NestList? For[i = 0; v = 0, i < 4 && v < 4, i++; v++, Print[x^i + b^v]] and the ...
5
votes
1answer
697 views

Writing lambda expressions in terms of pure functions

I am trying to create some functions that I want to comfortably compose together using the pure functions. The functions were originally written in $\lambda$-expressions so I would like to keep the ...