Git Product home page Git Product logo

phugly's Introduction

Phugly: A curried functional programming library for PHP

Functional programming in PHP is pretty ugly, but at least it's possible.

composer require cullylarson/phugly

Install dependencies

$ composer install

Run tests

$ composer run tests

phugly's People

Contributors

cullylarson avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

phugly's Issues

Add a function that only does something if the value is not null

So, if value === null, just return the value (null), otherwise do something. It's sort of a poor-man's catamorphism, just assuming the the Nothing case is F\Id. It essentially assumes a Monad where null is Nothing and all other values are Just.

I'm not sure if it's a great idea to continue to avoid explicit Monads in this library, and keep making exceptions/work-arounds. Though it does make using the library really easy.

Would it be more useful to have another function for when the value is null, rather than assuming F\id? Maybe add a cata function for that?

The function will be something like:

// 'nn' means 'not null'. good name?
function nn(...$args) {
    $nn = curry(function($f, $x) {
        return $x === null
            ? $x
            : $f($x);
    });

    return call_user_func_array($nn, $args);
}

Add a `getRandom` function

Gets a random number of values from an array. Potential implementation:

$getRandom = curry(function($num, $xs) {
    if(empty($xs) || !is_array($xs) || count($xs) <= $num) return $xs;

    $results = [];
    for($i = 0; $i < $num; $i++) {
        $idx = rand(0, count($xs) - 1);

        $results []= array_splice($xs, $idx, 1)[0];
    }
    return $results;
});

`doAt` should really be named `mapAt`

It's mapping at an index, so mapAt is a more meaningful name.

At some point I'd like to refactor to use lenses throughout the library, so this "rename" might not be necessary. Or, mapAt could be the lens version? I don't really like the idea of having a bunch of old functions around. Maybe the lens version of the library could be a new major version and we can scrap the old function names if necessary.

Make `objProp` take an array key (like `getAt`)

E.g. objProp(['firstKey', 'secondKey']). Now it only takes a string key, so cannot access nested objects. Ideally would replace objProp by expanding getAt to work with objects (c.f. #5). That way you wouldn't have to worry if an array is nested in an object nested in an array, etc.

Write documentation

Document the rationale of the library (why it is, how it is). Document all of the functions. Include examples.

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.