GithubHelp home page GithubHelp logo

php-fp's Introduction

Functional Programming Helpers

Currying

A curryable function returns a new function when called with less arguments than the curryable function requires. The new function returned will have the arguments applied, and will also be a curryable function.

This programming pattern can be used to build up more complex functions from less complex functions.

e.g.

// Create a curryable function
$concat = fp\curry(function ($a, $b) { return $a . $b; });

// Create a new function with 'Mr. ' applied
$addTitle = $concat('Mr. ');

echo $addTitle('Spiers');
// Mr. Spiers

Composition

$h = fp\compose($f, $g);

Function composition will return a new function ($h) which will first apply the second function ($g), pass its result into the first ($f).

Usage

Turning normal functions into curryable functions

$map = fp\curry('array_map');

Create non-closure functions that are curryable

function _tag($tag, $text) {
    return "<$tag>$text</$tag>";
}

function tag(...$args) {
    return fp\curry('_tag')->__invoke(...$args);
}

// We now have a paragraph function
$p = tag('p');

// We now have a div function
$div = tag('div');

echo $div($p("Some text"));
// <div><p>Some text</p></div>

php-fp's People

Contributors

camspiers avatar

Stargazers

Nick Rawe avatar Nicholas Shanks avatar MattWilliams avatar John Kapolos avatar Adrian Smith avatar Andrius Každailis avatar Mohammed Irfan avatar Josh Swift avatar Roman Yarovoy avatar Hannes Forsgård avatar Nic avatar Stevie Mayhew avatar  avatar Dan Munro avatar Michael Curry avatar Rocco Palladino avatar Simon Holywell avatar

Watchers

 avatar André Philip avatar Adrian Smith avatar Nicholas Shanks avatar James Cloos avatar

Forkers

shitbangs slifin

php-fp's Issues

placeholder support

is it possible to have a curried function where you can choose which parameter(s) are going to be called next by supplying a place holder class instance instead of just evaluating left to right?

for example

function example($parameter, $data, $parameter){
    //returns stuff here
}
$parameter = 5;
$data = []; //dynamic data here
$curriedFunc = curry('example',$parameter,(new Placeholder),$parameter);
$result = $curriedFunc($data);

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.