Skip to content

Commit e501f3d

Browse files
committed
set.from_list
1 parent 90eb83c commit e501f3d

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
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`, `to_list` and
9-
`contains` functions.
8+
- Created the `set` module with the `new`, `insert`, `delete`, `to_list`,
9+
`from_list` and `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`,
@@ -27,6 +27,7 @@
2727
- The `list` module gains the `filter_map` function.
2828
- The `list.contains` label `has` has been changed to `any`.
2929
- The `list.sort` label `sort_by` has been changed to `by`.
30+
- The `list.fold`'s first argument gained the label `over`.
3031

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

src/gleam/list.gleam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ pub fn flatten(lists: List(List(a))) -> List(a) {
426426
///
427427
/// This function runs in linear time.
428428
///
429-
pub fn fold(list: List(a), from initial: b, with fun: fn(a, b) -> b) -> b {
429+
pub fn fold(over list: List(a), from initial: b, with fun: fn(a, b) -> b) -> b {
430430
case list {
431431
[] -> initial
432432
[x, ..rest] -> fold(rest, fun(x, initial), fun)

src/gleam/set.gleam

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import gleam/map.{Map}
22
import gleam/result
3+
import gleam/list
34

45
/// A set is a collection of unique elements of the same type.
56
///
@@ -65,6 +66,8 @@ pub fn contains(in set: Set(element), this member: element) -> Bool {
6566
/// Remove an element from a set. If the set does not contain the element then
6667
/// the set is returned unchanged.
6768
///
69+
/// This function runs in logarithmic time.
70+
///
6871
/// ## Examples
6972
///
7073
/// > new() |> insert(2) |> delete(2) |> contains(1)
@@ -79,6 +82,8 @@ pub fn delete(from set: Set(element), this member: element) -> Set(element) {
7982
/// The list has no specific ordering, any unintentional ordering may change in
8083
/// future versions of Gleam or Erlang.
8184
///
85+
/// This function runs in linear time.
86+
///
8287
/// ## Examples
8388
///
8489
/// > new() |> insert(2) |> to_list
@@ -87,3 +92,22 @@ pub fn delete(from set: Set(element), this member: element) -> Set(element) {
8792
pub fn to_list(set: Set(element)) -> List(element) {
8893
map.keys(set.map)
8994
}
95+
96+
/// Create a new set of the elements in a given list.
97+
///
98+
/// This function runs in loglinear time.
99+
///
100+
/// ## Examples
101+
///
102+
/// > import gleam/list
103+
/// > [1, 1, 2, 4, 3, 2] |> from_list |> to_list |> list.sort
104+
/// [1, 3, 3, 4]
105+
///
106+
pub fn from_list(elements: List(element)) -> Set(element) {
107+
let map = list.fold(
108+
over: elements,
109+
from: map.new(),
110+
with: fn(k, m) { map.insert(m, k, []) },
111+
)
112+
Set(map)
113+
}

test/gleam/set_test.gleam

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,11 @@ pub fn to_list_test() {
5050
|> list.sort(by: int.compare)
5151
|> should.equal([2, 3, 4])
5252
}
53+
54+
pub fn from_list_test() {
55+
[1, 1, 2, 4, 3, 2]
56+
|> set.from_list
57+
|> set.to_list
58+
|> list.sort(by: int.compare)
59+
|> should.equal([1, 3, 3, 4])
60+
}

0 commit comments

Comments
 (0)