Giter VIP home page Giter VIP logo

functional's Introduction

Functional

Scrutinizer Code Quality Code Coverage Build Status

Motivation

I did create this project to explore Functional Programming.
This project doesn't use any loop!

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.

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.

Collection

You can wrap an array with Sergiors\Functional\Collection to use fluent interface.

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.

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

functional's People

Contributors

sergiors avatar haskellcamargo avatar

Stargazers

Leonardo Rifeli avatar

Watchers

James Cloos avatar Leonardo Rifeli avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.