diff options
-rw-r--r-- | CHANGELOG.md | 5 | ||||
-rw-r--r-- | gen/src/gleam@list.erl | 18 | ||||
-rw-r--r-- | gen/test/gleam@list_test.erl | 9 | ||||
-rw-r--r-- | src/gleam/list.gleam | 12 | ||||
-rw-r--r-- | test/gleam/list_test.gleam | 15 |
5 files changed, 34 insertions, 25 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index a9b613a..fedd31e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Unreleased + +- `list:sort` now requires a compare function as comparison operators now only + work on Ints. + ## v0.3.1 - 2019-08-08 - `result:map_error` has been relaxed to allow mapping to a different error diff --git a/gen/src/gleam@list.erl b/gen/src/gleam@list.erl index 548e983..411a6c8 100644 --- a/gen/src/gleam@list.erl +++ b/gen/src/gleam@list.erl @@ -1,7 +1,7 @@ -module(gleam@list). -compile(no_auto_import). --export([length/1, reverse/1, is_empty/1, contains/2, head/1, tail/1, filter/2, map/2, index_map/2, traverse/2, drop/2, take/2, new/0, append/2, flatten/1, fold/3, fold_right/3, find/2, all/2, any/2, zip/2, strict_zip/2, intersperse/2, at/2, unique/1, sort/1, range/2, repeat/2, split/2, split_while/2]). +-export([length/1, reverse/1, is_empty/1, contains/2, head/1, tail/1, filter/2, map/2, index_map/2, traverse/2, drop/2, take/2, new/0, append/2, flatten/1, fold/3, fold_right/3, find/2, all/2, any/2, zip/2, strict_zip/2, intersperse/2, at/2, unique/1, sort/2, range/2, repeat/2, split/2, split_while/2]). length(A) -> erlang:length(A). @@ -277,7 +277,7 @@ unique(List) -> [X | unique(filter(Rest, fun(Y) -> Y /= X end))] end. -merge_sort(A, B) -> +merge_sort(A, B, Compare) -> case {A, B} of {[], _} -> B; @@ -286,16 +286,16 @@ merge_sort(A, B) -> A; {[Ax | Ar], [Bx | Br]} -> - case Ax < Bx of - true -> - [Ax | merge_sort(Ar, B)]; + case Compare(Ax, Bx) of + lt -> + [Ax | merge_sort(Ar, B, Compare)]; - false -> - [Bx | merge_sort(A, Br)] + _ -> + [Bx | merge_sort(A, Br, Compare)] end end. -sort(List) -> +sort(List, Compare) -> ListLength = length(List), case ListLength < 2 of true -> @@ -305,7 +305,7 @@ sort(List) -> SplitLength = ListLength div 2, AList = take(List, SplitLength), BList = drop(List, SplitLength), - merge_sort(sort(AList), sort(BList)) + merge_sort(sort(AList, Compare), sort(BList, Compare), Compare) end. range(Start, Stop) -> diff --git a/gen/test/gleam@list_test.erl b/gen/test/gleam@list_test.erl index 942bee8..1037884 100644 --- a/gen/test/gleam@list_test.erl +++ b/gen/test/gleam@list_test.erl @@ -179,12 +179,11 @@ unique_test() -> gleam@expect:equal(gleam@list:unique([]), []). sort_test() -> - gleam@expect:equal(gleam@list:sort([4, 3, 6, 5, 4]), [3, 4, 4, 5, 6]), - gleam@expect:equal(gleam@list:sort([]), []), gleam@expect:equal( - gleam@list:sort([{1, 2}, {4, 5}, {3, 2}]), - [{1, 2}, {3, 2}, {4, 5}] - ). + gleam@list:sort([4, 3, 6, 5, 4], fun gleam@int:compare/2), + [3, 4, 4, 5, 6] + ), + gleam@expect:equal(gleam@list:sort([], fun gleam@int:compare/2), []). index_map_test() -> gleam@expect:equal( diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index 96ed75e..ab8b600 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -232,19 +232,19 @@ pub fn unique(list) { } } -fn merge_sort(a, b) { +fn merge_sort(a, b, compare) { case {a, b} { | {[], _} -> b | {_, []} -> a | {[ax | ar], [bx | br]} -> - case ax < bx { - | True -> [ax | merge_sort(ar, b)] - | False -> [bx | merge_sort(a, br)] + case compare(ax, bx) { + | order:Lt -> [ax | merge_sort(ar, b, compare)] + | _ -> [bx | merge_sort(a, br, compare)] } } } -pub fn sort(list) { +pub fn sort(list, compare) { let list_length = length(list) case list_length < 2 { | True -> list @@ -252,7 +252,7 @@ pub fn sort(list) { 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), sort(b_list)) + merge_sort(sort(a_list, compare), sort(b_list, compare), compare) } } diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam index 25028d9..a8b58e0 100644 --- a/test/gleam/list_test.gleam +++ b/test/gleam/list_test.gleam @@ -1,6 +1,7 @@ import gleam/expect import gleam/list import gleam/int +import gleam/float import gleam/string pub fn length_test() { @@ -258,14 +259,18 @@ pub fn unique_test() { } pub fn sort_test() { - list:sort([4, 3, 6, 5, 4]) + [4, 3, 6, 5, 4] + |> list:sort(_, int:compare) |> expect:equal(_, [3, 4, 4, 5, 6]) - list:sort([]) - |> expect:equal(_, []) + // TODO: Requires float:compare + // [4.1, 3.1, 6.1, 5.1, 4.1] + // |> list:sort(_, float:compare) + // |> expect:equal(_, [3.1, 4.1, 4.1, 5.1, 6.1]) - list:sort([{1, 2}, {4, 5}, {3, 2}]) - |> expect:equal(_, [{1, 2}, {3, 2}, {4, 5}]) + [] + |> list:sort(_, int:compare) + |> expect:equal(_, []) } pub fn index_map_test() { |