FirstPosition¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
FirstPosition[expr, pattern]
gives the position of the first element in expr matching pattern (in depth-first order), or Missing["NotFound"] if no such element is found.
FirstPosition[expr, pattern, default]
gives default if no element matching pattern is found; default is evaluated only when it is returned.
FirstPosition[expr, pattern, default, levelspec]
finds only objects on the levels specified by levelspec.
Notes
Over an association, the position is a key, e.g. {Key\[k\], ...}. The default level specification is {0, Infinity} with Heads -\> True; a position of {} represents the whole of expr.Examples (9)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (3)¶
In[1]:= FirstPosition[{a, b, c, b}, b]
Out[1]= {2}
In[2]:= FirstPosition[{{1, 2}, {3, 4}}, 4]
Out[2]= {2, 2}
In[3]:= FirstPosition[{1, 2, 3}, 5, None]
Out[3]= None
Applications (6)¶
A nested position, depth first
The held default is returned when nothing matches
Over an association the position is a key
Restricted to level 1
Implementation notes¶
Algorithm. builtin_first_position (src/patterns.c) gives the position of
the first subexpression matching a pattern, in depth-first order. Rather than
re-implement traversal it delegates to Position with a first-match cap and
takes the first result, inheriting Position's levelspec parsing, Heads
handling, association value → Key[...] remapping, and depth-first ordering
unchanged. A visible NDArray first argument is an atom to the matcher, so
patterns_delist_visible materialises it before matching.
The handler splits the trailing Heads -> True|False option from the positional
arguments expr, pattern, default, levelspec. It then builds the delegated
call: for a 2-argument association it uses the 2-argument Position form (the
only one that does the value→Key remap); otherwise it passes the supplied
levelspec — or the {0, Infinity} default, which includes level 0 (the whole
expression, position {}) — plus the match count 1 so Position stops at the
first hit, forwarding any Heads option. If Position returns a non-empty list
the first element is the answer; otherwise the held default is returned
(evaluated by the fixed-point loop only when returned), falling back to
Missing["NotFound"].
Attributes & limits. FirstPosition carries HoldRest | Protected, which is
how default stays unevaluated until it is actually produced. It takes two to
four positional arguments.
- Attributes
{HoldRest, Protected};defaultis evaluated only when it is returned. - Same defaults as
Position: levels{0, Infinity}withHeads -> True; a position of{}is the whole ofexpr. - Over an association the position is a key, e.g.
{Key[k], ...}. - Delegates to
Position(with the first-match cap), so traversal, level specs, theHeadsoption, and association handling matchPositionexactly.
Attributes: HoldRest, Protected.
References¶
See also: Position
- Source:
src/patterns.c - Specification:
docs/spec/builtins/pattern-matching.md - Tests:
tests/test_assoc_atomicity.c - Tests:
tests/test_firstposition.c
Notes & additional examples¶
Notes¶
FirstPosition[expr, pattern] gives the position — a list of indices — of the
first subexpression matching pattern in depth-first order, or
Missing["NotFound"] if there is none. FirstPosition[expr, pattern, default]
returns default instead; default is held and evaluated only when it is
actually returned. A fourth argument is a level specification.
It delegates to Position (with the match count fixed at 1), so it inherits
Position's traversal, default level spec {0, Infinity}, Heads -> True
handling, and association value → Key[...] remapping exactly. Because the
default spec includes level 0, a pattern that matches the whole of expr gives
the position {}.