All Projects → phpfn → Pipe

phpfn / Pipe

Licence: mit
[READONLY] Library for implementing function call chains

Projects that are alternatives of or similar to Pipe

pipe
Functional Pipeline in Go
Stars: ✭ 30 (-57.14%)
Mutual labels:  functional, pipe
Ppipe
pipes values through functions, an alternative to using the proposed pipe operator ( |> ) for ES
Stars: ✭ 192 (+174.29%)
Mutual labels:  chain, pipe
Pipetools
Functional plumbing for Python
Stars: ✭ 143 (+104.29%)
Mutual labels:  pipe, functional
pipe
Pipe operator for nim.
Stars: ✭ 51 (-27.14%)
Mutual labels:  functional, pipe
Kari.hpp
Experimental library for currying in C++17
Stars: ✭ 58 (-17.14%)
Mutual labels:  functional
Dart functional data
Simple and non-intrusive code generator for lenses and boilerplate of data types
Stars: ✭ 39 (-44.29%)
Mutual labels:  functional
Amino
functional data structures and utilities for python
Stars: ✭ 31 (-55.71%)
Mutual labels:  functional
Angularconcepts
Key Angular Concepts using Latest Angular version 5
Stars: ✭ 31 (-55.71%)
Mutual labels:  pipe
Openapi Spring Webflux Validator
🌱 A friendly kotlin library to validate API endpoints using an OpenApi 3.0 and Swagger 2.0 specification
Stars: ✭ 67 (-4.29%)
Mutual labels:  functional
Promised Pipe
A ramda.pipe-like utility that handles promises internally with zero dependencies
Stars: ✭ 64 (-8.57%)
Mutual labels:  pipe
Siler
⚡ Flat-files and plain-old PHP functions rockin'on as a set of general purpose high-level abstractions.
Stars: ✭ 1,056 (+1408.57%)
Mutual labels:  functional
Easygo
基于Kotlin、OkHttp的声明式网络框架,像写HTML界面一样写网络调用代码
Stars: ✭ 40 (-42.86%)
Mutual labels:  chain
Babel Plugin Partial Application
[DEPRECATED] Please use https://github.com/citycide/param.macro
Stars: ✭ 60 (-14.29%)
Mutual labels:  functional
Rexrex
🦖 Composable JavaScript regular expressions
Stars: ✭ 34 (-51.43%)
Mutual labels:  functional
Pypika
PyPika is a python SQL query builder that exposes the full richness of the SQL language using a syntax that reflects the resulting query. PyPika excels at all sorts of SQL queries but is especially useful for data analysis.
Stars: ✭ 1,111 (+1487.14%)
Mutual labels:  functional
Swiftlyext
SwiftlyExt is a collection of useful extensions for Swift 3 standard classes and types 🚀
Stars: ✭ 31 (-55.71%)
Mutual labels:  functional
Yglu
Yglu ᕄ !? - YAML glue for structural templating and processing
Stars: ✭ 51 (-27.14%)
Mutual labels:  functional
Elementx
⚡️ Functionally create DOM elements and compose them to a tree quickly
Stars: ✭ 62 (-11.43%)
Mutual labels:  functional
Phobos
The standard library of the D programming language
Stars: ✭ 1,038 (+1382.86%)
Mutual labels:  functional
Inferno Most Fp Demo
A demo for the ReactJS Tampa Bay meetup showing how to build a React+Redux-like architecture from scratch using Inferno, Most.js, reactive programmning, and various functional programming tools & techniques
Stars: ✭ 45 (-35.71%)
Mutual labels:  functional

Pipe

Object-oriented pipe operator implementation based on RFC Pipe Operator.

Installation

Library can be installed into any PHP application:

$ composer require phpfn/pipe

In order to access library make sure to include vendor/autoload.php in your file.

<?php

require __DIR__ . '/vendor/autoload.php';

Usage

A common PHP OOP pattern is the use of method chaining, or what is also known as "Fluent Expressions". So named for the way one method flows into the next to form a conceptual hyper-expression.

However, when using the functional approach this can lead to reduced readability, polluted symbol tables, or static-analysis defying type inconsistency such as in the following example:

<?php

$snakeCase = strtolower(
    preg_replace('/(.)(?=[A-Z])/u', '$1_', 
        preg_replace('/\s+/u', '', 
            ucwords('HelloWorld')
        )
    )
);
             
var_dump($snakeCase); // "hello_world"

The pipe library fixes this problem, allows you to chain the execution of pure functions:

pipe('Hello World')
    ->ucwords(_)
    ->preg_replace('/\s+/u', '', _)
    ->preg_replace('/(.)(?=[A-Z])/u', '$1_', _)
    ->strtolower(_)
    ->var_dump;
//
// string(11) "hello_world"
//

Another Example

See: https://wiki.php.net/rfc/pipe-operator#file_collection_example

<?php
$result = array_merge(
    $result,
    namespaced\func\get_file_arg(
        array_map(
            function ($x) use ($arg) {
                return $arg . '/' . $x;
            },
            array_filter(
                scandir($arg),
                function ($x) {
                    return $x !== '.' && $x !== '..';
                }
            )
        )
    )
);

With this library, the above could be easily rewritten as:

<?php

$result = pipe($arg)
    ->scanDir($arg)
    ->array_filter(_, fn($x): bool => $x !== '.' && $x !== '..')
    ->array_map(fn($x): string => $arg . '/' . $x, _)
    ->use('namespaced\func')->get_file_arg
    ->array_merge($result, _);

Working With Value

To pass a value as an argument to a function, use the underscore (_) character:

<?php

pipe('hello')
    ->str_replace('o', '', _)
    ->var_dump; // "hell"

You can omit parentheses if only one argument is used:

<?php

pipe('some')
    ->is_array
    ->var_dump; // bool(false) 

To get the value, use one of the options:

<?php
$context = pipe('hello')->strtoupper;

var_dump($context);
// object(Fun\Pipe\Pipe)#8 (1) { ... } 

var_dump($context());
// string(5) "HELLO"

Working With Namespace

Let's take a simple example of such code:

namespace {
    function foo() { return __FUNCTION__; }
}

namespace Example {
    function foo() { return __FUNCTION__; }
}

Let's try to manage the namespace:

$context = pipe()->use('Example')->foo;

echo $context(); // 'Example\\foo'

$context = $context->foo;

echo $context(); // 'foo'

Please note that the use function applies only to the subsequent function, all further operations performed in the current context:

pipe()
    ->use('Some\\Namespace')->foo // Call "\Some\Namespace\foo()"
    ->foo // Call "\foo()"
;

In order to perform several operations in another namespace, use an anonymous function as the second use argument.

pipe()
    ->use('Some\\Namespace', fn($pipe) => 
        $pipe
            ->a // Call "\Some\Namespace\a()"
            ->b // Call "\Some\Namespace\b()"
    )
    ->a // Call "a()"
;

Note that the behavior of the ->use() method differs depending on whether the second argument is passed.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].