add functions car and cdr because they are familiar for Lispers

This commit is contained in:
Pekka Laiho 2020-12-19 20:46:51 +07:00
parent 16808c7ff9
commit c53eba8d23

View File

@ -90,9 +90,11 @@ class Collections implements ILib
// Get partial seq
$env->set('first', new CoreFunc('first', 'Return the first element of a sequence or null.', 1, 1,
foreach (['car', 'first'] as $name) {
$env->set($name, new CoreFunc($name, 'Return the first element of a sequence or null.', 1, 1,
fn (Seq $a) => $a->getData()[0] ?? null
));
}
$env->set('second', new CoreFunc('second', 'Return the second element of a sequence or null.', 1, 1,
fn (Seq $a) => $a->getData()[1] ?? null
@ -116,11 +118,13 @@ class Collections implements ILib
}
));
$env->set('tail', new CoreFunc('tail', 'Return new sequence containing all elements of argument except first.', 1, 1,
foreach (['cdr', 'tail'] as $name) {
$env->set($name, new CoreFunc($name, 'Return new sequence containing all elements of argument except first.', 1, 1,
function (Seq $a) {
return $a::new(array_slice($a->getData(), 1));
}
));
}
$env->set('slice', new CoreFunc('slice', 'Return a slice of the given sequence. Second argument is offset and third is length.', 2, 3,
function (Seq $a, int $offset, ?int $length = null) {