From 26271b6c8165854ddc2d959c7eaf070a6c14da1f Mon Sep 17 00:00:00 2001 From: Giacomo Cavalieri Date: Sat, 24 Jun 2023 11:18:29 +0200 Subject: Add `list.map3` --- src/gleam/list.gleam | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src') diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index b1acb74..dce703b 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -412,6 +412,33 @@ fn do_map2( } } +/// Combines three lists into a single list using the given function. +/// +/// If a list is longer than the others the extra elements are dropped. +/// +pub fn map3( + list1: List(a), + list2: List(b), + list3: List(c), + with fun: fn(a, b, c) -> d, +) -> List(d) { + do_map3(list1, list2, list3, fun, []) +} + +fn do_map3( + list1: List(a), + list2: List(b), + list3: List(c), + fun: fn(a, b, c) -> d, + acc: List(d), +) -> List(d) { + case list1, list2, list3 { + [], _, _ | _, [], _ | _, _, [] -> reverse(acc) + [a, ..as_], [b, ..bs], [c, ..cs] -> + do_map3(as_, bs, cs, fun, [fun(a, b, c), ..acc]) + } +} + /// Similar to `map` but also lets you pass around an accumulated value. /// /// ## Examples -- cgit v1.2.3