diff options
-rw-r--r-- | CHANGELOG.md | 5 | ||||
-rw-r--r-- | src/gleam/list.gleam | 4 | ||||
-rw-r--r-- | src/gleam/set.gleam | 14 | ||||
-rw-r--r-- | test/gleam/set_test.gleam | 12 |
4 files changed, 31 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c80add..b4fc594 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,8 @@ - Created the `iterator` module with the `unfold`, `repeatedly`, `repeat`, `from_list`, `fold`, `run`, `to_list`, `take`, `drop`, `map`, `filter`, `cycle`, and `range` functions. -- Created the `set` module with the `new`, `insert`, `delete`, and `contains` - functions. +- Created the `set` module with the `new`, `insert`, `delete`, `to_list` and + `contains` functions. - Created the `io` module with the `print`, `println`, and `debug` functions. - Created the `queue` module with the `new`, `from_list`, `to_list`, `is_empty`, `length`, `push_back`, `push_front`, `pop_back`, `pop_front`, @@ -26,6 +26,7 @@ `tuple2_of` functions. - The `list` module gains the `filter_map` function. - The `list.contains` label `has` has been changed to `any`. +- The `list.sort` label `sort_by` has been changed to `by`. ## v0.8.0 - 2020-04-28 diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index bb35252..c0414d4 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -721,10 +721,10 @@ fn do_sort( /// ## Examples /// /// > import gleam/int -/// > list.sort([4, 3, 6, 5, 4, 1, 2], int.compare) +/// > list.sort([4, 3, 6, 5, 4, 1, 2], by: int.compare) /// [1, 2, 3, 4, 4, 5, 6] /// -pub fn sort(list: List(a), sort_by compare: fn(a, a) -> Order) -> List(a) { +pub fn sort(list: List(a), by compare: fn(a, a) -> Order) -> List(a) { do_sort(list, compare, length(list)) } diff --git a/src/gleam/set.gleam b/src/gleam/set.gleam index 7b52064..a654200 100644 --- a/src/gleam/set.gleam +++ b/src/gleam/set.gleam @@ -73,3 +73,17 @@ pub fn contains(in set: Set(element), this member: element) -> Bool { pub fn delete(from set: Set(element), this member: element) -> Set(element) { Set(map: map.delete(set.map, member)) } + +/// Convert the set into a list of the contained elements. +/// +/// The list has no specific ordering, any unintentional ordering may change in +/// future versions of Gleam or Erlang. +/// +/// ## Examples +/// +/// > new() |> insert(2) |> to_list +/// [2] +/// +pub fn to_list(set: Set(element)) -> List(element) { + map.keys(set.map) +} diff --git a/test/gleam/set_test.gleam b/test/gleam/set_test.gleam index dcc1356..efa4e0f 100644 --- a/test/gleam/set_test.gleam +++ b/test/gleam/set_test.gleam @@ -1,5 +1,7 @@ import gleam/should import gleam/set +import gleam/list +import gleam/int pub fn size_test() { set.new() @@ -38,3 +40,13 @@ pub fn delete_test() { |> set.contains(1) |> should.be_false } + +pub fn to_list_test() { + set.new() + |> set.insert(2) + |> set.insert(3) + |> set.insert(4) + |> set.to_list + |> list.sort(by: int.compare) + |> should.equal([2, 3, 4]) +} |