aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian <s@porto5.com>2021-04-26 13:12:40 +1000
committerLouis Pilfold <louis@lpil.uk>2021-04-29 20:24:23 +0100
commit5240db2fc2e6ea7ed6577c1e6cc49d8c551aa2da (patch)
tree6b139a2690e9a93ead272fe2ee179a2b2b7cd3c5
parent038ca5b5ade57bbac098df21f21ec6ba056ae299 (diff)
downloadgleam_stdlib-5240db2fc2e6ea7ed6577c1e6cc49d8c551aa2da.tar.gz
gleam_stdlib-5240db2fc2e6ea7ed6577c1e6cc49d8c551aa2da.zip
Do not use append in combinations_by_2
-rw-r--r--src/gleam/list.gleam19
1 files changed, 12 insertions, 7 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index 0622147..869fc10 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -1480,6 +1480,16 @@ pub fn combinations(items: List(a), by n: Int) -> List(List(a)) {
}
}
+fn do_combinations_by_2(items: List(a)) -> List(List(tuple(a, a))) {
+ case items {
+ [] -> []
+ [x, ..xs] -> {
+ let first_combinations = map(xs, with: fn(other) { tuple(x, other) })
+ [first_combinations, combinations_by_2(xs)]
+ }
+ }
+}
+
/// Return unique pair combinations of elements in the list
///
/// ## Examples
@@ -1490,11 +1500,6 @@ pub fn combinations(items: List(a), by n: Int) -> List(List(a)) {
/// ```
///
pub fn combinations_by_2(items: List(a)) -> List(tuple(a, a)) {
- case items {
- [] -> []
- [x, ..xs] -> {
- let first_combinations = map(xs, with: fn(other) { tuple(x, other) })
- append(first_combinations, combinations_by_2(xs))
- }
- }
+ do_combinations_by_2(items)
+ |> flatten
}