# Questions tagged [functional-style]

Questions about Mathematica's functional programming style, including the use of pure functions (Function[], #, &) and functions such as Map, Apply, Nest, and Through.

355 questions
Filter by
Sorted by
Tagged with
42 views

### How to build a list by successively applying a function while varying only one parameter?

I have to create a list by varying one parameter of a function. For now, I resort on that solution: Map[Function[m, f[arg1, arg2, m, arg4]], Range[-3,3]] ...
298 views

### Intuition behind FoldPair and SequenceFold?

In trawling through the documentation I've found the functions FoldPairList and SequenceFoldList. Examples are given of how to use FoldPairList: Partition a list into sublists of different lengths ...
2k views

### Using a list of tuples in a pure function

I want to use a list of tuples within a function to make assignments. Say I want to make assignments of the form value[i,j] = val What I have is a list of ...
102 views

### How to make FoldListIndexed ie FoldList with an index or iterator?

Is there a functional or inbuilt way to add an index to FoldList? i.e.: FoldListIndex[f,x,{a,b,...}] gives ...
98 views

### Implement gradient method in functional programming style

I implemented the gm for quadratic function in Mathematica, but it looks like more in an OOP style, as the pic shows. How to rewrite it in Mathematica style? I tried with following code: ...
370 views

### Using vtx[] instead of vtx

There're two ways to generate a random tree as 1 and 2 below. Both work fine here. So what's the reason to use vtx[] instead of ...
284 views

### Hold attributes vs performance

Is there a reason to use Hold* attributes for functional code (e.g. no intention to mutate input)? I'd expect performance gains as in pass by value vs pass by ...
18 views

### InverseFunction cannot evaluate negative arguments

I have difficulty figuring out where my code fails. I want to obtain $\textbf{ry}$ which is the inverse function of rho (see below). When I input positive values for its argument, there's no problem. ...
27 views

### Easiest way to simultaneously keep track of two values in a list

