PHP code example of anarchitecture / pipe
1. Go to this page and download the library: Download anarchitecture/pipe library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
anarchitecture / pipe example snippets
use Anarchitecture\pipe as p;
$a = range(1, 8)
|> p\array_map(fn ($x) => $x ** $x)
|> p\array_chunk(4)
|> p\array_map(array_sum(...));
// [288, 17650540]
use Anarchitecture\pipe as p;
p\array_map(fn ($x) => $x * 2);
// fn (array $input) => array_map(fn ($x) => $x * 2, $input)
use Anarchitecture\pipe as p;
// numeric keys => positional
$out1 = [10 => "a", 20 => "b", 30 => "c"]
|> p\apply(fn (string $a, string $b, string $c) => $a . $b . $c);
// "abc"
// string keys => named
$out2 = ["b" => 2, "a" => 1]
|> p\apply(fn (int $a, int $b) => $a - $b);
// -1
use Anarchitecture\pipe as p;
$out = " Hello "
|> p\when(is_string(...), trim(...))
|> p\when(p\equals("Hello"), p\value("bye"));
// "bye"
use Anarchitecture\pipe as p;
$out = "Hello"
|> p\if_else(
p\equals("Hello"),
p\value("bye"),
p\value("unknown")
);
// "bye"
use Anarchitecture\pipe as p;
$result = 0
|> p\iterate(static fn(int $x) : int => $x + 1)
|> p\iterable_take(4)
|> p\collect(...);
// [0, 1, 2, 3]
use Anarchitecture\pipe as p;
$sumPairs = [[6, 7, 8], [10, 20, 30]]
|> p\zip_map(fn ($a, $b) => $a + $b);
// [16, 27, 38]
use Anarchitecture\pipe as p;
$user = [
'id' => 123,
'email' => '[email protected] ',
'password_hash' => '...',
];
$public = $user
|> p\array_dissoc('password_hash');
// ['id' => 123, 'email' => '[email protected] ']
use Anarchitecture\pipe as p;
$out = " Hello "
|> trim(...)
|> p\tap(\var_dump(...)) // debug
|> strtoupper(...);
// prints "Hello", returns "HELLO"
use Anarchitecture\pipe as p;
$matrix = [
[1, 2, 3],
[4, 5, 6],
];
$t = $matrix
|> p\array_transpose();
// [
// [1, 4],
// [2, 5],
// [3, 6]
//]
use Anarchitecture\pipe as p;
$matrix = [
'r1' => ['a' => 1, 'b' => 2],
'r2' => ['a' => 3, 'c' => 4],
];
$t = $matrix
|> p\array_transpose();
// [
// 'a' => ['r1' => 1, 'r2' => 3],
// 'b' => ['r1' => 2, 'r2' => null],
// 'c' => ['r1' => null, 'r2' => 4],
// ]
use Anarchitecture\pipe as p;
$result = [1, 2]
|> p\iterable_zip([10, 20], [100, 200])
|> p\collect(...);
// [
// [1, 10, 100],
// [2, 20, 200]
//]
use Anarchitecture\pipe as p;
// linear (default): full windows only, no wraparound
$linear = [1, 2, 3, 4, 5, 6]
|> p\iterable_window(3)
|> p\collect(...);
// [
// [1, 2, 3],
// [2, 3, 4],
// [3, 4, 5],
// [4, 5, 6],
// ]
// circular: adds the boundary-crossing windows (end -> start)
$circular = [0, 1, 2, -2, -1]
|> p\iterable_window(size: 4, circular: true)
|> p\collect(...);
// [
// [0, 1, 2, -2],
// [1, 2, -2, -1],
// [2, -2, -1, 0],
// [-2, -1, 0, 1],
// [-1, 0, 1, 2],
// ]