added functions unset and unset!

This commit is contained in:
Pekka Laiho 2020-12-08 16:02:56 +07:00
parent 208b4ac86d
commit 07f9c7af8e
3 changed files with 27 additions and 0 deletions

View File

@ -329,6 +329,8 @@ reverse | `(reverse [1 2 3])` | `[3 2 1]` | Reverse the order of a sequence. Use
key? | `(key? {"a" "b"} "a")` | `true` | Return true if the hash-map contains the given key. key? | `(key? {"a" "b"} "a")` | `true` | Return true if the hash-map contains the given key.
set | `(set {"a" 1} "b" 2)` | `{"a":1 "b":2}` | Create new hash-map which contains the given key-value pair. set | `(set {"a" 1} "b" 2)` | `{"a":1 "b":2}` | Create new hash-map which contains the given key-value pair.
set! | `(set! {"a" 1} "b" 2)` | `2` | Modify the given hash-map by setting the given key-value pair and return the set value. **This function is mutable!** set! | `(set! {"a" 1} "b" 2)` | `2` | Modify the given hash-map by setting the given key-value pair and return the set value. **This function is mutable!**
unset | `(unset {"a":1 "b":2 "c":3} "b")` | `{"a":1 "c":3}` | Create a new hash-map with the given key removed.
unset! | `(unset! {"a":1 "b":2 "c":3} "b")` | `2` | Modify the given hash-map by removing the given key and return the corresponding value. **This function is mutable!**
keys | `(keys {"a" 1 "b" 2})` | `("a" "b")` | Return a list of the keys for a hash-map. keys | `(keys {"a" 1 "b" 2})` | `("a" "b")` | Return a list of the keys for a hash-map.
values | `(values {"a" 1 "b" 2})` | `(1 2)` | Return a list of the values for a hash-map. values | `(values {"a" 1 "b" 2})` | `(1 2)` | Return a list of the values for a hash-map.
zip | `(zip ["a" "b"] [1 2])` | `{"a":1 "b":2}` | Create a hash-map using the first sequence as keys and the second as values. Uses [array_combine](https://www.php.net/manual/en/function.array-combine.php) internally. zip | `(zip ["a" "b"] [1 2])` | `{"a":1 "b":2}` | Create a hash-map using the first sequence as keys and the second as values. Uses [array_combine](https://www.php.net/manual/en/function.array-combine.php) internally.

View File

@ -17,4 +17,15 @@ class Hash extends Collection
$this->data[$key] = $value; $this->data[$key] = $value;
return $value; return $value;
} }
public function unset(string $key)
{
if (array_key_exists($key, $this->data)) {
$value = $this->data[$key];
unset($this->data[$key]);
return $value;
}
return null;
}
} }

View File

@ -255,6 +255,20 @@ class Collections implements ILib
} }
)); ));
$env->set('unset', new CoreFunc('unset', 'Create a new hash-map from the first argument with the given key removed.', 2, 2,
function (Hash $a, string $key) {
$data = $a->getData();
unset($data[$key]);
return new Hash($data);
}
));
$env->set('unset!', new CoreFunc('unset!', 'Modify the hash-map (first argument) and remove the given key.', 2, 2,
function (Hash $a, string $key) {
return $a->unset($key);
}
));
$env->set('keys', new CoreFunc('keys', 'Return the keys of a hash-map as a list.', 1, 1, $env->set('keys', new CoreFunc('keys', 'Return the keys of a hash-map as a list.', 1, 1,
fn (Hash $a) => new MList(array_keys($a->getData())) fn (Hash $a) => new MList(array_keys($a->getData()))
)); ));