return curryN(2, function (callable $predicate, Foldable $list) {
return reduce(function (Listt $list, $x) use ($predicate) {
return $predicate($x)
- ? ListtCons::of(function () use ($list, $x) {
+ ? new ListtCons(function () use ($list, $x) {
return [$x, $list];
}) : $list;
}, fromNil(), $list);
function iterate(callable $fn, $a = null)
{
return curryN(2, function (callable $fn, $a): Listt {
- return ListtCons::of(function () use ($fn, $a) {
+ return new ListtCons(function () use ($fn, $a) {
return [$a, iterate($fn, $fn($a))];
});
})(...func_get_args());
*/
function repeat($a)
{
- return ListtCons::of(function () use ($a, &$list) {
+ return new ListtCons(function () use ($a, &$list) {
return [$a, repeat($a)];
});
}
return cycle($xs);
}
- return ListtCons::of(function () use ($xs, $cycled, $cycle) {
+ return new ListtCons(function () use ($xs, $cycled, $cycle) {
return [head($cycled), $cycle($xs, tail($cycled))];
});
};
return fromNil();
}
- return ListtCons::of(function () use ($i) {
+ return new ListtCons(function () use ($i) {
return [
$i->current(),
fromSnapshotIterator($i->snapshot())
*/
function fromValue($value): Listt
{
- return ListtCons::of(function () use ($value) {
- return [$value, fromNil()];
- });
+ return ListtCons::of($value);
}
/**
*
* @return Listt
*/
-function concat(Foldable $xs)
+function concat(Foldable $xs): Listt
{
- return foldr(function ($x, Listt $y) {
+ return foldr(function (Foldable $x, Listt $y) {
return foldr(prepend, $y, $x);
}, fromNil(), $xs);
}
function prepend($x, Listt $xs = null)
{
return curryN(2, function ($x, Listt $xs): Listt {
- return ListtCons::of(function () use ($x, $xs) {
+ return new ListtCons(function () use ($x, $xs) {
return [$x, $xs];
});
})(...func_get_args());
return fromNil();
}
- return $xs::of(function () use ($n, $xs) {
+ return new $xs(function () use ($n, $xs) {
return [head($xs), take($n - 1, tail($xs))];
});
})(...func_get_args());
$x = head($xs);
$y = head($ys);
- return ListtCons::of(function () use ($x, $y, $xs, $ys) {
+ return new ListtCons(function () use ($x, $y, $xs, $ys) {
return [
[$x, $y],
zip(tail($xs), tail($ys))
[$x, $y] = head($xs);
return [
- ListtCons::of(function () use ($x, $xs) {
+ new ListtCons(function () use ($x, $xs) {
return [$x, unzip(tail($xs))[0]];
}),
- ListtCons::of(function () use ($y, $xs) {
+ new ListtCons(function () use ($y, $xs) {
return [$y, unzip(tail($xs))[1]];
}),
];
{
return $this->value;
}
+
+ /**
+ * foldl _ z Nothing = z
+ * foldl f z (Just x) = f z x
+ *
+ * @inheritdoc
+ */
+ public function reduce(callable $function, $accumulator)
+ {
+ return $function($accumulator, $this->value);
+ }
}
interface Maybe extends
FantasyLand\Monad,
+ FantasyLand\Foldable,
Common\ValueOfInterface,
FantasyLand\Monoid
{
{
return null;
}
+
+ /**
+ * foldl _ z Nothing = z
+ * foldl f z (Just x) = f z x
+ *
+ * @inheritdoc
+ */
+ public function reduce(callable $function, $accumulator)
+ {
+ return $accumulator;
+ }
}
{
public const of = 'Widmogrod\Primitive\ListtCons::of';
+ /**
+ * @var callable
+ */
private $next;
/**
*/
public static function of($value)
{
- return new static($value);
+ return new self(function () use ($value): array {
+ return [$value, self::mempty()];
+ });
}
public function __construct(callable $next)
*/
public function map(callable $transformation): FantasyLand\Functor
{
- return self::of(function () use ($transformation) {
+ return new self(function () use ($transformation) {
[$head, $tail] = $this->headTail();
return [$transformation($head), $tail->map($transformation)];
}
if ($value instanceof self) {
- return self::of(function () use ($value) {
+ return new self(function () use ($value) {
[$x, $xs] = $this->headTail();
return [$x, $xs->concat($value)];
{
use Common\PointedTrait;
- public const of = 'Widmogrod\Primitive\Listt::of';
+ public const of = 'Widmogrod\Primitive\ListtConst::of';
public function __construct()
{
namespace test\Functional;
use Widmogrod\Functional as f;
+use Widmogrod\Monad\Maybe\Just;
+use Widmogrod\Monad\Maybe\Nothing;
class ConcatTest extends \PHPUnit\Framework\TestCase
{
3
]),
],
+ 'Just of lists' => [
+ '$array' => Just::of(f\fromIterable(['a', 1, 3])),
+ '$expected' => f\fromIterable(['a', 1, 3]),
+ ],
+ 'Nothing of lists' => [
+ '$array' => Nothing::mempty(),
+ '$expected' => f\fromNil()
+ ],
];
}
}