either_or
Implements a generic choice type named EitherOr
with variants Either and Or.
See also gleither.
pub type EitherOr(a, b) {
Either(a)
Or(b)
}
Installation
gleam add either_or@3
Example
Classify a list, then collect the values from each side:
import either_or
pub fn main() {
let values =
[-2, 3, -1, 4]
|> either_or.map_classify(fn(number) { number >= 0 })
either_or.partition(values)
// #([3, 4], [-2, -1])
}
Either contains values for which the predicate returned True; Or
contains the rest. partition preserves the original order of both groups.
What it provides
- Inspect and extract values with
is_either,is_or,get_either, andget_or. - Transform either or both sides with
map_either,map_or,map_eo, and their flat-map variants. - Collapse both variants into one type with
resolve_eoorresolve_oe. - Classify and separate collections with
map_classify,partition,keep_eithers, andkeep_ors. - Group consecutive values with
group_eithersorgroup_ors. - Convert to and from Gleam’s
Resulttype withto_resultandfrom_result.
See the API documentation for the complete reference.
Development
gleam test
License
Apache-2.0