Skip to main content
31 votes
Accepted

What makes mathematica a "functional programming language"?

On the Wolfram web site there is a description of the Wolfram Language (WL) entitled Notes for Programming Language Experts. At time of writing, it lists 47 attributes of WL including Functional. It ...
WReach's user avatar
  • 69.1k
29 votes

Analogies to Help New Users

Imho some important things to translate between Matlab and Mathematica: "everything is a matrix (or inefficient)" vs. "everything is an expression" indexing into arrays: ...
Henrik Schumacher's user avatar
25 votes

Why is Unevaluated[#]& different from Unevaluated?

I thought that for any function f we could use "f" interchangeably with "f[#]&". That ...
Szabolcs's user avatar
  • 236k
24 votes

Cleanest way to take a[b[c]] to a[b][c]

test = a[b[c[d]]]; Fold[ Construct, (* or Compose, see [1] *) Level[test, {-1}, Heads -> True] ] a[b][c][d] [1] - ...
Kuba's user avatar
  • 137k
23 votes
Accepted

Why use pure functions?

Think of it as a macro. It shortens up your code. It stops your namespace from being polluted with extra function definitions. In some cases it can be faster than defining an auxiliary function. I ...
Greg Hurst's user avatar
  • 36.3k
23 votes
Accepted

Iterate over # without needing to define a variable

f = Thread @* List; f[n0, {n1, n2, n3, n4, n5}] {{n0, n1}, {n0, n2}, {n0, n3}, {n0, n4}, {n0, n5}} If you have to use ...
kglr's user avatar
  • 397k
23 votes
Accepted

What is the fastest method to set the diagonals of a matrix to 1?

Edit A "one-line" way without any C source code: ...
Silvia's user avatar
  • 27.6k
22 votes

How to map and sum a list fast?

AbsoluteTiming[Total[Range[1, 100000000]^2]] (* {26.0686, 333333338333333350000000} *) or ...
BlacKow's user avatar
  • 6,438
21 votes

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

Just write the problem literally and use Reduce ...
mikado's user avatar
  • 16.8k
20 votes
Accepted

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

We can do this more efficiently using IntegerPartitions: Counts /@ IntegerPartitions[20, {20}, {1, 3, 4}/2] ...
Mr.Wizard's user avatar
  • 272k
19 votes
Accepted

Pseudo-currying in one line

EDIT: Modified to cover situation when an argument is a List Use Fold ...
Bob Hanlon's user avatar
  • 160k
18 votes

Monad-like structures in Mathematica

General My (biased) view on monads is that they are loudly advertised in Haskell and F#, but I kind of do not see what is the big deal. (Granted, similar things are said about OOP Design Patterns, ...
Anton Antonov's user avatar
18 votes

Pseudo-currying in one line

The deprecated (but valid) function HeadCompose basically does just that: ...
Mr.Wizard's user avatar
  • 272k
18 votes

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

Another solution: Select[FrobeniusSolve[{20, 15, 5}, 200], Total[#] == 20 &] ...
C. E.'s user avatar
  • 70.8k
18 votes
Accepted

Is there a way to do this with Mathematica shorthand notation?

You could use the operator form of Map instead: SecondFn @ Map[FirstFn] @ {2, 3, 5} SecondFn[{FirstFn[2], FirstFn[3], FirstFn[5]}] or ...
Carl Woll's user avatar
  • 131k
18 votes
Accepted

Why is Unevaluated[#]& different from Unevaluated?

The two are not equivalent. Even if f lacks special attributes. Assuming no special attributes are present, a function's arguments are always evaluated before the ...
user541686's user avatar
  • 1,317
18 votes

Function that does for Map what Through does for Apply?

One option is to use the forking operator {fn1, fn2, ...} from the Query sublanguage: ...
WReach's user avatar
  • 69.1k
18 votes

What is the fastest method to set the diagonals of a matrix to 1?

A long comment...I'm using V13.1, Mac M1 Pro, which might matter... First, I would point out the "About" section of the profile of user @WReach. Toward the end, the question "Why don't ...
Michael E2's user avatar
  • 237k
18 votes

What is the fastest method to set the diagonals of a matrix to 1?

It is pretty fast to use LibraryLink, making sure the memory is shared between the library and the Mathematica kernel. Note that you need a C compiler. C code: ...
Jacob Akkerboom's user avatar
16 votes
Accepted

How best to simulate n-body systems in a functional way?

In version 12.0 you can use NBodySimulation for that: ...
user21's user avatar
  • 40k
16 votes

Why is Unevaluated[#]& different from Unevaluated?

The head Unevaluated is a symbol with the HoldAllComplete attribute. The head ...
John Doty's user avatar
  • 13.8k
16 votes
Accepted

Meaning of ## #

Plus[## #] & is the same as Plus[Times[##, #1]], which always computes to the same as the simpler ...
Szabolcs's user avatar
  • 236k
15 votes

Iterate over # without needing to define a variable

Best on packed arrays... ...
Michael E2's user avatar
  • 237k
14 votes

Functional style using lazy lists?

I recently published a lazy list package on GitHub, so I might as well show it off here: https://github.com/ssmit1986/lazyLists Installation instructions can be found on the GitHub landing page ...
Sjoerd Smit's user avatar
  • 23.8k
13 votes
Accepted

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

This one constructs an AdjacencyMatrix that can be input to AdjacencyGraph: ...
kglr's user avatar
  • 397k
13 votes
Accepted

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

New analysis (in v10.1) For your newly stated goal: If I don't want to build a new function.I just wanna change the GeneralUtilities`BenchmarkPlot's default ...
Mr.Wizard's user avatar
  • 272k
13 votes

Is there a way to do this with Mathematica shorthand notation?

FirstFn /@ a // SecondFn SecondFn[{FirstFn[2], FirstFn[8], FirstFn[5]}]
Chris Degnen's user avatar
13 votes
Accepted

Map a two argument function to every element on list

Since you're looking to learn more about functional style, I'll elaborate rather than just provide an answer. We start with our table: ...
lericr's user avatar
  • 30.1k
12 votes

How to map and sum a list fast?

To overcome the memory issues, let's store as few data in the memory as possible: out = 0; Do[out = out + i^2, {i, 1, 100000000}]; // AbsoluteTiming out {58....
corey979's user avatar
  • 24k
12 votes
Accepted

Nested apply function at a list

The function you want to use is Fold which does almost what you want to do i.e. Fold[f,{a,b,c}] Out[] := f[f[f[a, b], c], d] ...
AndreasP's user avatar
  • 598

Only top scored, non community-wiki answers of a minimum length are eligible