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

learn more… | top users | synonyms

1
vote
2answers
35 views

How to round all the numbers in a function?

I have a complicated function that is a combination of floating-point numbers and variables. I want all the numbers rounded off, but I have no clue how to do this. For example I would want ...
8
votes
1answer
144 views

How to construct the RHS of Wick's theorem

I've been trying this for a few days now, but I just can't seem to find an efficient (speed-wise) functional way to code the following task. Context: I would like to construct an expressions that is ...
10
votes
1answer
168 views
10
votes
0answers
287 views

Is there research about the Mathematica programming paradigm?

There seem to be lots of papers discussing the functional programming paradigm terminology for Haskell, such as monad, category theory, lambda calculus, *morphism, etc. People study concepts and ...
1
vote
0answers
46 views

Different ways to program a recursive algorithm

Construct f (a numeric function) which takes multiple arguments with the following property: The single argument case $f(x_1)$ is meaningless, and the two-argument ...
4
votes
3answers
175 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. ...
88
votes
8answers
5k views

General strategies to write big code in Mathematica?

I think after six months of exposure to Mathematica and the Wolfram Language I am fairly OK with writing short codes and snippets. However, what are some general strategies to use in order to write ...
1
vote
2answers
76 views

Partial Function Evaluation [closed]

How can I evaluate only part of a stack of function calls? In particular, given f[x_,y_,z_]=x+y+z;, say, I'd like to define a function ...
0
votes
0answers
52 views

Strange sharp spikes causing overflow while doing gradient descent

I am trying to find a function h(r) that minimises a functional H(h) by gradient descent. The result of H(h) is a single number. (Basically, I have a field configuration in space and I am trying to ...
3
votes
1answer
60 views

How to use currying to write a tail recursive function for summing factorials given a certain bound

I am very confused about how can i use currying to write a tail recursive function for summing factorials within a certain bound or summing the bounds i.e. given [1,5] i should be getting ...
2
votes
1answer
60 views

Pairing values with their indexes for MapThread

Several operations require you to map over a list of values and to know the index of each value as you process it. To accomplish this, my current somewhat ugly solution uses MapThread: ...
14
votes
6answers
823 views

What's the cleanest functional-programming style way to perform this list operation?

I am looking for the correct method for accomplishing the unknown piece in the following code snippet: rolls = Tuples[Range[1, 4], {2}] ...
3
votes
3answers
97 views

Counting runs in bits, along with their starts and lengths, in functional style

Here is some code to visualize Project Euler Problem 502. The code scans for runs of bits in the binary representation of an integer, and outputs the beginning position of each run and the length of ...
18
votes
4answers
435 views

How to keep some of the results of the NestList

I'm using some iterative arithmetics to calculate wave propagation with the help of NestList. I have to use a small step size for iteration to guarantee the accuracy, which lead to too much data (e.g, ...
4
votes
0answers
64 views

RealDigits and functional style

So while thinking about Replacing numbers of which you only know certain digits I have faced, again, a problem that I find hard to extract from RealDigits' output ...
1
vote
0answers
120 views

Variable naming conventions for nested pure functions [closed]

My understanding is that for nesting pure functions, one should use the explicit version of pure functions. I would like to know the best way of making it readable for people. I currently have ...
2
votes
1answer
47 views

Recognition Power Head for numeric value

We know: Map[Head, {a, a^2, a^3}] (* {Symbol, Power, Power} *) and Map[Head, {2, 2^2, 2^3}] all of them will be known as ...
3
votes
3answers
116 views

how to map a function on a variable list

The following is just an example that captures what I want to do (more on it at the bottom). I'm trying to do something that can be paraphrased as follows: I have a global list, say ...
10
votes
0answers
153 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 ...
3
votes
1answer
116 views

The basic primitive recursive functions

How can the basic primitive recursive functions be expressed in the Wolfram Language? Apparently here's an example for primitive recursion: ...
6
votes
1answer
141 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 ...
8
votes
7answers
1k views

Better code to find Narcissistic number

My code finding Narcissistic numbers is not that slow, but it's not in functional style and lacks flexibility: if $n \neq 7$, I have to rewrite my code. Could you give some good advice? ...
32
votes
4answers
2k views

Using pure functions in Table

I need a table with the elements made of pure functions and list elements. This is a simplified example: I need a list as: ...
6
votes
3answers
264 views

How to get “Signature” of each element of “Permutation” output

I need to sum over all possible permutations, multiplied by the Signature of each permutation of a given list of symbols. But, I don't know how to determine the ...
5
votes
1answer
241 views

MapAt and generic Functions on Associations and Datasets

These are distinct but related questions about mapping functions to Associations and Datasets. Given: assoc = <| a -> 1, b -> 2, c -> 3|> And ...
6
votes
4answers
925 views

How can I avoid using a For-loop in my code?

I am trying to avoid using For because I heard that For is not cool in functional programming. Below is a problem that the first ...
3
votes
0answers
84 views

Delimited Continuations: Easy? or Fundamentally Difficult?

Apologies in advance for the length of this question, but this is as short as I have been able to make it. The Prescription I'm looking at the following tutorial on delimited (or composable) ...
7
votes
3answers
90 views

How to organize expression by symbols (like Collect), but apply different functions to each coefficient

I want to mimic the functionality of Collect[expr, {vars}, func], but with the following modification: The function f that is ...
79
votes
8answers
3k 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 ...
10
votes
4answers
282 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 ...
3
votes
1answer
74 views

Defining a list of functions

I'm using polynomial mappings such as s[{x_,y_}] := {x + y^2, y -x} and playing with compositions of such mapping, e.g. ...
2
votes
2answers
98 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 ...
2
votes
2answers
44 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: ...
1
vote
1answer
96 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 ...
7
votes
1answer
202 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 ...
6
votes
1answer
461 views
1
vote
2answers
109 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? ...
7
votes
1answer
120 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: ...
4
votes
1answer
68 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 ...
4
votes
3answers
137 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 ...
11
votes
1answer
152 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 ...
1
vote
1answer
63 views

Resolving Error: Mathematica evaluation order on Lists

I have a table that is 27 x 27, each entry corresponds to an element from the periodic table. I use the replacement rules in element list to allow me to convert a list of elements into a list of ...
7
votes
3answers
191 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 ...
14
votes
1answer
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 ...
6
votes
1answer
127 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: ...
3
votes
1answer
98 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 ...
3
votes
1answer
404 views

NestList with a list inside?

I'm new to Mathematica, and was looking for more efficient ways to implement the Do loop below. a, ...