This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function either(...$args) { | |
| return self::curry(function(callable $f, callable $g, Either $e) { | |
| return match (true) { | |
| $e->isLeft() => $f($e->value), | |
| default => $g($e->value) | |
| }; | |
| })(...$args); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -module(gregorian). | |
| -export([ | |
| days/0, | |
| months/0, | |
| dayTuples/0, | |
| monthTuples/0, | |
| dayOfWeek/1, | |
| monthOfYear/1, | |
| isLeap/1, | |
| daysOfMonth/2, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -module(hello). | |
| -export([ | |
| hello/0, | |
| ]). | |
| %% Hello world | |
| hello() -> | |
| io:format("Hello, world!~n"). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function maybe(...$args) { | |
| return curry(fn(mixed $v, callable $f, Maybe $m) => | |
| match (true) { | |
| $m->isNothing() => $v, | |
| default => call_user_func($f, $m->get()) | |
| } | |
| )(...$args); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Maybe { | |
| public function __construct(private $value = null) { } | |
| public function isNothing(): bool { | |
| return $this->value === null; | |
| } | |
| public function isJust(): bool { | |
| return !$this->isNothing(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function _getProp(string $prop, object $obj) { | |
| $r = new ReflectionClass($obj); | |
| $hasProp = $r->hasProperty($prop); | |
| $getter = 'get' . ucfirst($prop); | |
| $hasGetter = $r->hasMethod($getter); | |
| return match (true) { | |
| $hasProp && $r->getProperty($prop)->isPublic() => $obj->$prop, | |
| $hasGetter && $r->getMethod($getter)->isPublic() => $obj->$getter(), | |
| default => null | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function prop(...$args) { | |
| return self::curry(fn(string $prop, array $obj) => match (true) { | |
| is_array($obj) && array_key_exists($prop, $obj) => $obj[$prop], | |
| default => null | |
| })(...$args); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function init(array | string $xs) { | |
| return match (true) { | |
| is_string($xs) && !empty($xs) => substr($xs, 0, -1), | |
| is_array($xs) && !empty($xs) => array_slice($xs, 0, -1), | |
| default => null | |
| }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function last(array | string $xs) { | |
| return match (true) { | |
| is_string($xs) && !empty($xs) => substr($xs, -1), | |
| is_array($xs) && !empty($xs) => $xs[count($xs) - 1], | |
| default => null | |
| }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function drop(...$args) { | |
| return curry(fn($n, $xs) => match (true) { | |
| is_string($xs) => substr($xs, $n), | |
| is_array($xs) => array_slice($xs, $n), | |
| default => null | |
| })(...$args); | |
| } |
NewerOlder