1 <?php
declare(strict_types
=1);
3 function partition(int $numPartitions, array $array) : array {
4 $partitionSize = (int)ceil(count($array) / $numPartitions);
7 map(function($p) use ($array, $partitionSize) {
8 return array_slice($array, $p*$partitionSize, $partitionSize);
9 })(range(0, $numPartitions-1))
13 function formatDate($date) {
14 return $date->format(DATE_FORMAT
);
17 function ssort($comparitor) {
18 return function($array) use ($comparitor) {
19 uasort($array, uncurry($comparitor));
24 function uncurry($f) {
25 return function($a, $b) use ($f) {
30 function between($content, $start){
31 $r = explode($start, $content);
33 $r = explode($start, $r[1]);
39 const lines
= 'lines';
40 function lines(string $string): array {
41 return explode("\n", $string);
45 function glue(string $delim): callable
{
46 return function(array $strings) use ($delim): string {
47 return implode($delim, $strings);
51 const unlines
= 'unlines';
52 function unlines($lines) {
53 return implode("\n", $lines);
56 const ununlines
= 'ununlines';
57 function ununlines($lines) {
58 return implode("\n\n", $lines);
61 const zipWith
= 'zipWith';
62 function zipWith(callable
$zipper, array $a, array $b) {
63 return array_map($zipper, $a, $b);
66 function field($field) {
67 return function($array) use ($field) {
68 return $array[$field];
74 return function($b) use ($a) {
81 return function($arg) use ($fs) {
82 return array_reduce(array_reverse($fs), function($c, $f) {
88 function map($callable) {
89 return function($list) use ($callable) {
90 return array_map($callable, $list);
94 function aaray_column($column) {
95 return function($array) use ($column) {
96 return array_column($array, $column);
100 function aaray_slice($start) {
101 return function($length) use ($start) {
102 return function($array) use ($length, $start) {
103 return array_slice($array, $start, $length);
108 function filter($callable) {
109 return function($list) use ($callable) {
110 return array_filter($list, $callable);
114 function f∘
(callable
$f) {
115 return function(callable
$g) use ($f) {
116 return function($arg) use($g, $f) {
122 function ∘
f(callable
$f) {
123 return function(callable
$g) use ($f) {
124 return function($arg) use($g, $f) {
131 return array_merge($a, $b);
134 function closestIndex(int $n, array $list) {
135 $a = map(function(int $v) use ($n) : int {
141 return array_keys($a)[0];
144 function closest(int $n, array $list) : int {
145 return $list[closestIndex($n, $list)];
148 function array_diff_assoc_recursive($array1, $array2) {
150 foreach($array1 as $key => $value) {
151 if( is_array($value) ) {
152 if( !isset($array2[$key]) ||
!is_array($array2[$key]) ) {
153 $difference[$key] = $value;
155 $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
156 if( !empty($new_diff) )
157 $difference[$key] = $new_diff;
159 } else if( !array_key_exists($key,$array2) ||
$array2[$key] !== $value ) {
160 $difference[$key] = $value;