From 4b76392ab7f8c108288c4ef26febf0ae59d8033d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lajos=20M=C3=A9sz=C3=A1ros?= Date: Wed, 4 Aug 2021 11:32:53 +0200 Subject: [PATCH] changed the interpretation of toIndex in A::slice() --- README.md | 2 ++ src/A.php | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ea5bf4c..19e770b 100644 --- a/README.md +++ b/README.md @@ -514,6 +514,8 @@ $result = A::any(fn($x) => $x % 5 === 0, $data);
A::slice +#### Returns the elements of the given list from fromIndex (inclusive) to toIndex (exclusive) +
diff --git a/src/A.php b/src/A.php index 686d0e5..a76aa03 100644 --- a/src/A.php +++ b/src/A.php @@ -242,7 +242,7 @@ public static function tail(array $data): array { // A::init([1, 2, 3]) -> [1, 2] public static function init(array $data): array { - return self::slice(0, -1, $data); + return self::slice(0, self::length($data) - 1, $data); } // A::filter(x => x % 2 == 0, [1, 2, 3, 4, 5]) -> [2, 4] @@ -323,7 +323,7 @@ public static function contains($value, array $data): bool // A::slice(2, 4, [1, 2, 3, 4, 5]) -> [3, 4, 5] public static function slice(int $fromIndex, int $toIndex, array $data): array { - return array_slice($data, $fromIndex, $toIndex); + return array_slice($data, $fromIndex, $toIndex - $fromIndex); } // A::join('-', ['a', 'b', 'c']) -> 'a-b-c'