2017 © Pedro PelĂĄez
 

library functional

Functional Programming for PHP no loop behind

image

sergiors/functional

Functional Programming for PHP no loop behind

  • Tuesday, November 1, 2016
  • by sergiors
  • Repository
  • 3 Watchers
  • 22 Stars
  • 75 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Functional

Scrutinizer Code Quality Code Coverage Build Status, (*1)

Motivation

I did create this project to explore Functional Programming.
This project doesn't use any loop!, (*2)

Install

composer require sergiors/functional "dev-master"

How to use

use Sergiors\Functional as F;

$addFourNumbers = funciton ($a, $b, $c, $d) {
    return $a + $b + $c + $d;
};

$partialAddFourNumbers = F\partial($addFourNumbers);
$f = $partialAddFourNumbers(1, 2);
$g = $f(3);

echo $g(4); // => 10

You can hold native functions with Sergiors\Funcional\hold., (*3)

use Sergiors\Functional as F;

$implode = F\hold('implode', ',');
echo $implode(['a', 'b']); // => a,b

// something more complex
$pipe = F\pipe(
    F\hold('array_slice', F\_, 2),
    F\hold('array_map', function ($x) {
        return $x * 2;
    }),
    F\hold('implode', ',')
);

echo $pipe([10, 20, 30, 40, 50]); // => 60,80,100

Sergiors\Functional\_ is the placeholder for payload. If you don't set the placeholder, the payload will be last argument., (*4)

Collection

You can wrap an array with Sergiors\Functional\Collection to use fluent interface., (*5)

use Sergiors\Functional\Collection;

$ls1 = new Collection([1, 2, 3, 4, 5, 6]);

$ls2 = $ls1->filter(function ($x) {
    return $x > 2;
})->map(function ($x) {
    return $x + 1;
});

print_r($ls1->toArray()); // => [1, 2, 3, 4, 5, 6]
print_r($ls2->toArray()); // => [4, 5, 6, 7]

API

All functions are automatically applied partial application., (*6)

always()
append()
compose()
concat()
diff()
drop()
each()
equals()
filter()
flatten()
get()
getin()
gt()
gte()
has()
head()
hold()
id()
ifelse()
last()
lt()
lte()
map()
not()
partial()
pipe()
prop()
prepend()
reduce()
sort()
tail()
take()
takewhile()
Collection::concat()
Collection::filter()
Collection::map()
Collection::each()
Collection::prepend()
Collection::append()
Collection::reduce()
Collection::count()
Collection::getIterator()
Collection::toArray()

License

MIT, (*7)

The Versions

01/11 2016

dev-php7

dev-php7

Functional Programming for PHP no loop behind

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Sérgio Rafael Siqueira

04/07 2016

dev-master

9999999-dev

Functional Programming for PHP no loop behind

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sérgio Rafael Siqueira