aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSebastian Porto <s@porto5.com>2021-05-06 19:18:51 +1000
committerLouis Pilfold <louis@lpil.uk>2021-05-11 10:28:03 +0100
commit931d4544510afa4d879ef6c5e52f4750d0812f45 (patch)
tree4cd6527c49c953af011eda91481ca1901e59e56f /src
parent8efb599e9e97c856f7e3d9c60b5b4cf9df6a1fe0 (diff)
downloadgleam_stdlib-931d4544510afa4d879ef6c5e52f4750d0812f45.tar.gz
gleam_stdlib-931d4544510afa4d879ef6c5e52f4750d0812f45.zip
Add list.transpose and list.interleave
Diffstat (limited to 'src')
-rw-r--r--src/gleam/list.gleam45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index a0b3b16..1e29ff3 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -1522,3 +1522,48 @@ pub fn combination_pairs(items: List(a)) -> List(#(a, a)) {
do_combination_pairs(items)
|> flatten
}
+
+/// Make a list alternating the elements from the given lists
+///
+/// ## Examples
+///
+/// ```
+/// > list.interleave([[1, 2], [101, 102], [201, 202]])
+/// [1, 101, 201, 2, 102, 202]
+/// ```
+///
+pub fn interleave(list: List(List(a))) -> List(a) {
+ transpose(list)
+ |> flatten
+}
+
+/// Transpose rows and columns of the list of lists.
+///
+/// ## Examples
+///
+/// ```
+/// > transpose([[1, 2, 3], [101, 102, 103]])
+/// [[1, 101], [2, 102], [3, 103]]
+/// ```
+pub fn transpose(list_of_list: List(List(a))) -> List(List(a)) {
+ let take_first = fn(list) {
+ case list {
+ [] -> []
+ [f] -> [f]
+ [f, .._rest] -> [f]
+ }
+ }
+
+ case list_of_list {
+ [] -> []
+ [[], ..xss] -> transpose(xss)
+ rows -> {
+ let firsts =
+ rows
+ |> map(take_first)
+ |> flatten
+ let rest = transpose(map(rows, drop(_, 1)))
+ [firsts, ..rest]
+ }
+ }
+}