aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSebastian Porto <s@porto5.com>2021-05-08 06:28:31 +1000
committerGitHub <noreply@github.com>2021-05-07 20:28:31 +0000
commit32b56eb7cd04bdd47a548b021602a018bdf782c6 (patch)
treedd55c7e7371e2938bec5eb4c613f04585122ffb1 /src
parent7b10d02d88af84560855337e45c755686a396943 (diff)
downloadgleam_stdlib-32b56eb7cd04bdd47a548b021602a018bdf782c6.tar.gz
gleam_stdlib-32b56eb7cd04bdd47a548b021602a018bdf782c6.zip
Add list.flat_map (#202)
Diffstat (limited to 'src')
-rw-r--r--src/gleam/list.gleam14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index ea67ba5..a0b3b16 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -452,6 +452,20 @@ pub fn flatten(lists: List(List(a))) -> List(a) {
do_flatten(lists, [])
}
+/// Map and flatten the result
+///
+/// ## Examples
+///
+/// ```
+/// > flat_map([2, 4, 6], fn(x) { [x, x + 1] })
+/// [2, 3, 4, 5, 6, 7]
+/// ```
+///
+pub fn flat_map(over list: List(a), with fun: fn(a) -> List(b)) -> List(b) {
+ map(list, fun)
+ |> flatten
+}
+
/// Reduces a list of elements into a single value by calling a given function
/// on each element, going from left to right.
///