The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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

352 questions
Filter by
Sorted by
Tagged with
129 views

### Repeatedly apply a bivariate function with one argument running over a list

I'd like to know how, given symbols f and g, and a list {a,b,c,...}, to produce the ...
63 views

### Deciding whether the return value of a function is defined based on its input [duplicate]

I have been met with the following problem: I have defined the following: myFunc[e_Integer]; That means, if I call: ...
123 views

### Iterations in Mathematica [closed]

this is probably a very easy question for all of you. I have a stochastic Mathematica code (not important what it's exactly) which gives me a number at the end. Now I want Mathematica to run this ...
209 views

### How to terminate early a functional computation

Problem description: Given a list $A=\{1,2,0,7,0,-1,2,6,\ldots\}$ and a list $B=\{0,7,0\}$ write a program that determines whether $B$ appears in $A$ or not. Note: the order of $B$'s items does matter....
677 views

### Does Mathematica have a functional programming idiom to loop over a list till a condition is met? [duplicate]

The module below takes a list of integers and performs fadic addition on it (if a bit slowly). ...
260 views

### A better way than using nested For-loops to make a search list

I'm generating a List that can be up to 26 values long, a simplified example is below. Is there a better way than nested loops? ...
189 views

### How can I get all pairs of values from an association with elements key->list

I have an association of the form ds=<|key1 -> list1, key2 -> list2,... ,keyn -> listn|> I would like a simple way to map a function to all pairs <...
259 views

### List of functions to list of parameters

I am working in a Dataset and I need to create a function for a ChartStyle option of a ...
188 views

### Writing code which generates a function to append many columns to a dataset

I love datasets in Mathematica. Say I have one that looks like this: ...
238 views

### Through does not work with Subtract

Through[(Max - Min)[#]] & @ {1, 2, 3, 4, 5} gives 5 + (-Min)[{1, 2, 3, 4, 5}] Of course I can work around by ...
259 views

### The equivalent of dplyr mutate with Dataset

In R, there is a package "dplyr" that works on data.frame objects, which is one of the origins of the Mathematica Dataset construct. One of the things dplyr can do is "mutate" the columns of a data....
143 views

### functional way to identify & summarize repeated elements in List?

I have a List, for example (assume it's always ordered like this one): myList = {6, 6, 8, 10, 14} I would like to get a result like this: ...
171 views

### Using Map multiple times in one command

Is there a way to reproduce the effect of Table[i ** j, {i, list1}, {j, list2}] using Map instead of Table? Specifically, I want to write something along the ...
98 views

### Is there a way to create a table where the number of dimensions is parameterized?

I'm imagining the likes of this: ...
437 views

### Given a list of triangle vertices, how to find the neighbors for each vertex?

A triangle is represented by {a,b,c}, indicting that its vertex IDs are a, b, and ...
168 views

### Unevaluated @ {args} versus Unevaluated /@ {args}

Working my way through Robby Villegas's lovely notes on withholding evaluation, I almost got Polish Notation on my first try. Here is my final solution, which seems to work well enough: ...
406 views

### Use Map with multiple arguments over a nested list

I noticed there are a few similar questions to mine on here, but I couldn't figure out how to accomplish this mapping. I have sets of data I want to plot, which represent intensity and frames, and I ...
517 views

### Multiply pure function with scalar

Somtimes I still get lost in the Hold, HoldAll etc. functions of Mathematica. I want to scale a simple PDF function with a previously calculated variable e.g. ...
95 views

### Mapping Linear Model Fit Over An Arbitrary Number of sets

I have multiple sets of data, and I want to apply Linear Model Fit sequentially to each set of data. For example, below tstlst contains 3 data sets. Right now I ...
2k views

...
132 views

### How to define a function to save a list to a same-name file

I want a convenient way to save lots of intermediate data lists into files, so that next time the files can be imported automatically to homonym list variables in the notebook, without having to ...
138 views

### Better to use loop or function when populated list?

In the past I have pretty much stuck to expression building when using Mathematica, but recently I have been writing a procedure to populate an array, which I guess are called "lists" in Mathematica. ...
577 views

### Mimic a procedural, recursive clustering algorithm for site percolation using functional programming

Sorry in advance for my logorrhea: I just want to make sure all of the information is here. Context and Question I am investigating site percolation on a square lattice. I have a working, depth-...
130 views

I have data ...
114 views

### How to force evaluation on the right of an @ operator?

Is there a way to force evaluation on the right before the Prefix operator (@) without using parentheses? I came across this question when I was working with the ...
417 views

### Handy / flexible directory tree operations

Is there any shorter / more handy / more flexible way to get directory tree as a nested Rules/Associations list? I tried to incorporate ...
67 views

### Returning functions that are partly evaluated

I have some performance issues when calling Table[{h, Slow1[h, n]}, {n, nlist}, {h, 0.1, 10, 0.5}]; for a given list ...
181 views

### How can I do a two dimensional Fold? [closed]

I'd like to Fold over higher dimensional objects than lists. Semantically, the format would be something like ...
117 views

### Is it possible to sum the results of a Map - Apply?

I am fairly new to functional programming and would like to replace a For[] loop that sums the results over a Dataset. This may be a bit different since the ...
740 views

### How would I use Map on a Dataset column?

I am somewhat new to functional programming. To improve the performance of a package, I would like to re-write a package in a functional style. I have the following ...
272 views

### find position of nearest nonzero element in an array (functionally)

Given an array sel and an index position i0, how can I find the position of the nearest (left or right) nonzero element? I'm ...
1k views

### How to Fold over lazy lists and over tasks, just as with Lists?

Many modern languages, such as Haskell, feature lazy lists, which can model infinite sequences of values, albeit synchronously. Lately, the reactive programming movement has shown that asynchronous ...
2k views

### Elegant functional equivalent to a nested loop?

I have a square $n \times n$ matrix $m$, and need to apply a function $f$ to all elements on and above the diagonal. This is of course easy to do using a nested table: ...
1k views

### What is the difference between Composition (@*) and sequential applications (@)?

What is the difference between f@*g@*h@x and f@g@h@x Both evaluate to f[g[h[x]]] ...
2k views

### Replacing a for-loop with something functional

I'm a physics grad and have been using Mathematica mostly for data visualization. The evaluation part I normally do with C/Python, but I've been wanting to try out Mathematica for these tasks, too. ...
241 views

### How can I get from a list of characters representing a permutation to graphics showing the cycles?

I am trying to graphically represent cycles in various permutations of the alphabet, and have a question in two parts. I have ...
157 views

### Applying functions repeatedly on parts of complicated expressions

I am dealing with expressions like  \textrm{expr}=5D_{4,1}\langle12\rangle\langle23\rangle\big(-\langle15\rangle^2\langle24\rangle\langle34\rangle + \langle14\rangle\langle15\rangle\langle24\rangle\...
137 views

### How to define the following bracket?

My quetion is about the definition of the so-called Courant Bracket. Given two generalised vector field, defined as a formal sum of a vector and a one-form, i.e. $X = x + \lambda,\ Y = y + \omega$, ...
403 views

### How to evaluate a function on a list until stable?

I need to evaluate a function func on a range of integers (NOT recursively, just sequentially) until the result is stable enough. I can write a not very clever step-...
295 views

### The usage of MapIndexed, can it be removed?

project euler 67 When I'm solving this problem, my solution is as following. You can download the data file there. ...
222 views

### Help going from iterative to functional

The following code segment shows what I'd like to do. I'm a procedural programmer trying to learn the Mathematica functional style. Any help on this would be appreciated. ...
265 views

### Functional-programming virus infection

Update: The question may seem too broad, but the answer provided by belisarius responds perfectly to the question, as the use of $ListConvolve$ and $NestList$ meets the desired criteria in in addition ...
74 views

### Let's do this list-rearrangement prettier [duplicate]

Suppose that i have a list in this format= $list=\{ \{a,obj1 \} ,\{c,obj2 \}, \{a,obj3 \}, \{b,obj4 \} \dots\}$ I want to construct a new list in this way: \$ newlist= \{ \{ \{a,obj1\} , \{a,obj3\} ....
1k views

### Iterating over a list, in a functional way

I have learned a lot about functional programming just from messing around in Mathematica, but I've come across a problem that I don't know how to solve in a "functional" way. Any help would be ...
156 views

### Combining MapAt and MapIndexed

The very last example in the documentation for MapAt is: MapAt applies f repeatedly if a part is mentioned repeatedly: ...
91 views

### Supply a function as an argument to another, and find its minimum

I would like to minimize a function, which is supplied as an argument to another function, and report back the estimates of the values of the variables that result in its minimum. In specific, I would ...
278 views

### General function taking general number of arguments

The following is an edit of this post, for which a working solution was provided by John McGee. I have a different function and am not sure how to adapt the solution for it: I want to construct a ...
543 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 ...
112 views

### How to improve this function that selects parameters from arbitrary distribution using Recursion?

Below, I have defined a function using Which to select model parameters based upon various conditions. My questions are: Is there any danger in writing this function using Recursion? Is there a more ...
339 views

### Functional programming approach to avoid traditional loops

I'm currently learning from the wonderful post: Where can I find examples of good Mathematica programming practice? And i'm correcting my habits using Nest/Map/Thread instead of loops. The problem ...