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

28
votes
8answers
2k views

Can this be written well, without loops?

Inspired by this question I would like to know if the following code can be written without explicit loops (For, While, etc.) in a clean, efficient and non-contrived way. I have been unable to do so. ...
0
votes
0answers
59 views

Loop in a loop, or how to evaluate complex expressions

I need to find $h_{0j}$ for every $1\leq j\leq300$, where \begin{multline*} h_{0j} = \sum_{q=1}^{Q}g_qe^{-\lambda_q j\Delta}\frac{e^{\lambda_q\Delta}-1}{\lambda_q} + \sum_{p=1}^{P}\frac{e^{-\alpha_p ...
5
votes
3answers
103 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 ...
15
votes
2answers
998 views

Scan vs. Map vs. Apply

I cannot understand the difference between Apply, Scan, and Map. I have tried to play with ...
19
votes
1answer
437 views

Shorter syntax for Fold and FoldList?

I find it tedious to write out the following: a = {1, 2, 3, 4}; Fold[f, First @ a, Rest @ a] f[f[f[1, 2], 3], 4] It ...
0
votes
1answer
53 views

Returning functions that are partly evaluated

I have some performance issues when calling Table[{h, Slow1[h, n]}, {n, nlist}, {h, 0.1, 10, 0.5}]; for a given list ...
5
votes
0answers
78 views

How can I do a two dimensional `Fold`?

I'd like to Fold over higher dimensional objects than lists. Semantically, the format would be something like ...
0
votes
0answers
3 views

Show that A has an unique extension to a bounded operator on H [migrated]

if {e1 , e2 , ...} is an orthonormal basis for Hilbert space H and for each n there is a vector Ae_n in H such that Σ∥Ae_n∥ < ∞ .Show that A has an unique extension to a bounded operator on H.
0
votes
2answers
84 views

Is it possible to sum the results of a Map - Apply?

I am fairly new to functional programming and would like to replace a For[] loop that sums the results over a Dataset. This may be a bit different since the ...
7
votes
1answer
130 views

How would I use Map on a Dataset column?

I am somewhat new to functional programming. To improve the performance of a package, I would like to re-write a package in a functional style. I have the following ...
18
votes
4answers
402 views

Make MapIndexed listable

My goal is to apply MapIndexed to every element of a nested list without destroying the arithmetic operations within the elements. For example, let's start with ...
2
votes
3answers
76 views

find position of nearest nonzero element in an array (functionally)

Given an array sel and an index position i0, how can I find the position of the nearest (left or right) nonzero element? I'm ...
0
votes
1answer
219 views

mathematica apply list of functions to the same arguments [closed]

EDIT: "Through" is partial solution, because it cannot be used in expressions, where these functions are used without arguments. How can I apply list of functions to the same arguments? e.g. ...
10
votes
1answer
795 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 ...
8
votes
11answers
860 views

Elegant functional equivalent to a nested loop?

I have a square $n \times n$ matrix $m$, and need to apply a function $f$ to all elements on and above the diagonal. This is of course easy to do using a nested table: ...
19
votes
2answers
492 views

What is the difference between Composition (@*) and sequential applications (@)?

What is the difference between f@*g@*h@x and f@g@h@x Both evaluate to f[g[h[x]]] ...
23
votes
3answers
1k 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: ...
2
votes
3answers
143 views

How can I get from a list of characters representing a permutation to graphics showing the cycles?

I am trying to graphically represent cycles in various permutations of the alphabet, and have a question in two parts. I have ...
7
votes
5answers
175 views

Help going from iterative to functional

The following code segment shows what I'd like to do. I'm a procedural programmer trying to learn the Mathematica functional style. Any help on this would be appreciated. ...
1
vote
1answer
61 views

Applying functions repeatedly on parts of complicated expressions

I am dealing with expressions like $$ \textrm{expr}=5D_{4,1}\langle12\rangle\langle23\rangle\big(-\langle15\rangle^2\langle24\rangle\langle34\rangle + ...
6
votes
3answers
521 views

How do I convert an argument list to a list of arguments?

I think this is a basic question, but I am having difficulty finding the answer in the documentation. Thread is not what I am looking for, I think. Suppose that I ...
8
votes
4answers
326 views

How to evaluate a function on a list until stable?

I need to evaluate a function func on a range of integers (NOT recursively, just sequentially) until the result is stable enough. I can write a not very clever ...
0
votes
1answer
51 views

How to define the following bracket?

My quetion is about the definition of the so-called Courant Bracket. Given two generalised vector field, defined as a formal sum of a vector and a one-form, i.e. $X = x + \lambda,\ Y = y + \omega$, ...
3
votes
2answers
133 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. ...
5
votes
1answer
143 views

Functional-programming virus infection

Update: The question may seem too broad, but the answer provided by belisarius responds perfectly to the question, as the use of $ListConvolve$ and $NestList$ meets the desired criteria in in addition ...
1
vote
1answer
49 views

Let's do this list-rearrangement prettier [duplicate]

Suppose that i have a list in this format= $list=\{ \{a,obj1 \} ,\{c,obj2 \}, \{a,obj3 \}, \{b,obj4 \} \dots\}$ I want to construct a new list in this way: $ newlist= \{ \{ \{a,obj1\} , \{a,obj3\} ...
2
votes
3answers
183 views

How can I use NestWhileList instead of While in this case?

OK, the problem occurred when I challenged with Project Euler No. 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. My code is: ...
1
vote
1answer
46 views

Function taking arbitrary number of arguments

I have a very similar function to that which a solution was provided by kguler, but am unsure as to how to adapt the solution that was given there. That post can be seen here. The adaptation is only ...
3
votes
1answer
97 views

Listable on first level [duplicate]

I have some function which take List as input. f[l_List]:=someActions[l] I need apply this function to a list of lists. Now I ...
11
votes
2answers
253 views

Choose between `Apply` and `Map`

Even though this might be too general question(or not answerable), I want to get some advice here. I apologize in advance if it is not a relevant question. I frequently have to process large list of ...
4
votes
4answers
210 views

Iterating over a list, in a functional way

I have learned a lot about functional programming just from messing around in Mathematica, but I've come across a problem that I don't know how to solve in a "functional" way. Any help would be ...
3
votes
1answer
85 views

Combining MapAt and MapIndexed

The very last example in the documentation for MapAt is: MapAt applies f repeatedly if a part is mentioned repeatedly: ...
1
vote
1answer
89 views

The problem (time) caused by ?NumericQ

In here, I asked a related (similar) question. Using ?NumericQ seems have slowed down the entire calculation. But I now have a more serious problem: ...
0
votes
2answers
60 views

Supply a function as an argument to another, and find its minimum

I would like to minimize a function, which is supplied as an argument to another function, and report back the estimates of the values of the variables that result in its minimum. In specific, I would ...
18
votes
3answers
735 views

Can someone explain this snippet: (#[#] &)[#[#][#] &]

A friend showed me this snippet of code today: (#[#] &)[#[#][#] &] I can sort of see that we're constructing an anonymous function that takes a function ...
0
votes
1answer
84 views

How to improve this function that selects parameters from arbitrary distribution using Recursion?

Below, I have defined a function using Which to select model parameters based upon various conditions. My questions are: Is there any danger in writing this function using Recursion? Is there a more ...
1
vote
1answer
143 views

General function taking general number of arguments

The following is an edit of this post, for which a working solution was provided by John McGee. I have a different function and am not sure how to adapt the solution for it: I want to construct a ...
7
votes
3answers
341 views

Functional equivalent of a For loop referring to consecutive elements in a list

I have implemented this function which smooths the values in a list by, if it encounters a zero, replacing this entry by the average of its two neighbours (apart from the first and last values of the ...
27
votes
1answer
639 views

Prefix operator with low precedence

The question is simple, but I will elaborate on the background as well for those interested in the idea: How to define a new operator with specified precedence value? Background Mathematica was ...
3
votes
1answer
143 views

Functional programming approach to avoid traditional loops

I'm currently learning from the wonderful post: Where can I find examples of good Mathematica programming practice? And i'm correcting my habits using Nest/Map/Thread instead of loops. The problem ...
3
votes
3answers
263 views

fastest way to perform the following summation

I have a vector $A$ with components $A_{i}$, and two matrices $X$ and $Y$ with components $X_{ij}$, and $Y_{ij}$ respectively. What is the fastest way of performing the following summation ...
0
votes
2answers
52 views

How to continuous apply a function to an argument? [duplicate]

In order to calculate x = sqrt(1 + x) for a given number x, 40 times, I tried this: ...
4
votes
3answers
163 views

Selective threading of the Rule

How can I change form1 to form2 form1 {10000, 10004} -> {2, 2, 637} form2 ...
5
votes
3answers
276 views

Multiplying block matrices

I have a 2*8 matrix A, and a 2*8*2 matrix B. So B[[1]] and B[[2]] are both 8*2 matrices. I need a neat way to multiply A by B so that the first list in the result is A.B[[1]] and the second list ...
1
vote
0answers
71 views
1
vote
1answer
213 views

How can I call the R-programming packages in my Mathematica?

I am trying to call the R-programming package called highfrequency in my Mathematica so that I can use its function rCov. I ...
5
votes
1answer
430 views

Simple while loop alternative in Mathematica

I have noticed that there are a few examples that go into some detail regarding While loop alternatives. (See here for example: Alternatives to While Loops?) However, I wanted to provide a simpler ...
1
vote
0answers
72 views

Updating procedural fitting algorithm to more efficient style?

For some time now, I have used this procedural programming approach to fit data in my research that is too cumbersome for the built in Mathematica functions. (Although this simple example works fine ...
12
votes
3answers
716 views

What is the most efficient way to map a linked list?

In order to work with lists effectively using common recursive patterns from functional programming, it's often most convenient to worked with the "linked list" representation, that looks like this: ...
1
vote
3answers
86 views

How to distribute but skipping one level

I would like to write a Rule that mimics built-in Distribute[], but one that distributes ...