add function: slice

This commit is contained in:
Pekka Laiho 2020-10-17 13:53:09 +07:00
parent a7e4524470
commit 9209b8dd92
2 changed files with 7 additions and 0 deletions

View File

@ -163,6 +163,7 @@ penult | `(penult [1 2 3 4])` | `3` | Return the second-last element of a seque
last | `(last [1 2 3 4])` | `4` | Return the last element of a sequence. last | `(last [1 2 3 4])` | `4` | Return the last element of a sequence.
head | `(head [1 2 3 4])` | `[1 2 3]` | Return new sequence which contains all elements except the last. head | `(head [1 2 3 4])` | `[1 2 3]` | Return new sequence which contains all elements except the last.
tail | `(tail [1 2 3 4])` | `[2 3 4]` | Return new sequence which contains all elements except the first. tail | `(tail [1 2 3 4])` | `[2 3 4]` | Return new sequence which contains all elements except the first.
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).
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.

View File

@ -98,6 +98,12 @@ class Collections implements ILib
} }
)); ));
$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) {
return $a::new(array_slice($a->getData(), $offset, $length));
}
));
// Manipulate list // Manipulate list
$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,