From 7be3f6d4138ae51b79f61699c85b96a4bf7d25bc Mon Sep 17 00:00:00 2001 From: Julia Pitts Date: Sun, 16 Jun 2024 15:21:41 -0700 Subject: Add gleam/set.{map}. --- src/gleam/set.gleam | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src') diff --git a/src/gleam/set.gleam b/src/gleam/set.gleam index 12bfba1..9e64b07 100644 --- a/src/gleam/set.gleam +++ b/src/gleam/set.gleam @@ -218,6 +218,21 @@ pub fn filter( Set(dict.filter(in: set.dict, keeping: fn(m, _) { predicate(m) })) } +/// Creates a new set from a given set with the result of applying the given +/// function to each member. +/// +/// ## Examples +/// +/// ```gleam +/// map(from_list([1, 2, 3, 4], fn(x) { x * 2 })) +/// // -> [2, 4, 6, 8] +/// ``` +pub fn map(set: Set(member), with fun: fn(member) -> mapped) -> Set(mapped) { + fold(over: set, from: new(), with: fn(acc, member) { + insert(acc, fun(member)) + }) +} + /// Creates a new set from a given set with all the same entries except any /// entry found on the given list. /// -- cgit v1.2.3