aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2020-05-25 10:44:01 +0100
committerLouis Pilfold <louis@lpil.uk>2020-05-26 19:19:29 +0100
commitf310627585fa8bec37bee7b3ac5e783d0cfbc77a (patch)
treee8cebd89b880556d026537a6a20c509bd0a1c6ed
parent6378ec6a0e1af8c31e407c994e8077c07812f401 (diff)
downloadgleam_stdlib-f310627585fa8bec37bee7b3ac5e783d0cfbc77a.tar.gz
gleam_stdlib-f310627585fa8bec37bee7b3ac5e783d0cfbc77a.zip
set.delete!
-rw-r--r--CHANGELOG.md3
-rw-r--r--src/gleam/set.gleam12
-rw-r--r--test/gleam/set_test.gleam8
3 files changed, 22 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 531cba6..9c80add 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,7 +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`, and `contains` functions.
+- Created the `set` module with the `new`, `insert`, `delete`, 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`,
diff --git a/src/gleam/set.gleam b/src/gleam/set.gleam
index cd7cbe0..7b52064 100644
--- a/src/gleam/set.gleam
+++ b/src/gleam/set.gleam
@@ -61,3 +61,15 @@ pub fn contains(in set: Set(element), this member: element) -> Bool {
|> map.get(member)
|> result.is_ok
}
+
+/// Remove an element from a set. If the set does not contain the element then
+/// the set is returned unchanged.
+///
+/// ## Examples
+///
+/// > new() |> insert(2) |> delete(2) |> contains(1)
+/// False
+///
+pub fn delete(from set: Set(element), this member: element) -> Set(element) {
+ Set(map: map.delete(set.map, member))
+}
diff --git a/test/gleam/set_test.gleam b/test/gleam/set_test.gleam
index 7247c5c..dcc1356 100644
--- a/test/gleam/set_test.gleam
+++ b/test/gleam/set_test.gleam
@@ -30,3 +30,11 @@ pub fn contains_test() {
|> set.contains(this: 1)
|> should.be_false
}
+
+pub fn delete_test() {
+ set.new()
+ |> set.insert(1)
+ |> set.delete(1)
+ |> set.contains(1)
+ |> should.be_false
+}