return State::of(function (Listt $state) use ($a) {
return [
$a->next,
- f\append($state, Listt::of('PutStrLn'))
+ f\append($state, f\fromValue('PutStrLn'))
];
});
},
return State::of(function (Listt $state) use ($a) {
return [
($a->processor)('demo'),
- f\append($state, Listt::of('GetLine'))
+ f\append($state, f\fromValue('GetLine'))
];
});
},
return State::of(function (Listt $state) use ($a) {
return [
ff\Pure::of('exit'),
- f\append($state, Listt::of('ExitSuccess'))
+ f\append($state, f\fromValue('ExitSuccess'))
];
});
},
namespace example;
+use function Widmogrod\Functional\fromValue;
use Widmogrod\Primitive\Listt;
class ListComprehensionWithMonadTest extends \PHPUnit_Framework_TestCase
->bind(function ($n) {
return Listt::of(['a', 'b'])
->bind(function ($x) use ($n) {
- return [[$n, $x]];
+ return fromValue([$n, $x]);
});
});
namespace example;
use Widmogrod\Monad\Maybe;
-use Widmogrod\Monad\Maybe as m;
+use function Widmogrod\Monad\Maybe\just;
+use const Widmogrod\Monad\Maybe\maybeNull;
+use function Widmogrod\Monad\Maybe\nothing;
use Widmogrod\Primitive\Listt;
use Widmogrod\Functional as f;
// $get :: String a -> [b] -> Maybe b
$get = f\curryN(2, function ($key, $array) {
return isset($array[$key])
- ? m\just($array[$key])
- : m\nothing();
+ ? just($array[$key])
+ : nothing();
});
$listOfFirstImages = f\pipeline(
Listt::of,
- f\map(m\maybeNull),
- f\bind(f\bind($get('meta'))),
- f\bind(f\bind($get('images'))),
- f\bind(f\bind($get(0))),
- f\join
+ f\map(maybeNull),
+ f\map(f\bind($get('meta'))),
+ f\map(f\bind($get('images'))),
+ f\map(f\bind($get(0)))
);
$result = $listOfFirstImages($data);
$this->assertEquals(
- Listt::of([m\just('//first.jpg'), m\just('//third.jpg'), m\nothing()]),
+ Listt::of([just('//first.jpg'), just('//third.jpg'), nothing()]),
$result
);
}
$get = function ($key) {
return f\bind(function ($array) use ($key) {
return isset($array[$key])
- ? m\just($array[$key])
- : m\nothing();
+ ? just($array[$key])
+ : nothing();
});
};
$result = Listt::of($data)
->map(Maybe\maybeNull)
- ->bind($get('meta'))
- ->bind($get('images'))
- ->bind($get(0));
+ ->map($get('meta'))
+ ->map($get('images'))
+ ->map($get(0));
$this->assertEquals(
- Listt::of([m\just('//first.jpg'), m\just('//third.jpg'), m\nothing()]),
+ Listt::of([just('//first.jpg'), just('//third.jpg'), nothing()]),
$result
);
}
{
// $makeMaybeMonoid :: string -> Maybe Listt string
$makeMaybeMonoid = function ($val): Maybe {
- return maybeNull($val)->map(Listt::of);
+ return maybeNull($val)->map(f\fromValue);
};
// $names :: array Maybe Listt string
use Widmogrod\FantasyLand\Foldable;
use Widmogrod\Primitive\Listt;
+const fromIterable = 'Widmogrod\Functional\fromIterable';
+
function fromIterable(iterable $i): Listt
{
return Listt::of(array_map(identity, $i));
}
+const fromValue = 'Widmogrod\Functional\fromValue';
+
+function fromValue($value): Listt
+{
+ return Listt::of([$value]);
+}
+
/**
* @var callable
*/
public const of = 'Widmogrod\Primitive\Listt::of';
- /**
- * @param array $value
- */
- public function __construct($value)
+ public function __construct(iterable $value)
{
- $givenType = is_object($value) ? get_class($value) : gettype($value);
- assert(is_iterable($value), "Not iterable value given $givenType");
-
$this->value = $value;
}
{
return [
'Just' => [
- '$x' => Just::of(Listt::of(1)),
- '$y' => Just::of(Listt::of(2)),
- '$z' => Just::of(Listt::of(3))
+ '$x' => Just::of(Listt::of([1])),
+ '$y' => Just::of(Listt::of([2])),
+ '$z' => Just::of(Listt::of([3]))
],
'Nothing' => [
'$x' => Nothing::mempty(),
use Widmogrod\FantasyLand\Functor;
use Widmogrod\FantasyLand\Monoid;
use Widmogrod\Functional as f;
+use const Widmogrod\Functional\fromValue;
use Widmogrod\Helpful\ApplicativeLaws;
use Widmogrod\Helpful\FunctorLaws;
use Widmogrod\Helpful\MonadLaws;
{
MonadLaws::test(
f\curryN(3, [$this, 'assertEquals']),
- f\curryN(1, Listt::of),
+ f\curryN(1, fromValue),
$f,
$g,
$x
public function provideData()
{
$addOne = function ($x) {
- return Listt::of($x + 1);
+ return Listt::of([$x + 1]);
};
$addTwo = function ($x) {
- return Listt::of($x + 2);
+ return Listt::of([$x + 2]);
};
return [
{
return [
'Listt' => [
- '$pure' => Listt::of,
- '$u' => Listt::of(function () {
+ '$pure' => fromValue,
+ '$u' => Listt::of([function () {
return 1;
- }),
- '$v' => Listt::of(function () {
+ }]),
+ '$v' => Listt::of([function () {
return 5;
- }),
- '$w' => Listt::of(function () {
+ }]),
+ '$w' => Listt::of([function () {
return 7;
- }),
+ }]),
'$f' => function ($x) {
return $x + 400;
},
namespace test\Primitive;
+use Eris\TestTrait;
use Widmogrod\Functional as f;
use Widmogrod\Helpful\MonoidLaws;
use Widmogrod\Helpful\SetoidLaws;
class ProductTest extends \PHPUnit_Framework_TestCase
{
+ use TestTrait;
/**
* @dataProvider provideSetoidLaws
*/
private function randomize()
{
- return Product::of(random_int(-100000000, 100000000));
+ usleep(10);
+
+ return Product::of(random_int(-1000, 1000));
}
public function provideSetoidLaws()