Extract¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Extract[expr, pos]
extracts the part of expr at the position specified by pos.
Extract[expr, {pos1, pos2, ...}]
extracts a list of parts of expr.
Extract[expr, pos, h]
extracts parts of expr, wrapping each of them with head h before evaluation.
Extract[pos]
represents an operator form of Extract that can be applied to an expression.
Notes
The position pos has the same form as a Position result: a list of indices {i1, i2, ...} that descends i1 into expr, then i2, etc. A scalar index n is treated as the path {n}, so Extract\[expr, n\] is equivalent to Extract\[expr, {n}\]; in particular Extract\[expr, 0\] gives Head\[expr\]. Indices are 1-based and may be negative; index 0 selects the head. Extract is treated as atomic on Integer, Real, String, Symbol, Rational\[n, d\], and Complex\[re, im\].Examples (5)¶
Every input below was run against the current Mathilda build and its output recorded.
Applications (5)¶
In[1]:= Extract[{a,b,c},2]
Out[1]= b
In[2]:= Extract[{{a,b},{c,d}},{2,1}]
Out[2]= c
In[3]:= Extract[{{1,2},{3,4}},{{1,1},{2,2}}]
Out[3]= {1, 4}
In[4]:= Extract[a+b+c, 0]
Out[4]= Plus
In[5]:= Extract[x^4 + 2 x^2 + 1, Position[x^4 + 2 x^2 + 1, x]]
Out[5]= {x, x}
Implementation notes¶
builtin_extract (in src/part.c) reads Extract[expr, pos] (optionally with a held wrapper head as a third argument). If pos is a list-of-positions (a List whose elements are themselves Lists), it extracts each position via the helper extract_single and returns the results in a List; otherwise it treats pos as a single position path. The 1-arg operator form returns a Function[Extract[#, pos]] closure.
- Position specifications have the same form as those returned by
Position. Extract[expr, {i, j, ...}]is equivalent toPart[expr, i, j, ...].poscan be of the more general form{part1, part2, ...}wherepartiarePartspecifications such as an integeri,AllorSpan.- You can use
Extract[expr, ..., Hold]to extract parts without evaluation. - Reads a packed list through the buffer, the same gather
Partuses, so pulling three elements out of a 10^6-element array costs three reads rather than a materialisation of the whole array. An element from an integer buffer comes back as an exactInteger.Extract[expr, {0}]is head extraction and is unaffected.
Attributes: NHoldRest, Protected.
References¶
See also: Position, Part, Span
- Source:
src/part.c - Specification:
docs/spec/builtins/structural-manipulation.md - Tests:
tests/test_association.c - Tests:
tests/test_mapindexed.c - Tests:
tests/test_packed_list.c - Tests:
tests/test_part.c
Notes & additional examples¶
Notes¶
A single integer extracts one element; a position list like {2,1} extracts a nested part. A list of positions returns the list of extracted values. Index 0 selects the head, so Extract[expr, 0] returns Head[expr]. Because the position syntax matches the output of Position, the two compose directly: feeding Position[expr, x] back into Extract pulls out every occurrence of x.