SelectFirst
SelectFirst[{e1,e2,…},crit]
gives the first ei for which crit[ei] is True, or Missing["NotFound"] if none is found.
SelectFirst[{e1,e2,…},crit,default]
gives default if there is no ei such that crit[ei] is True.
SelectFirst[crit]
represents an operator form of SelectFirst that can be applied to an expression.
Details
- In SelectFirst[{e1,e2,…},crit,default], default is only evaluated if there is no ei such that crit[ei] is True.
- SelectFirst[crit][expr] is equivalent to SelectFirst[expr,crit].
Examples
open allclose allBasic Examples (4)
Select the first element that is even:
Use a pure function to test elements:
Give the default x when no even numbers are found:
SelectFirst operates on values in an Association:
Scope (3)
Generalizations & Extensions (2)
Properties & Relations (4)
The default value is only evaluated if needed:
SelectFirst returns only the first match:
Select returns all matches:
SelectFirst can be implemented with a combination of Select and Replace:
SelectFirst is similar to FirstCase except that it uses a function instead of a pattern:
Select the first list that has a sum of elements less than 10:
Use FirstCase to get the same result:
Text
Wolfram Research (2014), SelectFirst, Wolfram Language function, https://reference.wolfram.com/language/ref/SelectFirst.html.
CMS
Wolfram Language. 2014. "SelectFirst." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SelectFirst.html.
APA
Wolfram Language. (2014). SelectFirst. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SelectFirst.html