Giter VIP home page Giter VIP logo

fn's People

Contributors

andres-ml avatar ragboyjr avatar sa-tasche avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

fn's Issues

Some functions are not documented

Hi,

I noticed some functions (not, id, maybe others) are not listed/documented (I guess they have no tests and thus no documentation is automatically generated).

On a related note, I've seen some libraries use complement for function negation and not as a boolean negation; in case you wanted to rename the current not before documenting it.

Proposal: applyTo() utility function

Hello and thank you for writing this library.

Whenever I'm defining a pipeline of functions to be applied immediately to a starting value, instead of being given a name and saved for later, I use the following utility function:

/**
 * Accept an input and some callables, then apply the callables in order.
 *
 * @param   $input      Input to the computation.
 * @param   $callables  Any number of callables to be composed using pipe().
 * @return              Final result of the computation.
 */
function applyTo($input, callable ...$callables) {
    return f\pipe(...$callables)($input);
}

This enables a Bash-like, left-to-right / top-to-bottom style of pipeline definition and application, for those (like me) who find it more readable than the backwards style of compose():

$res = applyTo([1,2,3,4],            // <- input
    c\filter(fn ($x) => $x > 2),   // <- first step
    c\map(fn ($x) => $x * 3),      // ...
    c\toArray,                     // <- last step
);

assert($res == [9, 12]);

Here is a more complex example, showing how to use one applyTo() inside another:

$pages = [
    (object)['slug'=>'about', 'name'=>'About Us'],
    (object)['slug'=>'about', 'name'=>'About You'],
    (object)['slug'=>'news',  'name'=>'Latest News'],
];

$duplicate = applyTo($pages,
    c\groupBy(fn ($page) => $page->slug),
    c\filter(fn ($group) => count($group) > 1),
    c\map(fn ($pages) =>
        "Pages with slug '{$pages[0]->slug}': " . applyTo($pages,
            c\map(fn ($page) => "({$page->name})"),
            c\join(", "),
        )
    ),
    c\join(". "),
);

assert($duplicate == "Pages with slug 'about': (About Us), (About You)");

Do you have any suggestions / critiques? Would you accept a pull request for this function?

I also thought about other calling styles, such as curried: applyTo($input)($fn1, $fn2...) or array: applyTo($input, [$fn1, $fn2...]) but ultimately I decided to keep things simple.

Add Psalm Support

https://psalm.dev

Psalm is an incredibly powerful static analyzer that enables a level of type safety in a codebase that is typically very difficult to achieve with php.

Adding psalm typings for this codebase would be a great benefit for psalm users who also use this library. It could also be seen as a selling point for potential users of this library.

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.