diff options
author | Marcin Puc <marcin.e.puc@gmail.com> | 2021-03-14 17:56:53 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-03-28 20:59:55 +0100 |
commit | b44af798940bd0b695d57b9b902b81b2b5309bd4 (patch) | |
tree | 603c4911bad9b9eb3324f1b61c181c333aafeb6a /src | |
parent | f57e7091f127101d065999834e44f3059e63b3ec (diff) | |
download | gleam_stdlib-b44af798940bd0b695d57b9b902b81b2b5309bd4.tar.gz gleam_stdlib-b44af798940bd0b695d57b9b902b81b2b5309bd4.zip |
Add iterator.{any, all}
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/iterator.gleam | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/gleam/iterator.gleam b/src/gleam/iterator.gleam index 7e767db..4ea19f6 100644 --- a/src/gleam/iterator.gleam +++ b/src/gleam/iterator.gleam @@ -807,3 +807,75 @@ pub fn intersperse( } |> Iterator } + +fn do_any( + continuation: fn() -> Action(element), + predicate: fn(element) -> Bool, +) -> Bool { + case continuation() { + Stop -> False + Continue(e, next) -> predicate(e) || do_any(next, predicate) + } +} + +/// Returns `True` if any element emitted by the iterator satisfies the given predicate, +/// `False` otherwise. +/// +/// This function short-circuits once it finds a satisfying element. +/// +/// An empty iterator results in `False`. +/// +/// ## Examples +/// +/// > from_list([]) |> any(fn(n) { n % 2 == 0 }) +/// False +/// +/// > from_list([1, 2, 5, 7, 9]) |> any(fn(n) { n % 2 == 0 }) +/// True +/// +/// > from_list([1, 3, 5, 7, 9]) |> any(fn(n) { n % 2 == 0 }) +/// False +/// +pub fn any( + in iterator: Iterator(element), + satisfying predicate: fn(element) -> Bool, +) -> Bool { + iterator.continuation + |> do_any(predicate) +} + +fn do_all( + continuation: fn() -> Action(element), + predicate: fn(element) -> Bool, +) -> Bool { + case continuation() { + Stop -> True + Continue(e, next) -> predicate(e) && do_all(next, predicate) + } +} + +/// Returns `True` if all elements emitted by the iterator satisfy the given predicate, +/// `False` otherwise. +/// +/// This function short-circuits once it finds a non-satisfying element. +/// +/// An empty iterator results in `True`. +/// +/// ## Examples +/// +/// > from_list([]) |> all(fn(n) { n % 2 == 0 }) +/// True +/// +/// > from_list([2, 4, 6, 8]) |> all(fn(n) { n % 2 == 0 }) +/// True +/// +/// > from_list([2, 4, 5, 8]) |> all(fn(n) { n % 2 == 0 }) +/// False +/// +pub fn all( + in iterator: Iterator(element), + satisfying predicate: fn(element) -> Bool, +) -> Bool { + iterator.continuation + |> do_all(predicate) +} |