From 88405921248b94c360e91840fe7ebfe98c4d01af Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 27 Apr 2021 15:18:52 +1000 Subject: Use reverse and fold instead of append --- src/gleam/list.gleam | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index 1fcd2d8..4745f43 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -1474,7 +1474,12 @@ pub fn combinations(items: List(a), by n: Int) -> List(List(a)) { [x, ..xs] -> { let first_combinations = map(combinations(xs, n - 1), with: fn(com) { [x, ..com] }) - append(first_combinations, combinations(xs, n)) + |> reverse + fold( + first_combinations, + combinations(xs, n), + fn(c, acc) { [c, ..acc] }, + ) } } } -- cgit v1.2.3