findSingle
lib.findSingle
Docs pulled from | This Revision | 44 minutes ago
Find the sole element in the list matching the specified predicate.
Returns default if no such element exists, or
multiple if there are multiple matching elements.
Inputs
pred-
Predicate
default-
Default value to return if element was not found.
multiple-
Default value to return if more than one element was found
list-
Input list
Type
findSingle :: (a -> bool) -> a -> a -> [a] -> a
Examples
lib.lists.findSingle usage example
findSingle (x: x == 3) "none" "multiple" [ 1 3 3 ]
=> "multiple"
findSingle (x: x == 3) "none" "multiple" [ 1 3 ]
=> 3
findSingle (x: x == 3) "none" "multiple" [ 1 9 ]
=> "none"
Noogle detected
Implementation
The following is the current implementation of this function.
findSingle =
pred: default: multiple: list:
let
found = filter pred list;
len = length found;
in
if len == 0 then
default
else if len != 1 then
multiple
else
head found;