dev-master
9999999-dev http://github.com/ustream/option
MIT
The Requires
- php >=5.3.0
by Peter Wilcsinszky
option monad
0.1.0
0.1.0.0 http://github.com/ustream/option
MIT
The Requires
- php >=5.3.0
by Peter Wilcsinszky
option monad
This is a PHP interpretation of the Option type with monadic structure. We beleive that the tests document the behavior and even though the implementation is similar to php-option there are some important differences we need to highlight., (*1)
We found it useful, that the monadic computation may decide to return with an arbitrary type, that will be converted to an Option if necessary., (*2)
The rules for Some::apply
is as follows:, (*3)
Wrapping a non Option type:, (*4)
$result = someMethodReturningAnOptionalString($params) ->apply( function ($result) { return $result . ' is a string!'; }) ->getOrElse('default');
No need to explicitly return with None:, (*5)
$module = $dataMaybe ->apply( function ($data) { if (isset($data['moduleConfig'])) { if (count($data['moduleConfig']) == 1) { return key($data['moduleConfig']); } elseif (count($data['moduleConfig']) > 1) { return 'full'; } } } ) ->getOrElse('unknown');
We use None::otherwise
as the mirror of Some::apply
., (*6)
Combining apply and otherwise:, (*7)
header( locate($_SERVER["DOCUMENT_URI"]) ->apply( function ($location) { statsdIncrement('302'); return 'Location: ' . $location; } ) ->otherwise( function () { statsdIncrement('404'); } ) ->getOrElse("HTTP/1.0 404 Not Found") );
Creating a chain of fallback functions:, (*8)
return Ustream\Option\None::create() ->otherwise($this->ustreamPicture($userId, $size)) ->otherwise($this->facebookPictureFromSession($facebookNetwork, $isSecureAccess)) ->getOrElse($this->naPicture($size));
MIT
option monad
MIT
option monad