I have a bunch of functions a,b,c,... that all take in a list of images and return a list consisting of a list of transformed images, and a number (let's call it ...
81 views

### Define a curried anonymous function

I want to define a function like f[x_][y_][z_] := x + 2y + 3z but anonymously. The ways to do this that I've discovered so far are (1) define a normal function ...
38 views

### How can I consolidate mapping code between different levels of lists?

I have a list with the following structure: ...
100 views

### Map vs. Table for index-specific operations on 2D arrays–Part II

This is an extension of my recent question, Map vs. Table for index-specific operations on 2D arrays For that question I gave a minimal working example, since I was more interested in learning ...
63 views

### function application f//x when the function has more than one parameter [closed]

I want to use the function apply (or something even better). x//f with a function that has more than one parameter. Here is how it works with one parameter. <...
2k views

### Going full functional (Haskell style)

I'm trying to define some notation so that Mathematica code would be more functional, similar to Haskell (just for fun): currying, lambdas, infix operator to function conversion, etc.. And I have some ...
70 views

### How can I ask the Wolfram Language to tell me if a hypothetical expression with a particular head would be atomic or not?

Integers in the Wolfram Language are atomic expressions, while uses of the Map function are not atomic. But if you didn't know this, how could you find out using only the Wolfram Language? The best I ...
120 views

### Replacing loop with functional style

I keep hearing that one should avoid loops in Mathematica and try to write everything in functional style. I've seen few examples of how this done on here, but I could't apply to my problem ...
297 views

### Operator currying: how to convert f[a,b][c,d] to {a+c,b+d}?

This question is related to this golfing question (but I'm not interested in golfing, only in functional operator composition): How can we convert f[a,b][c,d] to <...
164 views

### Generate random non-zero “island” in matrix of zeros

I'm trying to write a code which will generate a 3 dimensional array of 0s, then pick some random location in the array, and subsequently expand contiguously to random neighboring sites until a set of ...
57 views

### Efficient ListConvolve over 3d matrix

I'm trying to find an efficient way of applying list convolve to a 3d matrix of 0 and 1, where about 5% of the matrix are 1, but the 1s may either be randomly placed, or grouped in one large ...
1k views

### How best to simulate n-body systems in a functional way?

I want to simulate a N-body system in the most functional way possible. The system I'm modeling are metallic colloidal particles in a solution of peroxide water, as here http://arxiv.org/pdf/1502....
89 views

### Best way to apply a list of functions to a list of values?

This question is closely related to questions 83720, 17460, and 11298. How would you write the operator F = Through[#1[#2]] & in the prettiest, fastest, or ...
1k views

### Prefix operator with low precedence

The question is simple, but I will elaborate on the background as well for those interested in the idea: How to define a new operator with specified precedence value? Background Mathematica was ...
43 views

### Changing one argument of a multivariable function at each step of Fold

I have a function, call it F[a_List,b_,c_,d_] which takes in 4 arguments. Now I want to compose this function with itself, where the number of compositions I want ...
37 views

### Construction of symmetric function / matching function argument patterns

[Edited for clarification after comment] Suppose that the function f is defined as the linear combination of several translated/reflected functions starting from the primitive f0[x,y,z] ...
32 views

### Constructing sequence of function with elements depending on the previous one / recursion

Starting from a function $\theta0[x\_ , y\_ , z\_ ]$, the terms $\varphi[x\_ , y\_ , z\_ ]$ constitutes a sequence of functions that is constructed as follows: Order 1 (one function and its ...
169 views

### Applying FoldList to its own output

I'm trying to figure out a functional way to essentially apply FoldList (or some related function such as Nest or ...
55 views

### How to handle exceptions and failures in Mathematica?

In python I can write fallback values like: ...
479 views

### How to build private function with built-in function's option

Since this code work well: ...
122 views

### Append a function of an expression as operator form using Curry?

I fielded this q to tech support but didn't get a conclusive answer yet: Is it possible to use Curry specifically to modify ...
887 views

### Meaning of ## #

While looking for an answer to a different problem, I came across the following example: In:= Array[Plus[##] &, {2,2}] Out= {{2,3},{3,4}} Having read ...
116 views

### Tag Set Delayed and D

I used the Tag Set Delayed to set F /: D[F[f_], x_] := F[D[f, x]] It works if I give as input a single F: ...
53 views

### Is using Length more efficient than using === as stopping criterion?

I am using NestWhileList[ f[#, elems]&, pairs, Length[#1]<Length[#2]&, 2, Infinity, -1 ] to generate all suitable subsets from ...
2k views

### Cleanest way to take a[b[c]] to a[b][c]

As indicated in the title I'm looking for the fastest way to transform a[b[c]] into a[b][c], and the natural generalization to ...
1k views

### Converting expressions into functions

I have defined an expression, such as a = x I would like to convert this into a function, such as f[x_]:=x However, in my ...
170 views

### Is there any way that convert entire mathematica code to java or python?

I am novice to Mathematica. I want to convert Mathematica code from my textbook to Java or Python to trace coding.
261 views

### Take single element of a list, and compare to average of whole list

If I have a list lst = {1, 2, 3, 4, 5}; Is there some way of taking the first element and then comparing it to the average of the other 4? Then to take element 2 ...
46 views

### Pure function: using T[[#]] where # indicates a position [duplicate]

When using T[[#]] in which # indicates a position, how do you pass on the position # (which ...
73 views

### Implementing a function so it accepts functions, function names and expressions as an argument

I'm trying to write a Mathematica function which takes an arbitrary expression as input. More concretely, I'm trying to write an integration function (utilizing the trapezoidal method), the input of ...
113 views

### Surprising result using operator form of FoldList [closed]

I was trying to implement some Graph related functions in Functional form and came accross this result which I found surprising. I want a function that does various operations on a graph. The ...
101 views

### Use iterator outside of iteration

I want to Map something that is depending on an iterator from the outside of e.g. Table into the procedure. What would be the ...
59 views

### Switch applying tests to forms

Is it correct that the Select equivalent of Cases is missing for Switch? I mean a function <...
1k views

### What makes mathematica a “functional programming language”?

According to the website of mathematica itself, mathematica is a "functional progrmaming language". I am trying to understand what "functional programming" is. I don't think I have ever worked with "...
5k views

### How do I convert an argument list to a sequence of arguments?

I think this is a basic question, but I am having difficulty finding the answer in the documentation. Thread is not what I am looking for, I think. Suppose that I ...
597 views

### Analogies to Help New Users

In many of the classes that I teach, I require students to learn the basics of Mathematica which we use throughout the semester to do computations and to submit homeworks (in notebook form). Some ...
97 views

### Creating a bound method (partial function application)

I want to bind arguments to methods (e.g. for use in OOP creating a bound method--what Java just calls a method). I realized this is basically just a partial function application. Now I want to make ...
510 views

### Are there advantages to using additional arguments of common functions rather than alternative ways of calculating with lists?

(Apologies for the long question title.) One of the interesting, if sometimes confusing, things about Mathematica is that there is always more than one way to do things. Even intermediate users can ...
5k views

### Functional style using lazy lists?

Let's say I want to answer the question "what are the first 400 palindromic prime numbers?" The first approach that comes to my mind from the set of languages that I know is to use some sort of lazy ...
209 views

### Function that returns a list of unevaluated functions in mathematica

I have a list of that contains words of two non commutative variables. Something like : ...