Skip to content

Commit

Permalink
Merge branch 'tune'
Browse files Browse the repository at this point in the history
  • Loading branch information
nishimura committed Jul 9, 2016
2 parents 174ff12 + 6c1c87a commit 252c2db
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
14 changes: 12 additions & 2 deletions src/Laiz/Func/Either.php
Expand Up @@ -27,18 +27,28 @@ abstract public function either(callable $left, callable $right);
use function Laiz\Func\f;

function Left(...$args){
if (count($args) === 1)
return new Either\Left(...$args);

return f(function($a){
return new Either\Left($a);
}, ...$args);
}
function Right(...$args){
if (count($args) === 1)
return new Either\Right(...$args);

return f(function($a){
return new Either\Right($a);
}, ...$args);
}

function either(...$args){
return f(function(callable $left, callable $right, Either $a){
$f = function(callable $left, callable $right, Either $a){
return $a->either($left, $right);
}, ...$args);
};
if (count($args) === 3)
return $f(...$args);
else
return f($f, ...$args);
}
8 changes: 6 additions & 2 deletions src/Laiz/Func/Functor.php
Expand Up @@ -18,9 +18,13 @@ public static function fmap(callable $f, $a);
// (<$>) :: Functor f => (a -> b) -> f a -> f b
function fmap(...$args)
{
return f(function(callable $f, $a){
$f = function(callable $f, $a){
return Loader::callInstanceMethod($a, 'fmap', $f, $a);
}, ...$args);
};
if (count($args) === 2)
return $f(...$args);
else
return f($f, ...$args);
}

// (<$) :: Functor f => a -> f b -> f a
Expand Down
2 changes: 1 addition & 1 deletion src/Laiz/Func/Monad.php
Expand Up @@ -17,7 +17,7 @@ public static function ret($a);

function bind(...$args) {
$f = function($m, callable $f){
$ret = Loader::callInstanceMethod($m, 'bind', $m, f($f));
$ret = Loader::callInstanceMethod($m, 'bind', $m, $f);
if ($ret instanceof Any)
$ret = $ret->cast($m);
return $ret;
Expand Down
24 changes: 18 additions & 6 deletions src/Laiz/Func/functions.php
Expand Up @@ -82,28 +82,40 @@ function foldl(...$args)

function foldr(...$args)
{
return f(function($f, $a, $b){
$f = function($f, $a, $b){
$ret = $a;
for ($i = count($b) - 1; $i >= 0; $i--){
$ret = $f($ret, $b[$i]);
}
return $ret;
}, ...$args);
};
if (count($args) === 3)
return $f(...$args);
else
return f($f, ...$args);
}

function colon(...$args)
{
return f(function($a, $as){
$f = function($a, $as){
array_unshift($as, $a);
return $as;
}, ...$args);
};
if (count($args))
return $f(...$args);
else
return f($f, ...$args);
}
function colonr(...$args)
{
return f(function($a, $as){
$f = function($a, $as){
$as[] = $a;
return $as;
}, ...$args);
};
if (count($args) === 2)
return $f(...$args);
else
return f($f, ...$args);
}

function concat(...$args)
Expand Down

0 comments on commit 252c2db

Please sign in to comment.