# 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
75 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 <...
176 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 ...
215 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,...
47 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 ...
85 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. ...
66 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/ ...
112 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 ...
353 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 ...
955 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 "...
920 views

...
30 views

### Scoping of OptionValue for inner functions in closures

Assume a function f is a closure over an inner function g. Both f and ...
150 views

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. ...
50 views

...
65 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'...
178 views

...
66 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 ...
94 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. ...
98 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 ...
94 views

### Problems folding - resorting to hacks with StringReplace

I would like to FoldList a simple function, with desired output: ...
833 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 ...
164 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 ...
131 views

### How to ScanThread and ScanIndexed?

Why are there MapThread and MapIndexed but no analogous functions for Scan? What is the ...
218 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: ...
154 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 ...
78 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 <...
140 views

### How to use history as one of the input in neural networks?

Suppose I have a net ...
122 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 ...
147 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 ...
748 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 ...
29 views

### Order of Map[] evaluation [duplicate]

Does f /@ list evaluate the f's in any particular order? For example, does it evaluate f on ...
102 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 ...
318 views

### Epsilon algorithm to accelerate sum's convergence in a functional approach

I am trying to learn to use Mathematica in an efficient way. Thus, I decided to spend some time on functional programming. I would like to implement the so-called epsilon algorithm, that is used to ...
156 views

### A Table of Sums, but faster

I'm trying to calculate the following lists a_j and b_j from some predefined values. (this is not code, it's just supposed to be a mathematical expression) ...
90 views

### Function Problem

Hello, I was wondering how to solve this problem using Mathematica?
123 views

### Create pattern to “redo” Nest for symbolic purposes, e.g. for commutators

I am symbolically working with matrix commutators (comm) and face the following issue: There are nested commutators in my final results, defined as ...
48 views

### Folow-up on combining pure functions [duplicate]

This is a follow-up to this question. Consider: f = #^2& g = #^3& Compose[f, g] (#1^3 &)^2 Am I the only one to think this is a bug?
474 views

### Combining pure functions

Is there an easy way to combine pure functions into a single pure function? For example, say I have f = #1^2 &; g = #1 - 2 &; and I want to define a new ...
109 views

### How to map a function to all sub-parts of an expression, except sub-parts passing some test

I'm constructing a function mapAllExcept which is similar to MapAll: It maps a function f ...
182 views

### How to find all prime power factorizations of an integer

I want to put in $n$ and get the possible factorizations into prime powers. Let's say for 24 I want {{8,3}, {4,2,3}, {2,2,2,3}}. The algorithm for doing this on ...
84 views

### Jug pouring puzzle in Mathematica

Using two unmarked 5 and 3 gallon jugs, obtain exactly 1 gallons in one of these jugs by filling, pouring, and emptying operations. Assume there is an endless supply of water. My approach: I use the ...
2k views

### Puzzle — 20 People to consume 20 units of food under constraints

I am learning Mathematica because I love it. I also love solving puzzles so I think it would be a nice way to learn Mathematica through puzzles. This is first puzzle in series I intend to solve. So ...
451 views

### How to build private function with built-in function's option

Since this code work well: ...
238 views

### Simplifying a reverse Fold for a Query

Is there a simpler way to parametrize this tail-recursive expression to be applied as a Query (for processing a nested ...
122 views

### FreeQ with symbolic condition [closed]

I'm having a problem: FreeQ[a, _?(# < 0 &)] returns True when a is kept symbolic. ...
129 views

### Conditional definition (/;) [duplicate]

I defined the following functions ...
175 views

### Alternative to FreeQ as functional programming

I wrote a code like this ConstQ[expr_]:=FreeQ[expr,a|c] in order to verify if some expression contains a or c. Let's call these a end c variable to avoid. I ...
442 views

### Function to draw densely interconnected layers in graph (e.g., neural net)

I can use Graph[] to manually draw the shape of a very simple set of interconnected layers (for instance, a small neural network): ...
145 views

### Replacing procedural code with functional code

I have the following code that determines when the second business day of each month is (given a start and end date). I have a few If statements I would like to replace with functional programming. <...