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
0
votes
0answers
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 <...
8
votes
1answer
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 ...
6
votes
5answers
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,...
1
vote
0answers
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 ...
2
votes
2answers
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. ...
1
vote
1answer
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/ ...
1
vote
1answer
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 ...
11
votes
1answer
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 ...
12
votes
1answer
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 "...
29
votes
3answers
920 views

Why is Unevaluated[#]& different from Unevaluated?

...
2
votes
0answers
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 ...
6
votes
4answers
150 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
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'...
4
votes
1answer
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 ...
1
vote
1answer
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. ...
3
votes
0answers
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 ...
5
votes
2answers
94 views

Problems folding - resorting to hacks with `StringReplace`

I would like to FoldList a simple function, with desired output: ...
17
votes
4answers
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 ...
1
vote
2answers
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 ...
3
votes
0answers
131 views

How to ScanThread and ScanIndexed?

Why are there MapThread and MapIndexed but no analogous functions for Scan? What is the ...
6
votes
3answers
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: ...
1
vote
5answers
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 ...
2
votes
1answer
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 <...
3
votes
1answer
140 views

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

Suppose I have a net ...
4
votes
1answer
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 ...
4
votes
3answers
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 ...
26
votes
3answers
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 ...
0
votes
0answers
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 ...
3
votes
1answer
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 ...
3
votes
0answers
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 ...
0
votes
0answers
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) ...
0
votes
2answers
90 views

Function Problem

Hello, I was wondering how to solve this problem using Mathematica?
5
votes
3answers
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 ...
2
votes
0answers
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?
11
votes
2answers
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 ...
2
votes
1answer
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 ...
2
votes
3answers
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 ...
1
vote
1answer
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 ...
16
votes
5answers
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 ...
11
votes
2answers
451 views

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

Since this code work well: ...
11
votes
2answers
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 ...
1
vote
1answer
122 views

FreeQ with symbolic condition [closed]

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

Conditional definition (/;) [duplicate]

I defined the following functions ...
1
vote
2answers
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 ...
12
votes
2answers
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): ...
3
votes
2answers
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. <...
24
votes
4answers
972 views

Pseudo-currying in one line

Often when I'm writing OOP code using an object-manager association I find myself doing something akin to currying the arguments to some form of delegate object or head. (Building a one-argument ...
3
votes
1answer
65 views

Generating “descendants” of a list

The game of "Nim" is played using stones arranged into piles. Players take turns removing 1 or more stones from a single pile, the player who can't take any stones loses. I am interested in writing ...