2017 © Pedro Peláez
 

library option

image

ustream/option

  • Monday, March 11, 2013
  • by oker1
  • Repository
  • 26 Watchers
  • 8 Stars
  • 53 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Ustream Option monad Build Status

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)

There is apply instead of map and flatMap

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)

  • If the return type is not an Option it will be wrapped in Some
  • If there is no return value (actually the return value is null) it will be translated to None
  • If the return type is an Option it will be passed through (the same as for flatMap)

Examples

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');

There is otherwise instead of orElse with LazyOption

We use None::otherwise as the mirror of Some::apply., (*6)

Examples

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));

The Versions

11/03 2013

dev-master

9999999-dev http://github.com/ustream/option

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Peter Wilcsinszky

option monad

11/03 2013

0.1.0

0.1.0.0 http://github.com/ustream/option

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Peter Wilcsinszky

option monad