From ddac856ed475d62726b610bc10cae0917f46393a Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Mon, 8 Jun 2020 10:02:59 +0700 Subject: [PATCH] add functions second, penult --- src/Lib/Collections.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Lib/Collections.php b/src/Lib/Collections.php index 9614b28..48548b1 100644 --- a/src/Lib/Collections.php +++ b/src/Lib/Collections.php @@ -70,13 +70,19 @@ class Collections implements ILib 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 + )); + $env->set('last', new CoreFunc('last', 'Return the last element of a sequence or null.', 1, 1, function (Seq $a) { - if ($a->count() == 0) { - return null; - } + return $a->getData()[$a->count() - 1] ?? null; + } + )); - return $a->getData()[$a->count() - 1]; + $env->set('penult', new CoreFunc('penult', 'Return the second last element of a sequence or null.', 1, 1, + function (Seq $a) { + return $a->getData()[$a->count() - 2] ?? null; } ));