From 2d34af6aa9312894c0e6967d30a0714f8a95c47d Mon Sep 17 00:00:00 2001 From: Veit Heller Date: Wed, 20 Jan 2021 09:54:08 +0100 Subject: [PATCH] feat: make empty? an interface (#1139) --- core/Array.carp | 1 + core/Interfaces.carp | 2 ++ core/Map.carp | 6 ++++-- core/StaticArray.carp | 1 + core/String.carp | 1 + test/string.carp | 2 +- 6 files changed, 10 insertions(+), 3 deletions(-) diff --git a/core/Array.carp b/core/Array.carp index 1057515a..821b9fc1 100644 --- a/core/Array.carp +++ b/core/Array.carp @@ -46,6 +46,7 @@ It will sum the previous sum with each new value, starting at `0`.") (doc empty? "checks whether the array `a` is empty.") (defn empty? [a] (= (Array.length a) 0)) + (implements empty? Array.empty?) (doc any? "checks whether any of the elements in `a` match the function `f`.") (defn any? [f a] diff --git a/core/Interfaces.carp b/core/Interfaces.carp index bab10793..3a60a52e 100644 --- a/core/Interfaces.carp +++ b/core/Interfaces.carp @@ -67,3 +67,5 @@ (definterface slice (Fn [&a Int Int] a)) (definterface blit (Fn [a] a)) ; For types that can be freely memcopied -- exact name is up for discussion + +(definterface empty? (Fn [&a] Bool)) diff --git a/core/Map.carp b/core/Map.carp index 9eca24c6..f829119e 100644 --- a/core/Map.carp +++ b/core/Map.carp @@ -288,7 +288,8 @@ (doc empty? "Check whether the map m is empty.") (defn empty? [m] - (= @(len m) 0)) + (= (Map.length m) 0)) + (implements empty? Map.empty?) (doc remove "Remove the value under the key k from the map m.") (defn remove [m k] @@ -520,7 +521,8 @@ (doc empty? "Check whether the set s is empty.") (defn empty? [s] - (= (length s) 0)) + (= (Set.length s) 0)) + (implements empty? Set.empty?) (doc remove "Remove the value `v` from the set `s`.") (defn remove [s v] diff --git a/core/StaticArray.carp b/core/StaticArray.carp index 01bb5fad..c9e422f5 100644 --- a/core/StaticArray.carp +++ b/core/StaticArray.carp @@ -52,6 +52,7 @@ (doc empty? "checks whether the array `a` is empty.") (defn empty? [a] (= (StaticArray.length a) 0)) + (implements empty? StaticArray.empty?) (doc any? "checks whether any of the elements in `a` match the function `f`.") (defn any? [f a] diff --git a/core/String.carp b/core/String.carp index 5dce5c3c..9437881e 100644 --- a/core/String.carp +++ b/core/String.carp @@ -73,6 +73,7 @@ (doc empty? "Check if the string is the empty string.") (defn empty? [s] (Int.= (length s) 0)) + (implements empty? String.empty?) (doc slice "Return part of a string, beginning at index `a` and ending at index `b`.") (defn slice [s a b] diff --git a/test/string.carp b/test/string.carp index 11a79351..1a5747e9 100644 --- a/test/string.carp +++ b/test/string.carp @@ -72,7 +72,7 @@ (empty? "") "empty? returns true on empty string") (assert-true test - (empty? &(zero)) + (empty? &(the String (zero))) "empty? returns true on (zero)") (assert-equal test &[\e \r \i \k]