From 318df648e88a492d0ddddd06e9b1424e31038f42 Mon Sep 17 00:00:00 2001 From: widmogrod Date: Wed, 20 Dec 2017 21:59:09 +0100 Subject: [PATCH] Introduce new functions tail, head, length --- src/Functional/listt.php | 133 ++++++++++++++++------------------------------- 1 file changed, 44 insertions(+), 89 deletions(-) diff --git a/src/Functional/listt.php b/src/Functional/listt.php index f757a38..7f2504d 100644 --- a/src/Functional/listt.php +++ b/src/Functional/listt.php @@ -91,92 +91,47 @@ function append(Listt $a, Listt $b = null) })(...func_get_args()); } -///** -// * head :: [a] -> a -// * -// * Extract the first element of a list, which must be non-empty. -// * -// * @param Listt $l -// * @return mixed -// */ -//function head(Listt $l) -//{ -// return $l->head(); -//} -// -///** -// * tail :: [a] -> [a] -// * -// * Extract the elements after the head of a list, which must be non-empty. -// * -// * @param Listt $l -// * @return Listt -// */ -//function tail(Listt $l) -//{ -// return $l->tail(); -//} -// -///** -// * last :: [a] -> a -// * -// * Extract the last element of a list, which must be finite and non-empty. -// * -// * @param Listt $l -// */ -//function last(Listt $l) -//{ -// // TODO -//} -// -///** -// * init :: [a] -> [a] -// * -// * Return all the elements of a list except the last one. The list must be non-empty. -// * -// * @param Listt $l -// */ -//function init(Listt $l) -//{ -// // TODO -//} -// -// -///** -// * length :: Foldable t => t a -> Int -// * -// * Returns the size/length of a finite structure as an Int. -// * The default implementation is optimized for structures that are similar to cons-lists, -// * because there is no general way to do better. -// * -// * @param Listt $l -// * @return int -// */ -//function length(Listt $l): int -//{ -// // TODO -//} -// -///** -// * (!!) :: [a] -> Int -> a infixl 9 -// * -// * List index (subscript) operator, starting from 0. It is an instance of the more general genericIndex, which takes an index of any integral type. -// * -// * @param Listt $L -// */ -//function index(Listt $L, int $index) -//{ -// // TODO -//} -// -///** -// * reverse :: [a] -> [a] -// * -// * reverse xs returns the elements of xs in reverse order. xs must be finite. -// * -// * @param Listt $l -// */ -//function reverse(Listt $l) -//{ -// // TODO -//} +/** + * head :: [a] -> a + * + * Extract the first element of a list, which must be non-empty. + * + * @param Listt $l + * @return mixed + * @throws \Widmogrod\Primitive\EmptyListError + */ +function head(Listt $l) +{ + return $l->head(); +} + +/** + * tail :: [a] -> [a] + * + * Extract the elements after the head of a list, which must be non-empty. + * + * @param Listt $l + * @return Listt + * @throws \Widmogrod\Primitive\EmptyListError + */ +function tail(Listt $l) +{ + return $l->tail(); +} + +/** + * length :: Foldable t => t a -> Int + * + * Returns the size/length of a finite structure as an Int. + * The default implementation is optimized for structures that are similar to cons-lists, + * because there is no general way to do better. + * + * @param Foldable $t + * @return int + */ +function length(Foldable $t): int +{ + return $t->reduce(function ($len) { + return $len + 1; + }, 0); +} -- 2.11.0