# 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.

183 questions
Filter by
Sorted by
Tagged with
859 views

258 views

### How to convert numbers to currency values?

I am trying to create a way to convert some values of numbers into currency (dollars). The example I display below takes in three random numbers between -4 and 4, and rounds them to the second decimal ...
140 views

### How to produce this list of pure function pairs

I need to produce this ...
115 views

### Bug when combining Dataset with a scoping construct and Map

The following fails in v11: ...
244 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 ...
545 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, ...
346 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: ...
821 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, ...
37 views

### How to use OptionValue inside a pure function [duplicate]

I want to define a function with OptionsPattern as argument which returns a pure function using OptionValue: ...
200 views

### Optimization of the solution to an ODE

Apologies if this is obvious -- I'm very new to Mathematica. I'm trying to minimize the solution to an ODE with respect to a variable. The following code generates the solution to the ODE, ...
168 views

### Evaluate pure function through round brackets

In have some matrices of pure functions or numbers which I multiply by other matrices or vectors. I would like the functions to be evaluated in the result, but all that I get is something like this: <...
514 views

### Solving a pure function to obtain function definition and then map that function

I've been trying to use Solve with pure functions to receive function definition and then use it with Map , to obtain results from mapping function to some data. Here is an example ...
196 views

### Automatic generation of pure function

My main problem consist in the creation of a sort function which wolud be used inside SortBy. Introduction The principle of the sort to apply is the following (...
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., ...
90 views

### Matching All Functions [duplicate]

Is there any way to match all functions? There's an existing answer for matching functions of one parameter (_@_) but it doesn't work for trying to test if a symbol ...
374 views

### How to force evaluation inside the body of a pure function?

For example, I'm writing a function to scale my list of numbers into a specific range. It goes like ...
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: ...
384 views

### How to use Select according to a list of criteria to produce a list of subsets of another list obeying those criteria [duplicate]

The code Print[Select[{1, 4, 3, 9}, Mod[#, 3] == 0 &]]; Print[Select[{1, 4, 3, 9}, Mod[#, 3] == 1 &]]; Print[Select[{1, 4, 3, 9}, Mod[#, 3] == 2 &]] ...
258 views

### Variable naming conventions for nested pure functions [closed]

My understanding is that for nesting pure functions, one should use the explicit version of pure functions. I would like to know the best way of making it readable for people. I currently have ...
126 views

### Creating a list of unevaluated pure functions with Apply

Suppose I have a list list with length L and a function ...
58 views

### Deciphering Coding Shortcuts [duplicate]

I'm having trouble deciphering why the following couple of expressions evaluate the way they do. #&@@ This returns the first value of a list. Why? Naïvely I ...
133 views

### How to write this code as a pure function? [duplicate]

The code has three places with same variable ls. So I think it would be possible to write it as a pure function. ...
37 views

### Why is g[#1, #2] & different from g[##] & for a function with two variables in this example? [duplicate]

If we only consider functions with two variables, I expect g[#1, #2] & is the same as g[##] &. But in the following ...
94 views

### Translating syntax of a pure function

I have a matrix in complex exponential form. I want to factor out the complex exponential form and then express the inside in the a+i*b form. For example: \begin{bmatrix} e^{i \frac{\pi}{4}} ...
87 views

### How to get the values out of Alternatitves? [closed]

Saying we have something like: SomeFunction[ ... { Alternatives[1, 2, 3] :> "something" } ... ] How can I know if ...
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]]}
519 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?
88 views

### How to use pattern matching to apply a function to specific elements of a matrix

I have a 3x3 matrix testweightedgraph = {{{}, {p, q, pq, pq}, {}}, {{p, q, pq, pq}, {q}, {}}, {{}, {}, {}}}; whose elements are (possibly empty) lists. I would ...
149 views

### Replace variable with # and pass function body as an argument to a function

I have a very simple problem, which I would like to make a bit general, but none of my naive approaches have worked yet. This is what I want to do step by step: Define a parameter: ...