mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-22 13:24:46 +00:00
added more functions for collections
This commit is contained in:
parent
190696db9b
commit
5e49f724fa
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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()))
|
||||
));
|
||||
|
Loading…
Reference in New Issue
Block a user