support strings for empty?

This commit is contained in:
Pekka Laiho 2020-10-18 09:30:21 +07:00
parent d65fb5a58a
commit 80a72b616b
2 changed files with 10 additions and 1 deletions

View File

@ -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).

View File

@ -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,