2017 © Pedro Peláez
 

library php-monadic

Monadic type programming in PHP

image

yshnb/php-monadic

Monadic type programming in PHP

  • Monday, June 22, 2015
  • by yshnb
  • Repository
  • 1 Watchers
  • 2 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

php-monadic

library for monad pattern in PHP., (*1)

type

this library implements below type (class), (*2)

  • Identity
  • Maybe
  • Either
  • ListLike(List)

Usage

Install

you can install from packagist.org., (*3)

composer require yshnb/php-monadic:dev-master

Examples

Identity

Identity is a monad type, which simply wrap any value., (*4)

<?php

use Monadic\Type\Identity;

$foo = 1;
$identity = Identity::unit($foo)->bind(function($val) {
    return Identity::unit($val * 2);
});
echo $identity->get(); // 2

Maybe

<?php

use Monadic\Type\Maybe;

$foo = 1;
$maybe = Maybe::unit($foo)->bind(function($val) {
    return Maybe::unit($val + 1);
});
echo $maybe->get(); // 2

$maybe = $maybe->bind(function($val) {
    return Maybe::unit();
});
echo $maybe->get(); // null

Either

<?php

use Monadic\Type\Either\Right;
use Monadic\Type\Either\Left;

$foo = 1;
$either = Right::unit($foo)->bind(function($val) {
    return Left::unit($val + 1);
})->left(function($val) {
    return Right::unit($val + 1);
})->left(function($val) {
    // not executed
    return Right::unit($val + 1);
});
echo $either->get(); // 3

ListLike

ListLike represents list type (class). Because list is a reserved word in PHP, this type is called ListLike., (*5)

<?php

use Monadic\Type\ListLike;

$listLike = ListLike::unit(1,2,3);
$listLike = $listLike->bind(function($val) {
    return ListLike::unit($val * 2);
});
echo $listLike[0]; // 3
echo $listLike[1]; // 4
echo $listLike[2]; // 6

The Versions

22/06 2015

dev-master

9999999-dev

Monadic type programming in PHP

  Sources   Download

MIT

The Development Requires

by Yoshinobu Wakamatsu