Completed
Push — master ( cfe428...5e2d42 )
by Amine
02:00
created
src/functions.php 3 patches
Doc Comments   +1 added lines, -3 removed lines patch added patch discarded remove patch
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
  * @param  callable $fn
81 81
  * @param  int $argsCount
82 82
  * @param  array  $boundArgs
83
- * @return callable
83
+ * @return \Closure
84 84
  */
85 85
 function _curried_function($fn, $argsCount, $boundArgs = []) {
86 86
     return function() use($fn, $argsCount, $boundArgs) {
@@ -152,8 +152,6 @@  discard block
 block discarded – undo
152 152
  * ```
153 153
  *
154 154
  * @signature (*... -> a) -> [*] -> a
155
- * @param  callable $fn
156
- * @param  array    $args
157 155
  * @return mixed
158 156
  */
159 157
 function apply() {
Please login to merge, or discard this patch.
Spacing   +4 added lines, -5 removed lines patch added patch discarded remove patch
@@ -28,8 +28,7 @@  discard block
 block discarded – undo
28 28
  */
29 29
 function _number_of_args($fn) {
30 30
     $reflector = is_array($fn) ?
31
-        new \ReflectionMethod($fn[0], $fn[1]) :
32
-        new \ReflectionFunction($fn);
31
+        new \ReflectionMethod($fn[0], $fn[1]) : new \ReflectionFunction($fn);
33 32
     return $reflector->getNumberOfRequiredParameters();
34 33
 }
35 34
 
@@ -59,7 +58,7 @@  discard block
 block discarded – undo
59 58
     $addArgument = function($currentBoundArgs, $arg) use($fnArgsCount) {
60 59
         $currentBoundArgsCount = count($currentBoundArgs);
61 60
         $placeholderPosition = 0;
62
-        while($placeholderPosition < $currentBoundArgsCount && !_is_placeholder($currentBoundArgs[$placeholderPosition]))
61
+        while ($placeholderPosition < $currentBoundArgsCount && !_is_placeholder($currentBoundArgs[$placeholderPosition]))
63 62
             $placeholderPosition ++;
64 63
         if ($currentBoundArgsCount < $fnArgsCount || $placeholderPosition == $currentBoundArgsCount) {
65 64
             $currentBoundArgs[] = $arg;
@@ -179,9 +178,9 @@  discard block
 block discarded – undo
179 178
  */
180 179
 function pipe() {
181 180
     $fns = func_get_args();
182
-    if(count($fns) < 1)
181
+    if (count($fns) < 1)
183 182
         throw new InvalidArgument("pipe() requires at least one argument");
184
-    return curry(function () use ($fns) {
183
+    return curry(function() use ($fns) {
185 184
         $result = _apply(array_shift($fns), func_get_args());
186 185
         foreach ($fns as $fn) {
187 186
             $result = $fn($result);
Please login to merge, or discard this patch.
Braces   +9 added lines, -6 removed lines patch added patch discarded remove patch
@@ -59,8 +59,9 @@  discard block
 block discarded – undo
59 59
     $addArgument = function($currentBoundArgs, $arg) use($fnArgsCount) {
60 60
         $currentBoundArgsCount = count($currentBoundArgs);
61 61
         $placeholderPosition = 0;
62
-        while($placeholderPosition < $currentBoundArgsCount && !_is_placeholder($currentBoundArgs[$placeholderPosition]))
63
-            $placeholderPosition ++;
62
+        while($placeholderPosition < $currentBoundArgsCount && !_is_placeholder($currentBoundArgs[$placeholderPosition])) {
63
+                    $placeholderPosition ++;
64
+        }
64 65
         if ($currentBoundArgsCount < $fnArgsCount || $placeholderPosition == $currentBoundArgsCount) {
65 66
             $currentBoundArgs[] = $arg;
66 67
         } else { // There is a placeholder and number of bound args > $fnArgsCount
@@ -87,8 +88,9 @@  discard block
 block discarded – undo
87 88
         $boundArgs = _merge_args($argsCount, $boundArgs, func_get_args());
88 89
         $numberOfPlaceholders = count(array_filter($boundArgs, _f('_is_placeholder')));
89 90
         $numberOfGivenArgs = count($boundArgs) - $numberOfPlaceholders;
90
-        if ($numberOfGivenArgs >= $argsCount)
91
-            return call_user_func_array($fn, $boundArgs);
91
+        if ($numberOfGivenArgs >= $argsCount) {
92
+                    return call_user_func_array($fn, $boundArgs);
93
+        }
92 94
         return _curried_function($fn, $argsCount, $boundArgs);
93 95
     };
94 96
 }
@@ -179,8 +181,9 @@  discard block
 block discarded – undo
179 181
  */
180 182
 function pipe() {
181 183
     $fns = func_get_args();
182
-    if(count($fns) < 1)
183
-        throw new InvalidArgument("pipe() requires at least one argument");
184
+    if(count($fns) < 1) {
185
+            throw new InvalidArgument("pipe() requires at least one argument");
186
+    }
184 187
     return curry(function () use ($fns) {
185 188
         $result = _apply(array_shift($fns), func_get_args());
186 189
         foreach ($fns as $fn) {
Please login to merge, or discard this patch.
src/list.php 2 patches
Spacing   +16 added lines, -17 removed lines patch added patch discarded remove patch
@@ -56,7 +56,7 @@  discard block
 block discarded – undo
56 56
  * @return array
57 57
  */
58 58
 function filter() {
59
-    $filter = function($fn, $list){
59
+    $filter = function($fn, $list) {
60 60
         return array_values(array_filter($list, $fn));
61 61
     };
62 62
     return apply(curry($filter), func_get_args());
@@ -78,7 +78,7 @@  discard block
 block discarded – undo
78 78
  * @return array
79 79
  */
80 80
 function reduce() {
81
-    $reduce = function($fn, $initial, $list){
81
+    $reduce = function($fn, $initial, $list) {
82 82
         return array_reduce($list, $fn, $initial);
83 83
     };
84 84
     return apply(curry($reduce), func_get_args());
@@ -104,7 +104,7 @@  discard block
 block discarded – undo
104 104
  * @return array
105 105
  */
106 106
 function each() {
107
-    $each = function($fn, $list){
107
+    $each = function($fn, $list) {
108 108
         foreach ($list as $item) {
109 109
             apply($fn, [$item]);
110 110
         }
@@ -128,7 +128,7 @@  discard block
 block discarded – undo
128 128
  * @return mixed
129 129
  */
130 130
 function head($list) {
131
-    if(is_string($list))
131
+    if (is_string($list))
132 132
         return substr($list, 0, 1);
133 133
     return (count($list) > 0)
134 134
         ? $list[0]
@@ -150,7 +150,7 @@  discard block
 block discarded – undo
150 150
  * @return mixed
151 151
  */
152 152
 function last($list) {
153
-    if(is_string($list))
153
+    if (is_string($list))
154 154
         return substr($list, -1);
155 155
     return (count($list) > 0)
156 156
         ? $list[count($list) - 1]
@@ -173,7 +173,7 @@  discard block
 block discarded – undo
173 173
  * @return array
174 174
  */
175 175
 function init($list) {
176
-    if(is_string($list))
176
+    if (is_string($list))
177 177
         return (strlen($list) > 1)
178 178
             ? substr($list, 0, strlen($list) - 1)
179 179
             : '';
@@ -198,7 +198,7 @@  discard block
 block discarded – undo
198 198
  * @return array
199 199
  */
200 200
 function tail($list) {
201
-    if(is_string($list))
201
+    if (is_string($list))
202 202
         return (strlen($list) > 1)
203 203
             ? substr($list, 1)
204 204
             : '';
@@ -322,8 +322,7 @@  discard block
 block discarded – undo
322 322
  */
323 323
 function concatAll() {
324 324
     $concatAll = function($lists) {
325
-        return length($lists) == 0 ? [] :
326
-            reduce(concat(), head($lists), tail($lists));
325
+        return length($lists) == 0 ? [] : reduce(concat(), head($lists), tail($lists));
327 326
     };
328 327
     return apply(curry($concatAll), func_get_args());
329 328
 }
@@ -400,7 +399,7 @@  discard block
 block discarded – undo
400 399
  * @return array
401 400
  */
402 401
 function append() {
403
-    $append = function ($item, $list) {
402
+    $append = function($item, $list) {
404 403
         return concat($list, [$item]);
405 404
     };
406 405
     return apply(curry($append), func_get_args());
@@ -447,7 +446,7 @@  discard block
 block discarded – undo
447 446
         $length = length($list);
448 447
         if ($count > $length || $count < -$length)
449 448
             return [];
450
-        if(is_string($list)) {
449
+        if (is_string($list)) {
451 450
             return ($count >= 0)
452 451
                 ? substr($list, 0, $count)
453 452
                 : substr($list, $count);
@@ -703,9 +702,9 @@  discard block
 block discarded – undo
703 702
  */
704 703
 function slices() {
705 704
     $slices = function($size, $list) {
706
-        if(empty($list))
705
+        if (empty($list))
707 706
             return is_string($list) ? '' : [];
708
-        if(length($list) <= $size)
707
+        if (length($list) <= $size)
709 708
             return [$list];
710 709
         return prepend(take($size, $list), slices($size, remove($size, $list)));
711 710
     };
@@ -778,7 +777,7 @@  discard block
 block discarded – undo
778 777
 function findLastIndex() {
779 778
     $findLastIndex = function($predicate, $list) {
780 779
         foreach (reverse(toPairs($list)) as $pair) {
781
-            if($predicate($pair[1]))
780
+            if ($predicate($pair[1]))
782 781
                 return $pair[0];
783 782
         }
784 783
         return null;
@@ -851,7 +850,7 @@  discard block
 block discarded – undo
851 850
     $indexOf = function($item, $list) {
852 851
         if (is_string($list)) {
853 852
             $index = strpos($list, $item);
854
-            return (-1 == $index)
853
+            return (- 1 == $index)
855 854
                 ? null
856 855
                 : $index;
857 856
         }
@@ -880,7 +879,7 @@  discard block
 block discarded – undo
880 879
     $lastIndexOf = function($item, $list) {
881 880
         if (is_string($list)) {
882 881
             $index = strrpos($list, $item);
883
-            return (-1 == $index)
882
+            return (- 1 == $index)
884 883
                 ? null
885 884
                 : $index;
886 885
         }
@@ -968,7 +967,7 @@  discard block
 block discarded – undo
968 967
     $groupBy = function($fn, $list) {
969 968
         return reduce(function($result, $item) use($fn) {
970 969
             $index = $fn($item);
971
-            if (! isset($result[$index]))
970
+            if (!isset($result[$index]))
972 971
                 $result[$index] = [];
973 972
             $result[$index][] = $item;
974 973
             return $result;
Please login to merge, or discard this patch.
Braces   +39 added lines, -26 removed lines patch added patch discarded remove patch
@@ -128,8 +128,9 @@  discard block
 block discarded – undo
128 128
  * @return mixed
129 129
  */
130 130
 function head($list) {
131
-    if(is_string($list))
132
-        return substr($list, 0, 1);
131
+    if(is_string($list)) {
132
+            return substr($list, 0, 1);
133
+    }
133 134
     return (count($list) > 0)
134 135
         ? $list[0]
135 136
         : null;
@@ -150,8 +151,9 @@  discard block
 block discarded – undo
150 151
  * @return mixed
151 152
  */
152 153
 function last($list) {
153
-    if(is_string($list))
154
-        return substr($list, -1);
154
+    if(is_string($list)) {
155
+            return substr($list, -1);
156
+    }
155 157
     return (count($list) > 0)
156 158
         ? $list[count($list) - 1]
157 159
         : null;
@@ -173,10 +175,11 @@  discard block
 block discarded – undo
173 175
  * @return array
174 176
  */
175 177
 function init($list) {
176
-    if(is_string($list))
177
-        return (strlen($list) > 1)
178
+    if(is_string($list)) {
179
+            return (strlen($list) > 1)
178 180
             ? substr($list, 0, strlen($list) - 1)
179 181
             : '';
182
+    }
180 183
     return (count($list) > 1)
181 184
         ? array_slice($list, 0, count($list) - 1)
182 185
         : [];
@@ -198,10 +201,11 @@  discard block
 block discarded – undo
198 201
  * @return array
199 202
  */
200 203
 function tail($list) {
201
-    if(is_string($list))
202
-        return (strlen($list) > 1)
204
+    if(is_string($list)) {
205
+            return (strlen($list) > 1)
203 206
             ? substr($list, 1)
204 207
             : '';
208
+    }
205 209
     return (count($list) > 1)
206 210
         ? array_slice($list, 1)
207 211
         : [];
@@ -302,8 +306,9 @@  discard block
 block discarded – undo
302 306
         $t1 = toString($list1);
303 307
         $t2 = toString($list2);
304 308
         // echo "Concating {$t1} and {$t2}", PHP_EOL;
305
-        if (is_string($list1) && is_string($list2))
306
-            return $list1 . $list2;
309
+        if (is_string($list1) && is_string($list2)) {
310
+                    return $list1 . $list2;
311
+        }
307 312
         return array_merge($list1, $list2);
308 313
     };
309 314
     return apply(curry($concat), func_get_args());
@@ -377,8 +382,9 @@  discard block
 block discarded – undo
377 382
 function insertAll() {
378 383
     $insertAll = function($position, $items, $list) {
379 384
         $length = length($list);
380
-        if ($position < 0)
381
-            $position = $length + $position;
385
+        if ($position < 0) {
386
+                    $position = $length + $position;
387
+        }
382 388
         return ($position >= $length) 
383 389
             ? concat($list, $items) 
384 390
             : concatAll([take($position, $list), $items, remove($position, $list)]);
@@ -445,8 +451,9 @@  discard block
 block discarded – undo
445 451
 function take() {
446 452
     $take = function($count, $list) {
447 453
         $length = length($list);
448
-        if ($count > $length || $count < -$length)
449
-            return [];
454
+        if ($count > $length || $count < -$length) {
455
+                    return [];
456
+        }
450 457
         if(is_string($list)) {
451 458
             return ($count >= 0)
452 459
                 ? substr($list, 0, $count)
@@ -571,8 +578,9 @@  discard block
 block discarded – undo
571 578
 function remove() {
572 579
     $remove = function($count, $list) {
573 580
         $length = length($list);
574
-        if ($count > $length || $count < -$length)
575
-            return [];
581
+        if ($count > $length || $count < -$length) {
582
+                    return [];
583
+        }
576 584
         return ($count > 0)
577 585
             ? take($count - $length, $list)
578 586
             : take($count + $length, $list);
@@ -703,10 +711,12 @@  discard block
 block discarded – undo
703 711
  */
704 712
 function slices() {
705 713
     $slices = function($size, $list) {
706
-        if(empty($list))
707
-            return is_string($list) ? '' : [];
708
-        if(length($list) <= $size)
709
-            return [$list];
714
+        if(empty($list)) {
715
+                    return is_string($list) ? '' : [];
716
+        }
717
+        if(length($list) <= $size) {
718
+                    return [$list];
719
+        }
710 720
         return prepend(take($size, $list), slices($size, remove($size, $list)));
711 721
     };
712 722
     return apply(curry($slices), func_get_args());
@@ -752,8 +762,9 @@  discard block
 block discarded – undo
752 762
 function findIndex() {
753 763
     $findIndex = function($predicate, $list) {
754 764
         foreach ($list as $key => $value) {
755
-            if ($predicate($value))
756
-                return $key;
765
+            if ($predicate($value)) {
766
+                            return $key;
767
+            }
757 768
         }
758 769
         return null;
759 770
     };
@@ -778,8 +789,9 @@  discard block
 block discarded – undo
778 789
 function findLastIndex() {
779 790
     $findLastIndex = function($predicate, $list) {
780 791
         foreach (reverse(toPairs($list)) as $pair) {
781
-            if($predicate($pair[1]))
782
-                return $pair[0];
792
+            if($predicate($pair[1])) {
793
+                            return $pair[0];
794
+            }
783 795
         }
784 796
         return null;
785 797
     };
@@ -968,8 +980,9 @@  discard block
 block discarded – undo
968 980
     $groupBy = function($fn, $list) {
969 981
         return reduce(function($result, $item) use($fn) {
970 982
             $index = $fn($item);
971
-            if (! isset($result[$index]))
972
-                $result[$index] = [];
983
+            if (! isset($result[$index])) {
984
+                            $result[$index] = [];
985
+            }
973 986
             $result[$index][] = $item;
974 987
             return $result;
975 988
         }, [], $list);
Please login to merge, or discard this patch.
src/Stream.php 1 patch
Spacing   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -81,7 +81,7 @@  discard block
 block discarded – undo
81 81
      * @param  mixed $data
82 82
      * @return Stream
83 83
      */
84
-    public static function of ($data)
84
+    public static function of($data)
85 85
     {
86 86
         $data = func_get_args();
87 87
         if (count($data) == 1)
@@ -96,7 +96,7 @@  discard block
 block discarded – undo
96 96
      * @param  array $operations
97 97
      * @return Stream
98 98
      */
99
-    protected static function with ($data, $operations, $type)
99
+    protected static function with($data, $operations, $type)
100 100
     {
101 101
         return new Stream($data, $operations, $type);
102 102
     }
@@ -107,7 +107,7 @@  discard block
 block discarded – undo
107 107
      * @param  array $operations
108 108
      * @return array|Error
109 109
      */
110
-    protected static function optimize ($operations)
110
+    protected static function optimize($operations)
111 111
     {
112 112
         // TODO: ...
113 113
         return $operations;
@@ -120,11 +120,11 @@  discard block
 block discarded – undo
120 120
      * @param  mixed $data
121 121
      * @return mixed|Error
122 122
      */
123
-    protected static function execute ($operations, $data)
123
+    protected static function execute($operations, $data)
124 124
     {
125 125
         if (length($operations) == 0)
126 126
             return $data;
127
-        $operations = apply(_f('pipe'), map(function($operation){
127
+        $operations = apply(_f('pipe'), map(function($operation) {
128 128
             if ($operation['name'] == 'apply') {
129 129
                 return $operation['args'];
130 130
             }
@@ -142,7 +142,7 @@  discard block
 block discarded – undo
142 142
      * @param  string $type
143 143
      * @return bool
144 144
      */
145
-    protected static function canApply ($operation, $type)
145
+    protected static function canApply($operation, $type)
146 146
     {
147 147
         return isset(Stream::$transformations[$operation]) && (
148 148
             $type == 'Unknown' ||
@@ -158,7 +158,7 @@  discard block
 block discarded – undo
158 158
      * @param  string $type
159 159
      * @return bool
160 160
      */
161
-    protected static function returnOf ($operation, $type)
161
+    protected static function returnOf($operation, $type)
162 162
     {
163 163
         return isset(Stream::$transformations[$operation][$type])
164 164
             ? Stream::$transformations[$operation][$type]
@@ -173,12 +173,12 @@  discard block
 block discarded – undo
173 173
      * @param  Stream $stream
174 174
      * @return Stream
175 175
      */
176
-    protected static function apply ($operation, $args, $stream)
176
+    protected static function apply($operation, $args, $stream)
177 177
     {
178 178
         if ($stream->type == 'Error') {
179 179
             return Stream::of(Error::of("Could not apply {$operation} to {$stream->type}", $stream->data));
180 180
         }
181
-        if (! Stream::canApply($operation, $stream->type)) {
181
+        if (!Stream::canApply($operation, $stream->type)) {
182 182
             $data = toString($stream->data);
183 183
             return Stream::of(Error::of("Could not apply {$operation} to {$stream->type}({$data})"));
184 184
         }
@@ -194,7 +194,7 @@  discard block
 block discarded – undo
194 194
      *
195 195
      * @param mixed $data
196 196
      */
197
-    protected function __construct ($data, $operations, $type)
197
+    protected function __construct($data, $operations, $type)
198 198
     {
199 199
         $this->data = $data;
200 200
         $this->type = $type;
@@ -232,7 +232,7 @@  discard block
 block discarded – undo
232 232
      * @signature Stream(a) -> a
233 233
      * @return mixed
234 234
      */
235
-    public function get ()
235
+    public function get()
236 236
     {
237 237
         if ($this->type == 'Error')
238 238
             return $this->data;
@@ -251,7 +251,7 @@  discard block
 block discarded – undo
251 251
      * @param  callable $fn
252 252
      * @return Stream
253 253
      */
254
-    public function map (callable $fn)
254
+    public function map(callable $fn)
255 255
     {
256 256
         return Stream::apply('map', [$fn], $this);
257 257
     }
@@ -268,7 +268,7 @@  discard block
 block discarded – undo
268 268
      * @param  callable $predicate
269 269
      * @return Stream
270 270
      */
271
-    public function filter (callable $predicate)
271
+    public function filter(callable $predicate)
272 272
     {
273 273
         return Stream::apply('filter', [$predicate], $this);
274 274
     }
@@ -286,7 +286,7 @@  discard block
 block discarded – undo
286 286
      * @param mixed $initial
287 287
      * @return Stream
288 288
      */
289
-    public function reduce ($fn, $initial)
289
+    public function reduce($fn, $initial)
290 290
     {
291 291
         return Stream::apply('reduce', [$fn, $initial], $this);
292 292
     }
@@ -305,7 +305,7 @@  discard block
 block discarded – undo
305 305
      * @param callable $fn
306 306
      * @return Stream
307 307
      */
308
-    public function chain ($fn)
308
+    public function chain($fn)
309 309
     {
310 310
         return Stream::apply('chain', [$fn], $this);
311 311
     }
@@ -325,7 +325,7 @@  discard block
 block discarded – undo
325 325
      * @signature Stream(String) -> Number
326 326
      * @return Stream
327 327
      */
328
-    public function length ()
328
+    public function length()
329 329
     {
330 330
         return Stream::apply('length', [], $this);
331 331
     }
@@ -346,7 +346,7 @@  discard block
 block discarded – undo
346 346
      * @param int $number
347 347
      * @return Stream
348 348
      */
349
-    public function take ($number)
349
+    public function take($number)
350 350
     {
351 351
         return Stream::apply('take', [$number], $this);
352 352
     }
@@ -367,7 +367,7 @@  discard block
 block discarded – undo
367 367
      * @param callable $fn
368 368
      * @return Stream
369 369
      */
370
-    public function then ($fn)
370
+    public function then($fn)
371 371
     {
372 372
         $result = $this;
373 373
         foreach (func_get_args() as $fn) {
@@ -417,7 +417,7 @@  discard block
 block discarded – undo
417 417
      * @param  mixed|null $args...
418 418
      * @return Stream
419 419
      */
420
-    public function call ($method)
420
+    public function call($method)
421 421
     {
422 422
         $args = tail(func_get_args());
423 423
         return Stream::apply('apply', function($data) use($method, $args) {
@@ -471,7 +471,7 @@  discard block
 block discarded – undo
471 471
      * @param  mixed|null $args...
472 472
      * @return Stream
473 473
      */
474
-    public function run ($method)
474
+    public function run($method)
475 475
     {
476 476
         $args = tail(func_get_args());
477 477
         return Stream::apply('apply', function($data) use($method, $args) {
Please login to merge, or discard this patch.