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
29 views

Applying functions to members of members… of lists of lists…, etc

Suppose that I have a list of depth $k$, and I want to apply a function $f$ to all elements at level $n$ - e.g. $f,\{\{\{a,b\},\{b,c\}\},\{\{c,d\}\}\},3\mapsto\{\{\{f(a),f(b)\},\{f(b),f(c)\}\},\{\{f(c)...
5
votes
1answer
268 views

An efficient way to evaluate this expression with deeply nested Do loops

Is there an efficient way to evaluate something like this? ...
6
votes
2answers
352 views

I'm not able to delete -> in {{x -> a}, {x -> b}, {x -> c, x -> d}}

Here is what I have done. Input DeleteCases[{{x -> a}, {x -> b}, {x -> c, x -> d}}, Rule, {2}] Output ...
4
votes
1answer
65 views

Output of Two Functions as a Single Function

I have two functions: One of which outputs a number and the other outputs a picture. What I would like to do is create a single function that would output both the number and the picture. As a MWE ...
4
votes
4answers
215 views

How to Force Evaluation of Derivative in a Pure Function Definition

Let's say I have a function f1 = #^2 &; I want to calculate its derivative, perform some manipulations and make the result a pure function. I try ...
3
votes
1answer
40 views

Indexed KeyValueMap

I have been trying to figure it out and I am probably very close, but I have spent 2 hours without sucess. I have the expression below ...
2
votes
1answer
58 views

replacing variables inside an If statement

ClearAll[x, xx, z] xx = Array[x, 2]; y = If[z == 0, 0, xx[[2]]] gives for y: If[z == 0, 0, xx[[2]]] I want to see ...
1
vote
1answer
39 views

How to change list type from CountryData to regular text? [closed]

When you access a list of countries from CountryData the outputs are boxed and is not normal text. How do I convert the type of items inside the list so I can modify the style? For example I have: <...
1
vote
1answer
44 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]] ...
9
votes
2answers
375 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 ...
2
votes
1answer
101 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: ...
0
votes
0answers
20 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. ...
0
votes
0answers
28 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 ...
5
votes
2answers
88 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 ...
1
vote
1answer
40 views

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

I have a list with the following structure: ...
2
votes
2answers
102 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
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. <...
1
vote
2answers
73 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
122 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 ...
9
votes
4answers
350 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
58 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
166 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
92 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
44 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
38 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
170 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
65 views

How to handle exceptions and failures in Mathematica?

In python I can write fallback values like: ...
15
votes
2answers
285 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 ...
3
votes
2answers
136 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
119 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
54 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 ...
5
votes
2answers
103 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
209 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
263 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
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 ...
1
vote
0answers
114 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
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 ...
16
votes
1answer
889 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
62 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
106 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
85 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
611 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
64 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
631 views

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

Given: ...
5
votes
1answer
110 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
51 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
303 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, ...

1 2 3 4 5 8