Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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

1
vote
1answer
43 views

How to write conditional loop structure in a function based on input passed through that function

I know the question is rather vague so, here is what I am trying to do: Lets say we have the following function: `color[data_,variableInputs_]. Now what I want this function to do is loop over the ...
6
votes
0answers
62 views

What is the philosophy behind \[Rule] (a.k.a. `->`) for assigning options?

Today I was banging my head against my desk when I realized that the problem I was experiencing was due to having typed ImageSize[Large] instead of ...
23
votes
3answers
362 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 ...
1
vote
1answer
45 views

How do I Map Transpose without resorting to Table?

I am trying to replace Table with more efficient wrapping functions like Map, Apply or their derivatives, like MapThread or @@@. The use of Table is a habit inherited from procedural programming, ...
5
votes
1answer
77 views

Packing and unpacking of arrays with Map/Table

How can I create function, that will not unpack packed arrays with Map/Table. For example I want read bytes from opened stream (this is my original goal): ...
1
vote
1answer
40 views

Low level built-in function to transpose expression (not array) [closed]

I have an expression like this F[G[1, b], G[3, y], G[4, y], G[5, u]] that I would like to Transpose to obtain ...
4
votes
5answers
274 views

How to build these functions without nested For loops?

I am trying to solve the 8-puzzle using two heuristics, which are: Pieces out of place and Distance Manhattan. After consulting several websites for example see and some books, I'm with some doubts, ...
2
votes
3answers
218 views

Select an element from each matrix row/column specified by a vector

Let's say we have a matrix (or an arbitrary 2+D list) x and an integer vector l of the same length. What is the most elegant way ...
2
votes
0answers
56 views

How to overload and compile pure functions operating on associations

I am writing a small package for myself for operating on zonotopes. I am storing the generator matrix and the center of the zonotope in an association. I decided to write a small constructor: ...
0
votes
1answer
68 views

Defining a random potential on the unit square

I'm trying to define a random potential on the 2D unit square. For some fixed j, I want to decompose the unit square into $2^{2j}$ smaller squares with sides length $2^{-j}$. On each of the smaller ...
2
votes
1answer
81 views

How to program MapList? [closed]

I would like to know if there is a nice functional way to code up a MapList which applies f to each element of ...
7
votes
2answers
264 views

Using Nest to build continued fraction

To build basic continued fraction one can simply use: Nest[1/(1 + #) &, x, 3] However, it's unclear how using Nest ...
2
votes
1answer
41 views

Problem in defining a function inside a module

I have a problem in defining a function in a module. I looked at Mathematica help and followed the same procedure. ...
9
votes
2answers
227 views

Applying Function with SlotSequence and Lists

I would like to apply a function to a list of lists, while using each of the 2 elements of the list in a SlotSequence in two different places in the function. What I am working with is ...
1
vote
1answer
51 views

Angle between one vector and a list of vectors [closed]

I have one vector v1 and one list of vectors v2. I want to find the angle between them (see below): ...
1
vote
0answers
43 views

Multiple Square Bracketed Function definition

I am familiar with the typical way of defining a function--such as f[x_,y_]:=x+y but I have not found a discussion of definitions such as ...
6
votes
2answers
95 views

Efficient way to partition list of expressions based on its contents

I have a list of polynomials polys linear in a set of variables vars. How do I partition the list based on successive dependence ...
2
votes
1answer
59 views

Nested BlockchainBlockData fail

New 11.3 functions to query blockchains. In trying to build a block link by iterative lookup, this function works for several iterations then fails: ...
0
votes
0answers
61 views

What is the best equivalent way in MMA to micmicking python's generator expression?

In python, there is a syntax called generator expression. For example, [i for i in range(1,5)], [i**2 for i in range(1,5)], and <...
6
votes
1answer
147 views

Position-based Inline syntax for Curry

I would like to update an existing pseudo-curry function: • /: h_[pre___, •, post___] := Function[expr, h[pre, expr, post]]; that I use inline like this - the ...
6
votes
5answers
203 views

When will $1^2+2^2+3^2+5^2+7^2+11^2+\cdots\cdots + p^2$ (where $p$ is prime) be greater than $10^{10}$?

How to solve the problem: When will $1^2+2^2+3^2+5^2+7^2+11^2+13^2+\cdots\cdots + p^2$ (where every base and $p$ are prime, except the base of the first term $1$) be greater than $10^{10}$? (Namely,...
0
votes
0answers
36 views

Functional way to get some info from graph

Given some graph I need to gather some info from vertexes and edges. Here what I have, I think it's rather procedural way. I'd like to know how to make the same thing but in functional way. Have seen ...
2
votes
2answers
82 views

How to build equations from list of lists

I have something like this edgesList = {{1, 3}, {1, 5}, {2, 1}, {2, 6}, {5, 2}, {5, 6}, {6, 1}, {6, 4}} And I need something like this as output. ...
1
vote
1answer
42 views

How to make subscripts from list?

I want to make list of subscripted symbols in functional way. So that given edges list I can get something like the list below/ ...
1
vote
1answer
68 views

A function which performs multiple steps

I'm trying to figure out how I can define a function which takes an input, performs an operation on it, then produces a table from the result. For example, I have a function ...
10
votes
1answer
254 views

Why use pure functions?

I've now read the official documentation of wolfram on (anonymous) pure functions, and the part about (anonymous) pure functions in the book "mathematica programming intro". I believe I understand ...
9
votes
1answer
396 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 "...
28
votes
3answers
854 views
2
votes
0answers
26 views

Scoping of OptionValue for inner functions in closures

Assume a function f is a closure over an inner function g. Both f and ...
6
votes
4answers
144 views

Function for adding a head to an expression

The gist of this question is to find a function that takes a list of functions and a list of arguments and wraps each argument with the corresponding function e.g. ...
1
vote
1answer
48 views

Compute and evaluate function of a function and keep the result a function

Let's assume I have a various functions for signal amplitudes, e.g. pulse[t_] = Sech[t]^2 and pulse2[t_]L = Exp[-2t^2]. What I'...
4
votes
1answer
60 views

Apply after with arguments

Though I have been using Mathematica on and off for many years to help with learning math, it is only recently I have been introduced to its functional programming style and power. So for example if I ...
1
vote
1answer
81 views

Priority of nested function in Mathematica

I am facing a small problem with the nested function in Mathematica. Please take a look at the attached Code below. ...
3
votes
0answers
92 views

How to visualise Mathematica code and how to become a better coder [closed]

I seem to be using functional coding frequently. However, it does not come naturally and I usually copy from example, and then adapt for my own. How do you recommend I improve as a coder? I would ...
5
votes
2answers
92 views

Problems folding - resorting to hacks with `StringReplace`

I would like to FoldList a simple function, with desired output: ...
17
votes
4answers
796 views

Is there a way to do this with Mathematica shorthand notation?

I'm a big fan of Mathematica's shorthand notation, things like @ and /@, which I use the most. It's really useful when I'm quickly whipping something up. However, something has always driven me a bit ...
1
vote
2answers
98 views

How to manipulate a function (Append/Prepend) multiple times?

I'm having trouble trying to manipulate a function in a type of "loop" format. Essentially what I want to do is be able to manipulate the amount of Append functions ...
3
votes
0answers
111 views

How to ScanThread and ScanIndexed?

Why are there MapThread and MapIndexed but no analogous functions for Scan? What is the ...
5
votes
3answers
198 views

Replace Table by functional programming

Just for learning purposes I try to create a list that looks like this: {{{3, 9}, {4, 16}}, {{5, 25}, {6, 36}}} One way to do it is by using Table: ...
1
vote
5answers
114 views

Recursive “InverseFactorial” Function

I am attempting to write a general function that can take any number and back calculate to see if it is the factorial of a smaller number. The easiest and quickest way to do this is to take the ...
2
votes
1answer
73 views

How to get an element of a list by specifying element position as list? [duplicate]

How can I retrieve an element of a list, specifying the position of the desired element as another list? For instance, I like to get list[[2,1]] but all I have is <...
3
votes
1answer
135 views
4
votes
1answer
108 views

Fast way to compute a truncated series using Fold

I'm looking at an efficient and fast way to compute the results of a finite summation $$\sum_{i=0}^Mf_ig_i\,. $$ I noticed that simply applying ...
4
votes
3answers
129 views

Use Apply over a matrix of functions

I have F = {{f11,f21},{f12,f22}}; (*Could be larger*) x = {x1,x2,x3,x4,x5}; (*Could be larger*) I want to have a matrix Fx ...
25
votes
3answers
569 views

How and why to use monadic programming in Mathematica?

This is a methodological question with two parts: How to use monadic programming in Mathematica? Why use monadic programming in Mathematica? In my opinion the questions are inter-related -- we ...
0
votes
0answers
28 views

Order of Map[] evaluation [duplicate]

Does f /@ list evaluate the f's in any particular order? For example, does it evaluate f on ...
3
votes
1answer
97 views

A partial outer product

Here is an example of what I want to do: Suppose I have a collection of samples S = {s1, s2, s3, ...}. I want to construct the correlation matrix (and yes, this ...