From 80a72b616be214a2da064589fc6aeffaf2cca879 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Sun, 18 Oct 2020 09:30:21 +0700 Subject: [PATCH] support strings for empty? --- README.md | 1 + src/Lib/Collections.php | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 687f4cb..ce3318a 100644 --- a/README.md +++ b/README.md @@ -271,6 +271,7 @@ sqrt | `(sqrt 2)` | `1.41` | Calculate the square root. Name | Example | Example result | Description ------- | ------- | -------------- | ----------- +empty? | `(empty? "")` | `true` | Return true if argument is empty string. len | `(len "hello world")` | `11` | Return the length of a string using [strlen](https://www.php.net/manual/en/function.strlen.php). trim | `(trim " abc ")` | `"abc"` | Trim the string using [trim](https://www.php.net/manual/en/function.trim). upcase | `(upcase "abc")` | `"ABC"` | Make the string upper case using [strtoupper](https://www.php.net/manual/en/function.strtoupper). diff --git a/src/Lib/Collections.php b/src/Lib/Collections.php index 45089b1..a2e0d7e 100644 --- a/src/Lib/Collections.php +++ b/src/Lib/Collections.php @@ -45,7 +45,15 @@ class Collections implements ILib // Read information $env->set('empty?', new CoreFunc('empty?', 'Return true if collection is empty.', 1, 1, - fn (Collection $a) => $a->count() == 0 + function ($a) { + if ($a instanceof Collection) { + return $a->count() === 0; + } elseif (is_string($a)) { + return $a === ''; + } + + throw new MadLispException('argument to empty? is not collection or string'); + } )); $env->set('get', new CoreFunc('get', 'Get the item from first argument (collection) by using the second argument as index or key.', 2, 2,