From c5845fa29b5059739b6f7a5b0c7da21e752de4fa Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Wed, 2 Sep 2020 22:49:29 +0100 Subject: list.partition --- src/gleam/list.gleam | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src') diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index d9296da..4ca39f4 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -1034,3 +1034,21 @@ pub fn each(list: List(a), f: fn(a) -> b) -> Nil { } } } + +pub fn partition( + list: List(a), + with categorise: fn(a) -> Bool, +) -> tuple(List(a), List(a)) { + do_partition(list, categorise, [], []) +} + +fn do_partition(list, categorise, trues, falses) { + case list { + [] -> tuple(reverse(trues), reverse(falses)) + [x, ..xs] -> + case categorise(x) { + True -> do_partition(xs, categorise, [x, ..trues], falses) + False -> do_partition(xs, categorise, trues, [x, ..falses]) + } + } +} -- cgit v1.2.3