Syntax¶
Postfix application (//)¶
expr // f is equivalent to f[expr].
The // operator has the lowest precedence, so it applies to the entire
left-hand expression.
Because it really is f[expr], a Sequence on the left spreads into f's
arguments rather than arriving as one:
A Sequence one level down has already been spread by the head holding it,
so f still sees a single argument:
Postfix after an operator chain:
Postfix with a curried function call:
Chained postfix:
Prefix application (@) with comparison operators¶
@ has higher precedence than ==, so f@x == y means (f@x) == y.
Apply (@@) and Map (/@) inside an implicit product¶
@@, @@@ and /@ bind tighter than multiplication, so in a b @@ c the
operator reaches only the factor next to it — whether the leading factor is a
number or a function call:
Rule (->) and RuleDelayed (:>) as general operators¶
-> creates a rule, and :> creates a delayed rule.
These can be used anywhere in expressions, not just in replacement contexts.
Multi-index Part extraction ([[i, j]])¶
expr[[i, j]] extracts nested parts: first part i, then part j from the result.
$ wo 'FullForm[a[[1,2,3]]]'
Part::partd: Part specification a[[1,2,3]] is longer than depth of object.
.* (regex*)
FullForm[a[[1,2,3]]]
Spaces inside the Part brackets (x[ [i] ])¶
The two brackets of [[…]] are separate tokens, so whitespace, newlines
and comments may sit between them — a lone […] can never start an
expression, so x[ [i] ] is unambiguously Part[x, i].
Part of an operator expression ((a /. b)[[i]])¶
[[…]] binds tighter than every infix operator, so a parenthesized
operator expression keeps its parentheses when echoed — dropping them
would change what re-parsing produces.
Increment (++) and Decrement (--)¶
x++ increments x by 1 and returns the old value.
x-- decrements x by 1 and returns the old value.
Extraction and application in a chain (a["k"][[1]]["j"])¶
[[...]] and [...] alternate freely, so a nested association is walked in
one expression.
The same holds for a slot, which is what sorting associations by a keyed field relies on.
$ wo 'SortBy[{<|"pos" -> {3, 1}|>, <|"pos" -> {1, 2}|>}, #["pos"][[1]] &]'
{<|pos -> {1, 2}|>, <|pos -> {3, 1}|>}
A statement sequence closed by &¶
A & with no statement between it and the preceding ; turns everything
before it into the body of a pure function, and that function takes the
usual continuation — stmt; & /@ list is (stmt;) & /@ list.
Part Assignment¶
Assigning to a Part expression modifies the list in-place.
Rule Pattern Evaluation¶
Both sides of -> (Rule) are evaluated.
Stored Anonymous Functions¶
Anonymous functions can be stored in variables and called later.
Nested Function Slot Scoping¶
Slots (#) bind to the innermost & (Function).
Head-Constrained Patterns (_Head)¶
A blank can be restricted to a head, with or without a name. Each head is a separate definition, so the argument's head selects which one fires.
Alternatives (|)¶
The | operator represents alternatives in pattern matching.
Optional Pattern Defaults (:)¶
Writing pattern : value gives an argument a default, so the argument may
be left out. The short form names a blank (x_ : 2):
The default may be written on any pattern, not just a named blank. Naming
the pattern first (x : … : 2) makes the colon close the whole named
pattern, so a pattern test or an alternation gets the default:
Without that leading name the colon binds tightly to the term written in
front of it, which is rarely what you want: in _Symbol | _Integer : 2 the
default lands on _Integer alone, and an Optional buried inside an
alternation makes no argument optional:
After a ? the colon is read as part of the test, so _?NumericQ : 2 is
PatternTest[_, Pattern[NumericQ, 2]] and matches nothing:
The default value is taken as written — it is not checked against the pattern it stands in for:
Comments ((* … *))¶
A comment is skipped wherever whitespace is, and what it holds is never read as code — not even a bracket, a quote or a semicolon left over from commented-out code:
Comments nest, so a commented-out block that itself holds comments is skipped whole: