Function composition.

Overview

igorw/compose

Function composition.

Allows you to stitch functions together to form a pipeline. This can be useful if you have to transform data in many steps and you want to describe those steps on a high level.

compose

Generally, function composition means taking two functions f and g, and producing a new function z, which applies f to the result of g.

z = compose(f, g)
; z(x) => f(g(x))

This library provides a compose function that does just this.

$z = igorw\compose($f, $g);
var_dump($z($x));

It supports an arbitrary number of functions to be composed via varargs.

$z = igorw\compose($f, $g, $h, $i);

The innermost function (the last one in the list) can take an arbitrary number of arguments, whereas the others may only take a single argument.

$z = igorw\compose($f, $g);
$z('a', 'b', 'c');
// => $f($g('a', 'b', 'c'))

pipeline

pipeline is the same as compose, but the arguments are reversed. This is more easy to read in some cases, because you can list the functions in the order they will be called.

It is quite similar to a unix pipe in that regard.

Examples

function transform_data($data) {
    return [
        'name' => $data['firstname'].' '.$data['lastname'],
    ];
}

$transformJson = igorw\pipeline(
    function ($json) { return json_decode($json, true); },
    'transform_data',
    'json_encode'
);

$json = <<<EOF
{"firstname": "Igor", "lastname": "Wiedler"}
{"firstname": "Beau", "lastname": "Simensen"}
EOF;

$list = explode("\n", $json);
$newList = array_map($transformJson, $list);
$newJson = implode("\n", $newList);

// =>
// {"name": "Igor Wiedler"}
// {"name": "Beau Simensen"}
You might also like...
A php sharex uploader with discord embed function/twitter card support

Sharex Uploader Simple Sharex Uploader with Discord embed function Download replace your data and upload to your sevrer

To run time/IO related unit tests (e.g., sleep function calls, database queries, API calls, etc) faster using Swoole.

To run time/IO related unit tests (e.g., sleep function calls, database queries, API calls, etc) faster using Swoole.

WordPress core test suite function and class declaration stubs for static analysis by PHPStan

WordPress Core Test Suite Stubs This package provides stub declarations for the WordPress Core Test Suite functions, classes and interfaces. These stu

Looks up which function or method a line of code belongs to

sebastian/code-unit-reverse-lookup Looks up which function or method a line of code belongs to. Installation You can add this library as a local, per-

Alternative to tmpfile() function

TmpFile Alternative to tmpfile() function. Installation You can install the latest version via Composer: composer require denisyukphp/tmpfile This pa

A helper package to flash a bootstrap alert to the browser via a Facade or a helper function.

Alert Box (Laravel) A helper package to flash a bootstrap alert to the browser via a Facade or a helper function. div class="alert alert-info fade in

A Sidecar function to run Inertia server-side rendering on Lambda.

Sidecar SSR for InertiaJS 🚨 This is currently very much in beta! You can see a fully working Jetstream + Inertia + Sidecar demo repo at hammerstonede

This Repository contains a custom Workflow for Alfred which provides the function to instantly search in the Magento 2 DevDocs
This Repository contains a custom Workflow for Alfred which provides the function to instantly search in the Magento 2 DevDocs

Introduction Add the custom search to your Alfred Workflow and have a quicker access to the Magento 2 DevDocs. Installation Just download the alfredwo

Composer Plugin for automatically including files for easing function usage in php.

Php Inc Php inc is a composer plugin for automatically including certain files into composer's autoload and autoload-dev files config. Given a set of

TWINS FUNCTION IN PHP

TWINS FUNCTION IN PHP During a test, I was faced with this problem of checking if two strings are twins. I couldn't finish the problem in time so I de

A magic memoization function
A magic memoization function

A magic memoization function This package contains a once function. You can pass a callable to it. Here's quick example: $myClass = new class() {

Shortest Path - have a function ShortestPath (strArr) take strArr which will be an array of strings which models a non-looping Graph.

Have the function ShortestPath(strArr) take strArr which will be an array of strings which models a non-looping Graph

🔒 Password Exposed Helper Function - Check if a password has been exposed in a data breach.
🔒 Password Exposed Helper Function - Check if a password has been exposed in a data breach.

🔒 Password Exposed Helper Function This PHP package provides a password_exposed helper function, that uses the haveibeenpwned.com API to check if a p

PHP Class Encoding featuring popular Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strings.
PHP Class Encoding featuring popular Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strings.

forceutf8 PHP Class Encoding featuring popular \ForceUTF8\Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strin

Extend Jenssegers/laravel-mongodb to support transaction function

Laravel Mongodb (Transactional support) Introduction Jensseger's laravel-mongodb extension package is very popular among Laravel developers, but it la

Laravel & MySQL, jQuery, Ajax, Bootstrap. Also, it's include email send function without any API.
Laravel & MySQL, jQuery, Ajax, Bootstrap. Also, it's include email send function without any API.

Rewards-Dacor Laravel & MySQL, jQuery, Ajax, Bootstrap. Also, it's include email send function without any API. [Live site link] ( https://rewardsdaco

WooCommerce function and class declaration stubs for static analysis.

WooCommerce Stubs This package provides stub declarations for WooCommerce functions, classes and interfaces. These stubs can help plugin and theme dev

Comments
  • Retain type hints when constructing a composed function

    Retain type hints when constructing a composed function

    This is useful if code you are passing a composed function to inspects the function with reflection to determine which parameters to pass to the function.

    An example would be the controller resolver in the Symfony HttpKernel through which Silex tries to inject the Application or the Request object if a type hint exists. So with this patch it becomes possible to use composed functions as callbacks for Silex routes.

    Long live eval.

    opened by naderman 0
  • `composer.json` doesn't give PHP version

    `composer.json` doesn't give PHP version

    Could you please use composer.json to state the supported versions of PHP?

    The software works in PHP 5.3 — I have confirmed this by reading src/compose.php, and by running the test suite under PHP 5.3.3.

    opened by ruafozy 0
Owner
Igor
Igor
Mail sending module for Mezzio and Laminas MVC with support for file attachment and template email composition

This module provides an easy and flexible way to send emails from Mezzio and Laminas MVC applications (formerly known as Zend Expressive and Zend MVC). It allows you to pre-configure emails and transports, and then send those emails at runtime.

null 82 Jan 16, 2022
ODM with inheritance and OOP composition for Laravel 5+

ODM with inheritance and OOP composition for Laravel 5+ Full Documentation | CHANGELOG LODM module is intended to bring the Spiral ODM component funct

Anton Titov 21 Aug 17, 2022
A CRUD app made with Laravel and VueJS 3 (API Composition)

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Ludovic Guénet 2 Apr 1, 2022
🌶🥗🧀🍉 Brazilian Table of Food Composition made by IBGE - Laravel 9 REST API

About API of the 2008-2009 Family Budget Survey: tables of nutritional composition of foods consumed in Brazil / IBGE, Coordination of Work and Income

Mizael Clistion 4 Dec 27, 2022
The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. It provides a better dump() function that you can use instead of var_dump().

VarDumper Component The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. It provides a better dump() function t

Symfony 7.1k Dec 23, 2022
Validates passwords against PHP's password_hash function using PASSWORD_DEFAULT. Will rehash when needed, and will upgrade legacy passwords with the Upgrade decorator.

Password Validator Password Validator validates password_hash generated passwords, rehashes passwords as necessary, and will upgrade legacy passwords.

Jeremy Kendall 142 Dec 25, 2022
The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. It provides a better dump() function that you can use instead of var_dump().

VarDumper Component The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. It provides a better dump() function t

Symfony 7.1k Jan 1, 2023
Appwrite configuration for Cloud Function runtimes settings 🌩

Appwrite Runtimes Appwrite repository for Cloud Function runtimes that contains the configurations and tests for all of the Appwrite runtime environme

Appwrite 39 Nov 22, 2022
Zen Cart® is a full-function e-commerce application for your website.

Zen Cart® - The Art of E-Commerce Zen Cart® was the first Open Source e-Commerce web application to be fully PA-DSS Certified. Zen Cart® v1.5.8 is an

Zen Cart 304 Jan 6, 2023
Search PHP source code for function & method calls, variables, and more from PHP.

Searching PHP source code made easy Search PHP source code for function & method calls, variable assignments, classes and more directly from PHP. Inst

Permafrost Software 22 Nov 24, 2022