Skip to content

Commit 90eb83c

Browse files
committed
set.to_list
1 parent f310627 commit 90eb83c

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
- Created the `iterator` module with the `unfold`, `repeatedly`, `repeat`,
66
`from_list`, `fold`, `run`, `to_list`, `take`, `drop`, `map`, `filter`,
77
`cycle`, and `range` functions.
8-
- Created the `set` module with the `new`, `insert`, `delete`, and `contains`
9-
functions.
8+
- Created the `set` module with the `new`, `insert`, `delete`, `to_list` and
9+
`contains` functions.
1010
- Created the `io` module with the `print`, `println`, and `debug` functions.
1111
- Created the `queue` module with the `new`, `from_list`, `to_list`,
1212
`is_empty`, `length`, `push_back`, `push_front`, `pop_back`, `pop_front`,
@@ -26,6 +26,7 @@
2626
`tuple2_of` functions.
2727
- The `list` module gains the `filter_map` function.
2828
- The `list.contains` label `has` has been changed to `any`.
29+
- The `list.sort` label `sort_by` has been changed to `by`.
2930

3031
## v0.8.0 - 2020-04-28
3132

src/gleam/list.gleam

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,10 @@ fn do_sort(
721721
/// ## Examples
722722
///
723723
/// > import gleam/int
724-
/// > list.sort([4, 3, 6, 5, 4, 1, 2], int.compare)
724+
/// > list.sort([4, 3, 6, 5, 4, 1, 2], by: int.compare)
725725
/// [1, 2, 3, 4, 4, 5, 6]
726726
///
727-
pub fn sort(list: List(a), sort_by compare: fn(a, a) -> Order) -> List(a) {
727+
pub fn sort(list: List(a), by compare: fn(a, a) -> Order) -> List(a) {
728728
do_sort(list, compare, length(list))
729729
}
730730

src/gleam/set.gleam

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,17 @@ pub fn contains(in set: Set(element), this member: element) -> Bool {
7373
pub fn delete(from set: Set(element), this member: element) -> Set(element) {
7474
Set(map: map.delete(set.map, member))
7575
}
76+
77+
/// Convert the set into a list of the contained elements.
78+
///
79+
/// The list has no specific ordering, any unintentional ordering may change in
80+
/// future versions of Gleam or Erlang.
81+
///
82+
/// ## Examples
83+
///
84+
/// > new() |> insert(2) |> to_list
85+
/// [2]
86+
///
87+
pub fn to_list(set: Set(element)) -> List(element) {
88+
map.keys(set.map)
89+
}

test/gleam/set_test.gleam

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import gleam/should
22
import gleam/set
3+
import gleam/list
4+
import gleam/int
35

46
pub fn size_test() {
57
set.new()
@@ -38,3 +40,13 @@ pub fn delete_test() {
3840
|> set.contains(1)
3941
|> should.be_false
4042
}
43+
44+
pub fn to_list_test() {
45+
set.new()
46+
|> set.insert(2)
47+
|> set.insert(3)
48+
|> set.insert(4)
49+
|> set.to_list
50+
|> list.sort(by: int.compare)
51+
|> should.equal([2, 3, 4])
52+
}

0 commit comments

Comments
 (0)