Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Functions

Nested Function Application

$ wo 'Plus[Divide[6, 2], Abs[-5]]'
8

Anonymous Identity Function

$ wo '#&[1]'
1

Function Application

$ wo '#^2 &[{1, 2, 3}]'
{1, 4, 9}

/@ (Map)

Apply a function to each element of a list.

$ wo 'Sign /@ {7, -2, 0, -5}'
{1, -1, 0, -1}
$ wo '#^2& /@ {1, 2, 3}'
{1, 4, 9}
$ wo 'Sin@(Pi/2)'
1
$ wo '(Pi/2) // Sin'
1

Define And Use A Function

$ wo 'Double[x_] := x * 2; Double[5]'
10
$ wo 'Double[x_] := x * 2; Double[Sin[Pi/2]]'
2
$ wo 'Double[x_] := x * 2; Double @ Sin @ (Pi/2)'
2
$ wo 'Double[x_] := x * 2; (Pi/2) // Sin // Double'
2

Apply (@@)

Replaces the head of an expression with a function.

$ wo 'f @@ {1, 2, 3}'
f[1, 2, 3]

Fold

Applies a function cumulatively to elements of a list, starting with an initial value.

$ wo 'Fold[Plus, 0, {1, 2, 3}]'
6

FoldList

Like Fold, but returns a list of intermediate results.

$ wo 'FoldList[Plus, 0, {1, 2, 3}]'
{0, 1, 3, 6}

Nest

Applies a function repeatedly to an expression.

$ wo 'Nest[f, x, 3]'
f[f[f[x]]]

NestList

Like Nest, but returns a list of intermediate results.

$ wo 'NestList[f, x, 3]'
{x, f[x], f[f[x]], f[f[f[x]]]}

NestWhile

Applies a function repeatedly while a test returns True.

$ wo 'NestWhile[# + 1 &, 0, # < 5 &]'
5
$ wo 'NestWhile[# / 2 &, 64, EvenQ]'
1

NestWhileList

Like NestWhile, but returns a list of all intermediate results.

$ wo 'NestWhileList[# + 1 &, 0, # < 5 &]'
{0, 1, 2, 3, 4, 5}
$ wo 'NestWhileList[# / 2 &, 64, EvenQ]'
{64, 32, 16, 8, 4, 2, 1}

Through

Applies a list of functions to an argument.

$ wo 'Through[{Sin, Cos}, 0]'
{Sin, Cos}
$ wo 'Through[{Abs, Sign}, -5]'
{Abs, Sign}

TakeLargest

Returns the n largest elements from a list.

$ wo 'TakeLargest[{3, 1, 4, 1, 5, 9, 2, 6}, 3]'
{9, 6, 5}
$ wo 'TakeLargest[{5, 2, 8, 1}, 2]'
{8, 5}

TakeSmallest

Returns the n smallest elements from a list.

$ wo 'TakeSmallest[{3, 1, 4, 1, 5, 9, 2, 6}, 3]'
{1, 1, 2}
$ wo 'TakeSmallest[{5, 2, 8, 1}, 2]'
{1, 2}

ArrayDepth

Returns the depth of a nested list.

$ wo 'ArrayDepth[{1, 2, 3}]'
1
$ wo 'ArrayDepth[{{1, 2}, {3, 4}}]'
2
$ wo 'ArrayDepth[{{{1}}}]'
3
$ wo 'ArrayDepth[5]'
0

Run

Executes a system command and returns the exit code.

$ wo 'Run["echo hello"]'
hello
0
$ wo 'Run["exit 0"]'
0
$ wo 'Run["exit 1"]'
256

Pattern Definitions with SetDelayed

Define a function using pattern matching.

$ wo 'f[x_] := x^2; f[3]'
9
$ wo 'f[x_] := x^2; f[5]'
25
$ wo 'f[x_] := x + 1; f[10]'
11

Optional Pattern Arguments

Optional arguments use the x_:default syntax to provide default values.

$ wo 'f[x_:0] := x + 1; f[]'
1
$ wo 'f[x_:0] := x + 1; f[5]'
6

Optional with head constraint

$ wo 'g[q_List: {}, n_] := {q, n}; g[5]'
{{}, 5}
$ wo 'g[q_List: {}, n_] := {q, n}; g[{1, 2}, 3]'
{{1, 2}, 3}

Multiple optional arguments

$ wo 'f[a_:0, b_, c_:99] := {a, b, c}; f[5]'
{0, 5, 99}
$ wo 'f[a_:0, b_, c_:99] := {a, b, c}; f[1, 5]'
{1, 5, 99}
$ wo 'f[a_:0, b_, c_:99] := {a, b, c}; f[1, 5, 10]'
{1, 5, 10}

With

With substitutes constant values into the body expression.

$ wo 'With[{x = 5}, x + 1]'
6
$ wo 'With[{x = 2, y = 3}, x + y]'
5
$ wo 'With[{l = Length[{1,2,3}]}, l + 1]'
4

DateString

$ wo 'StringStartsQ[DateString[Now, "ISODateTime"], "2026-"]'
True