PHP code example of anarchitecture / combinatorics

1. Go to this page and download the library: Download anarchitecture/combinatorics 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 / combinatorics example snippets




use Anarchitecture\combinatorics as c;
use Anarchitecture\pipe as p;

$items = ['a' => 1, 'b' => 2];

$subsets = $items
    |> c\iterable_powerset()
    |> p\collect(...)

// [
//  [],
//  ['a'=>1],
//  ['b'=>2],
//  ['a'=>1,'b'=>2]
// ]

use Anarchitecture\combinatorics as c;
use Anarchitecture\pipe as p;

$items = ['a' => 1, 'b' => 2, 'c' => 3];

$pairs = $items
    |> c\iterable_combinations(2)
    |> p\collect(...);

// [
//   ['b' => 2, 'c' => 3],
//   ['a' => 1, 'c' => 3],
//   ['a' => 1, 'b' => 2],
// ]

use Anarchitecture\combinatorics as c;
use Anarchitecture\pipe as p;

$allocations = ['a' => null, 'b' => null, 'c' => null]
    |> c\iterable_allocations(2)
    |> p\collect(...);

// [
//   ['a' => 0, 'b' => 0, 'c' => 2],
//   ['a' => 0, 'b' => 1, 'c' => 1],
//   ['a' => 0, 'b' => 2, 'c' => 0],
//   ['a' => 1, 'b' => 0, 'c' => 1],
//   ['a' => 1, 'b' => 1, 'c' => 0],
//   ['a' => 2, 'b' => 0, 'c' => 0],
// ]