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.

Filter by
Sorted by
Tagged with
1
vote
1answer
35 views

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

I have a list with the following structure: ...
2
votes
2answers
90 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 ...
1
vote
0answers
61 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. <...
1
vote
2answers
66 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 ...
5
votes
1answer
116 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 ...
5
votes
1answer
273 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 <...
2
votes
2answers
44 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 ...
4
votes
5answers
155 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 ...
3
votes
1answer
78 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 ...
3
votes
1answer
39 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 ...
2
votes
1answer
36 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] ...
0
votes
1answer
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 ...
3
votes
2answers
165 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 ...
0
votes
0answers
50 views

How to handle exceptions and failures in Mathematica?

In python I can write fallback values like: ...
5
votes
0answers
77 views

Hold attributes in functional programming

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 ...
3
votes
2answers
105 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 ...
0
votes
1answer
62 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: ...
3
votes
1answer
52 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 ...
4
votes
2answers
67 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 ...
18
votes
4answers
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 ...
3
votes
1answer
137 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.
3
votes
0answers
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 ...
6
votes
4answers
252 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 ...
1
vote
1answer
68 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 ...
1
vote
0answers
107 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 ...
1
vote
1answer
99 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 ...
16
votes
1answer
882 views

Meaning of ## #

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

Switch applying tests to forms

Is it correct that the Select equivalent of Cases is missing for Switch? I mean a function <...
2
votes
1answer
76 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 ...
1
vote
1answer
59 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 ...
29
votes
3answers
569 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
58 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, ...
6
votes
2answers
628 views

How can I find a more efficient solution to mapping characters to digits?

Given: ...
5
votes
1answer
103 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
50 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
295 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
238 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 ...
3
votes
0answers
119 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
80 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
85 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
276 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
63 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
249 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
62 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
46 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
104 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
67 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
74 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 <...
8
votes
1answer
174 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
213 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,...