From 52b7bc693405edd56505ab170c1ba679a29fddc1 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Sat, 5 Dec 2020 15:26:51 +0700 Subject: [PATCH] add concat function --- README.md | 1 + src/Lib/Collections.php | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 69c86ba..af99a55 100644 --- a/README.md +++ b/README.md @@ -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). 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). +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. 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. diff --git a/src/Lib/Collections.php b/src/Lib/Collections.php index a2e0d7e..15da36f 100644 --- a/src/Lib/Collections.php +++ b/src/Lib/Collections.php @@ -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, 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, 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, function (Seq $a, ...$b) { $data = $a->getData();