diff options
author | Louis Pilfold <louis@lpil.uk> | 2019-08-14 23:33:06 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2019-08-14 23:33:06 +0100 |
commit | 261889c5b56b6610c370e2bd6ae78c0f81e22688 (patch) | |
tree | 5f035fc7d491fb4c8c61969e100b31ce7bf1ec8b /src | |
parent | ddba860d353d86c3daa8c3bf7054e9f73c43eac4 (diff) | |
download | gleam_stdlib-261889c5b56b6610c370e2bd6ae78c0f81e22688.tar.gz gleam_stdlib-261889c5b56b6610c370e2bd6ae78c0f81e22688.zip |
Slightly optimise list:sort
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/list.gleam | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index ab8b600..24437c6 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -244,18 +244,25 @@ fn merge_sort(a, b, compare) { } } -pub fn sort(list, compare) { - let list_length = length(list) +fn do_sort(list, compare, list_length) { case list_length < 2 { | True -> list | False -> let split_length = list_length / 2 let a_list = take(list, split_length) let b_list = drop(list, split_length) - merge_sort(sort(a_list, compare), sort(b_list, compare), compare) + merge_sort( + do_sort(a_list, compare, split_length), + do_sort(b_list, compare, list_length - split_length), + compare, + ) } } +pub fn sort(list, compare) { + do_sort(list, compare, length(list)) +} + pub fn range(start, stop) { case int:compare(start, stop) { | order:Eq -> [] |