use Widmogrod\FantasyLand\Foldable;
use Widmogrod\Primitive\Listt;
use Widmogrod\Primitive\ListtCons;
-use Widmogrod\Primitive\ListtNil;
const fromIterable = 'Widmogrod\Functional\fromIterable';
});
}
+/**
+ * @var callable
+ */
const fromValue = 'Widmogrod\Functional\fromValue';
function fromValue($value): Listt
function fromNil(): Listt
{
- return new ListtNil();
+ return ListtCons::mempty();
}
/**
}, fromNil(), $xs);
}
+/**
+ * @var callable
+ */
const prepend = 'Widmogrod\Functional\prepend';
/**
})(...func_get_args());
}
+/**
+ * @var callable
+ */
const append = 'Widmogrod\Functional\append';
/**
}
/**
+ * @var callable
+ */
+const head = 'Widmogrod\Functional\head';
+
+/**
* head :: [a] -> a
*
* Extract the first element of a list, which must be non-empty.
}
/**
+ * @var callable
+ */
+const tail = 'Widmogrod\Functional\tail';
+
+/**
* tail :: [a] -> [a]
*
* Extract the elements after the head of a list, which must be non-empty.
}
/**
+ * @var callable
+ */
+const length = 'Widmogrod\Functional\length';
+
+/**
* length :: Foldable t => t a -> Int
*
* Returns the size/length of a finite structure as an Int.
use Widmogrod\Primitive\Listt;
/**
+ * @var callable
+ */
+const take = 'Widmogrod\Functional\take';
+
+/**
* take :: Int -> [a] -> [a]
*
* take n, applied to a list xs, returns the prefix of xs of length n, or xs itself if n > length xs:
return fromNil();
}
- if ($n > length($xs)) {
- return $xs;
- }
-
try {
return prepend(head($xs), take($n - 1, tail($xs)));
} catch (EmptyListError $e) {
}
/**
+ * @var callable
+ */
+const drop = 'Widmogrod\Functional\drop';
+
+/**
* drop :: Int -> [a] -> [a]
*
* drop n xs returns the suffix of xs after the first n elements, or [] if n > length xs:
return $xs;
}
- if ($n > length($xs)) {
- return fromNil();
- }
-
try {
return drop($n - 1, tail($xs));
} catch (EmptyListError $e) {
}
/**
+ * @var callable
+ */
+const splitAt = 'Widmogrod\Functional\splitAt';
+
+/**
* splitAt :: Int -> [a] -> ([a], [a])
*
* splitAt n xs returns a tuple where first element is xs prefix of length n and second element is the remainder of the list:
+ *
+ * @param int $n
+ * @param Listt $xs
+ * @return array
*/
-function splitAt()
+function splitAt(int $n, Listt $xs = null)
{
- // TODO
+ return curryN(2, function (int $n, Listt $xs): array {
+ return [take($n, $xs), drop($n, $xs)];
+ })(...func_get_args());
}
/**
+ * @var callable
+ */
+const takeWhile = 'Widmogrod\Functional\takeWhile';
+
+/**
* takeWhile :: (a -> Bool) -> [a] -> [a]
*
* takeWhile, applied to a predicate p and a list xs, returns the longest prefix (possibly empty) of xs of elements that satisfy p:
}
/**
+ * @var callable
+ */
+const dropWhile = 'Widmogrod\Functional\dropWhile';
+
+/**
* dropWhile :: (a -> Bool) -> [a] -> [a]
*
* dropWhile p xs returns the suffix remaining after takeWhile p xs:
}
/**
+ * @var callable
+ */
+const span = 'Widmogrod\Functional\span';
+
+/**
* span :: (a -> Bool) -> [a] -> ([a], [a])
*
* span, applied to a predicate p and a list xs, returns a tuple where first element is longest prefix (possibly empty) of xs of elements that satisfy p and second element is the remainder of the list:
}
/**
+ * @var callable
+ */
+const breakk = 'Widmogrod\Functional\breakk';
+
+/**
* break :: (a -> Bool) -> [a] -> ([a], [a])
*
* break, applied to a predicate p and a list xs, returns a tuple where first element is longest prefix (possibly empty) of xs of elements that do not satisfy p and second element is the remainder of the list:
use Widmogrod\Primitive\Listt;
use function Widmogrod\Functional\fromIterable;
use function Widmogrod\Functional\fromNil;
+use function Widmogrod\Functional\repeat;
use function Widmogrod\Functional\take;
class TakeTest extends \PHPUnit_Framework_TestCase
$e = print_r($expected->extract(), true);
$this->assertTrue(
- $result->equals($expected),
- "$e != $r"
+ $result->equals($expected)
);
}
'$n' => 3000,
'$expected' => fromIterable([1, 2, 3, 4, 5]),
],
+ 'should return part of infinite list' => [
+ '$a' => repeat('a'),
+ '$n' => 3,
+ '$expected' => fromIterable(['a', 'a', 'a']),
+ ],
];
}
}