add concat function

This commit is contained in:
Pekka Laiho 2020-12-05 15:26:51 +07:00
parent 33f4cecbbf
commit 52b7bc6934
2 changed files with 10 additions and 2 deletions

View File

@ -199,6 +199,7 @@ tail | `(tail [1 2 3 4])` | `[2 3 4]` | Return new sequence which contains al
slice | `(slice [1 2 3 4 5] 1 3)` | `[2 3 4]` | Return a slice of the sequence using offset and length. Uses [array_slice](https://www.php.net/manual/en/function.array-slice.php). slice | `(slice [1 2 3 4 5] 1 3)` | `[2 3 4]` | Return a slice of the sequence using offset and length. Uses [array_slice](https://www.php.net/manual/en/function.array-slice.php).
apply | `(apply + 1 2 [3 4])` | `10` | Call the first argument using a sequence as argument list. Intervening arguments are prepended to the list. apply | `(apply + 1 2 [3 4])` | `10` | Call the first argument using a sequence as argument list. Intervening arguments are prepended to the list.
chunk | `(chunk [1 2 3 4 5] 2)` | `[[1 2] [3 4] [5]]` | Divide a sequence to multiple sequences with specified length using [array_chunk](https://www.php.net/manual/en/function.array-chunk.php). chunk | `(chunk [1 2 3 4 5] 2)` | `[[1 2] [3 4] [5]]` | Divide a sequence to multiple sequences with specified length using [array_chunk](https://www.php.net/manual/en/function.array-chunk.php).
concat | `(concat [1 2] '(3 4))` | `[1 2 3 4]` | Concatenate multiple sequences together. The type (list or vector) of the first argument determines the type of the output.
push | `(push [1 2] 3 4)` | `[1 2 3 4]` | Create new sequence by inserting arguments at the end. push | `(push [1 2] 3 4)` | `[1 2 3 4]` | Create new sequence by inserting arguments at the end.
pull | `(pull 1 2 [3 4])` | `[1 2 3 4]` | Create new sequence by inserting arguments at the beginning. pull | `(pull 1 2 [3 4])` | `[1 2 3 4]` | Create new sequence by inserting arguments at the beginning.
map | `(map (fn (a) (* a 2)) [1 2 3])` | `[2 4 6]` | Create new sequence by calling a function for each item. Uses [array_map](https://www.php.net/manual/en/function.array-map.php) internally. map | `(map (fn (a) (* a 2)) [1 2 3])` | `[2 4 6]` | Create new sequence by calling a function for each item. Uses [array_map](https://www.php.net/manual/en/function.array-map.php) internally.

View File

@ -72,7 +72,7 @@ class Collections implements ILib
} }
)); ));
// Get partial list // Get partial seq
$env->set('first', new CoreFunc('first', 'Return the first element of a sequence or null.', 1, 1, $env->set('first', new CoreFunc('first', 'Return the first element of a sequence or null.', 1, 1,
fn (Seq $a) => $a->getData()[0] ?? null fn (Seq $a) => $a->getData()[0] ?? null
@ -112,7 +112,7 @@ class Collections implements ILib
} }
)); ));
// Manipulate list // Manipulate seq
$env->set('apply', new CoreFunc('apply', 'Apply the first argument (function) using second argument (sequence) as arguments.', 2, -1, $env->set('apply', new CoreFunc('apply', 'Apply the first argument (function) using second argument (sequence) as arguments.', 2, -1,
function (...$args) { function (...$args) {
@ -144,6 +144,13 @@ class Collections implements ILib
} }
)); ));
$env->set('concat', new CoreFunc('concat', 'Concatenate multiple sequences together.', 1, -1,
function (Seq ...$args) {
$data = array_map(fn ($a) => $a->getData(), $args);
return $args[0]::new(array_merge(...$data));
}
));
$env->set('push', new CoreFunc('push', 'Push the remaining arguments at the end of the sequence (first argument).', 2, -1, $env->set('push', new CoreFunc('push', 'Push the remaining arguments at the end of the sequence (first argument).', 2, -1,
function (Seq $a, ...$b) { function (Seq $a, ...$b) {
$data = $a->getData(); $data = $a->getData();