From 5e49f724faac560f1e283c7e6d9a76fca1e99836 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Tue, 2 Jun 2020 19:17:18 +0700 Subject: [PATCH] added more functions for collections --- src/Env.php | 6 ------ src/Hash.php | 6 ++++++ src/Lib/Collections.php | 42 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/Env.php b/src/Env.php index 8ba4d0e..c8b9416 100644 --- a/src/Env.php +++ b/src/Env.php @@ -20,10 +20,4 @@ class Env extends Hash throw new MadLispException("symbol $key not defined in env"); } - - public function set(string $key, $value) - { - $this->data[$key] = $value; - return $value; - } } diff --git a/src/Hash.php b/src/Hash.php index 0b4bcc0..ce09a70 100644 --- a/src/Hash.php +++ b/src/Hash.php @@ -11,4 +11,10 @@ class Hash extends Collection throw new MadLispException("hash does not contain key $key"); } + + public function set(string $key, $value) + { + $this->data[$key] = $value; + return $value; + } } diff --git a/src/Lib/Collections.php b/src/Lib/Collections.php index fdeda31..1f1d105 100644 --- a/src/Lib/Collections.php +++ b/src/Lib/Collections.php @@ -30,12 +30,16 @@ class Collections implements ILib fn (...$args) => new Vector($args) )); - // Other + // Read information $env->set('empty?', new CoreFunc('empty?', 'Return true if collection is empty.', 1, 1, fn (Collection $a) => $a->count() == 0 )); + $env->set('get', new CoreFunc('get', 'Get the item from first argument (collection) by using the second argument as index or key.', 2, 2, + fn (Collection $a, $b) => $a->get($b) + )); + $env->set('len', new CoreFunc('len', 'Return the length of string or number of elements in collection.', 1, 1, function ($a) { if ($a instanceof Collection) { @@ -48,6 +52,8 @@ class Collections implements ILib } )); + // Get partial list + $env->set('first', new CoreFunc('first', 'Return the first element of a sequence or null.', 1, 1, fn (Seq $a) => $a->getData()[0] ?? null )); @@ -74,6 +80,18 @@ class Collections implements ILib } )); + // Manipulate list + + $env->set('push', new CoreFunc('push', 'Push the remaining arguments at the end of the sequence (first argument).', 2, -1, + function (Seq $a, ...$b) { + $data = $a->getData(); + foreach ($b as $c) { + $data[] = $c; + } + return $a::new($data); + } + )); + $env->set('map', new CoreFunc('map', 'Apply the first argument (function) to all elements of second argument (sequence).', 2, 2, function (Func $f, Seq $a) { return $a::new(array_map($f->getClosure(), $a->getData())); @@ -86,6 +104,28 @@ class Collections implements ILib } )); + // Hash map functions + + $env->set('key?', new CoreFunc('key?', 'Return true if first argument (hash-map) contains the second argument as key.', 2, 2, + fn (Hash $a, string $b) => $a->has($b) + )); + + $env->set('set', new CoreFunc('set', 'Create new hash-map from first argument and set key (second argument) to value given by third argument.', 3, 3, + function (Hash $a, string $key, $val) { + // Immutable version + $hash = new Hash($a->getData()); + $hash->set($key, $val); + return $hash; + } + )); + + $env->set('set!', new CoreFunc('set!', 'Modify the hash-map (first argument) and set key (second argument) to value given by third argument and return the value.', 3, 3, + function (Hash $a, string $key, $val) { + // Mutable version + return $a->set($key, $val); + } + )); + $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())) ));