Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

0
votes
1answer
46 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
46 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
52 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
104 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
1answer
41 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
224 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
62 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
76 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
94 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 ...
1
vote
2answers
48 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
57 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
49 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
518 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
51 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, ...
5
votes
1answer
90 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
49 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
285 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
228 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
92 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
75 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
83 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
267 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
46 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
242 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
56 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
45 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
99 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
63 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
68 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 <...
7
votes
1answer
158 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
206 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,...
0
votes
0answers
41 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
83 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
53 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
79 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 ...
10
votes
1answer
294 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 ...
10
votes
1answer
648 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
876 views
2
votes
0answers
28 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
148 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
61 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
61 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
88 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
94 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
93 views

Problems folding - resorting to hacks with `StringReplace`

I would like to FoldList a simple function, with desired output: ...
17
votes
4answers
814 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 ...