aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/gleam/dict.gleam18
-rw-r--r--test/gleam/dict_test.gleam17
2 files changed, 35 insertions, 0 deletions
diff --git a/src/gleam/dict.gleam b/src/gleam/dict.gleam
index f44daa3..99ca30c 100644
--- a/src/gleam/dict.gleam
+++ b/src/gleam/dict.gleam
@@ -35,6 +35,24 @@ pub type Dict(key, value)
@external(javascript, "../gleam_stdlib.mjs", "map_size")
pub fn size(dict: Dict(k, v)) -> Int
+/// Determines whether or not the dict is empty.
+///
+/// ## Examples
+///
+/// ```gleam
+/// new() |> is_empty
+/// // -> True
+/// ```
+///
+/// ```gleam
+/// new() |> insert("b", 1) |> is_empty
+/// // -> False
+/// ```
+///
+pub fn is_empty(dict: Dict(a, b)) -> Bool {
+ dict == new()
+}
+
/// Converts the dict to a list of 2-element tuples `#(key, value)`, one for
/// each key-value pair in the dict.
///
diff --git a/test/gleam/dict_test.gleam b/test/gleam/dict_test.gleam
index 0bcb334..9ec1475 100644
--- a/test/gleam/dict_test.gleam
+++ b/test/gleam/dict_test.gleam
@@ -349,6 +349,23 @@ pub fn size_test() {
|> should.equal(18)
}
+pub fn is_empty_test() {
+ dict.new()
+ |> dict.is_empty()
+ |> should.be_true()
+
+ dict.new()
+ |> dict.insert(1, 10)
+ |> dict.is_empty()
+ |> should.be_false()
+
+ dict.new()
+ |> dict.insert(1, 10)
+ |> dict.delete(1)
+ |> dict.is_empty()
+ |> should.be_true()
+}
+
// https://github.com/gleam-lang/stdlib/issues/435
pub fn peters_bug_test() {
dict.new()