-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathcollection_functions.php
More file actions
1753 lines (1542 loc) · 43.2 KB
/
Copy pathcollection_functions.php
File metadata and controls
1753 lines (1542 loc) · 43.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace DusanKasan\Knapsack;
use DusanKasan\Knapsack\Exceptions\InvalidArgument;
use DusanKasan\Knapsack\Exceptions\ItemNotFound;
use DusanKasan\Knapsack\Exceptions\NoMoreItems;
use Iterator;
use IteratorIterator;
use Traversable;
/**
* Converts $collection to array. If there are multiple items with the same key, only the last will be preserved.
*
* @param array|Traversable $collection
* @return array
*/
function toArray($collection)
{
return is_array($collection) ? $collection : iterator_to_array($collection);
}
/**
* Returns a lazy collection of distinct items in $collection.
*
* @param array|Traversable $collection
* @return Collection
*/
function distinct($collection)
{
$generatorFactory = function () use ($collection) {
$distinctValues = [];
foreach ($collection as $key => $value) {
if (!in_array($value, $distinctValues)) {
$distinctValues[] = $value;
yield $key => $value;
}
}
};
return new Collection($generatorFactory);
}
/**
* Returns number of items in $collection.
*
* @param array|Traversable $collection
* @return int
*/
function size($collection)
{
$result = 0;
foreach ($collection as $value) {
$result++;
}
return $result;
}
/**
* Returns a non-lazy collection with items from $collection in reversed order.
*
* @param array|Traversable $collection
* @return Collection
*/
function reverse($collection)
{
$generatorFactory = function () use ($collection) {
$array = [];
foreach ($collection as $key => $value) {
$array[] = [$key, $value];
}
return map(
indexBy(
array_reverse($array),
function ($item) {
return $item[0];
}
),
function ($item) {
return $item[1];
}
);
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection of values from $collection (i.e. the keys are reset).
*
* @param array|Traversable $collection
* @return Collection
*/
function values($collection)
{
$generatorFactory = function () use ($collection) {
foreach ($collection as $value) {
yield $value;
}
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection of keys from $collection.
*
* @param array|Traversable $collection
* @return Collection
*/
function keys($collection)
{
$generatorFactory = function () use ($collection) {
foreach ($collection as $key => $value) {
yield $key;
}
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection of items from $collection repeated infinitely.
*
* @param array|Traversable $collection
* @return Collection
*/
function cycle($collection)
{
$generatorFactory = function () use ($collection) {
while (true) {
foreach ($collection as $key => $value) {
yield $key => $value;
}
}
};
return new Collection($generatorFactory);
}
/**
* Returns a non-lazy collection of shuffled items from $collection.
*
* @param array|Traversable $collection
* @return Collection
*/
function shuffle($collection)
{
$buffer = [];
foreach ($collection as $key => $value) {
$buffer[] = [$key, $value];
}
\shuffle($buffer);
return dereferenceKeyValue($buffer);
}
/**
* Returns true if $collection does not contain any items.
*
* @param array|Traversable $collection
* @return bool
*/
function isEmpty($collection)
{
foreach ($collection as $value) {
return false;
}
return true;
}
/**
* Returns true if $collection does contain any items.
*
* @param array|Traversable $collection
* @return bool
*/
function isNotEmpty($collection)
{
return !isEmpty($collection);
}
/**
* Returns a collection where keys are distinct values from $collection and values are number of occurrences of each
* value.
*
* @param array|Traversable $collection
* @return Collection
*/
function frequencies($collection)
{
return countBy($collection, '\DusanKasan\Knapsack\identity');
}
/**
* Returns the first item of $collection or throws ItemNotFound if #collection is empty.
*
* @param array|Traversable $collection
* @return mixed
*/
function first($collection)
{
return get(values($collection), 0);
}
/**
* Returns the last item of $collection or throws ItemNotFound if #collection is empty.
*
* @param array|Traversable $collection
* @return mixed
*/
function last($collection)
{
return first(reverse($collection));
}
/**
* Returns a lazy collection of items of $collection where value of each item is set to the return value of calling
* $function on its value and key.
*
* @param array|Traversable $collection
* @param callable $function ($value, $key)
* @return Collection
*/
function map($collection, callable $function)
{
$generatorFactory = function () use ($collection, $function) {
foreach ($collection as $key => $value) {
yield $key => $function($value, $key);
}
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection of items from $collection for which $function returns true.
*
* @param array|Traversable $collection
* @param callable|null $function ($value, $key)
* @return Collection
*/
function filter($collection, callable $function = null)
{
if (null === $function) {
$function = function ($value) {
return (bool) $value;
};
}
$generatorFactory = function () use ($collection, $function) {
foreach ($collection as $key => $value) {
if ($function($value, $key)) {
yield $key => $value;
}
}
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection with items from all $collections passed as argument appended together
*
* @param array|Traversable ...$collections
* @return Collection
*/
function concat(...$collections)
{
$generatorFactory = function () use ($collections) {
foreach ($collections as $collection) {
foreach ($collection as $key => $value) {
yield $key => $value;
}
}
};
return new Collection($generatorFactory);
}
/**
* Reduces the collection to single value by iterating over the collection and calling $reduction while
* passing $startValue and current key/item as parameters. The output of $function is used as $startValue in
* next iteration. The output of $function on last element is the return value of this function.
*
* @param array|Traversable $collection
* @param callable $function ($tmpValue, $value, $key)
* @param mixed $startValue
* @return mixed
*/
function reduce($collection, callable $function, $startValue)
{
$tmp = duplicate($startValue);
foreach ($collection as $key => $value) {
$tmp = $function($tmp, $value, $key);
}
return $tmp;
}
/**
* Flattens multiple levels of nesting in collection. If $levelsToFlatten is not specified, flattens all levels of
* nesting.
*
* @param array|Traversable $collection
* @param int $levelsToFlatten -1 to flatten everything
* @return Collection
*/
function flatten($collection, $levelsToFlatten = -1)
{
$generatorFactory = function () use ($collection, $levelsToFlatten) {
$flattenNextLevel = $levelsToFlatten < 0 || $levelsToFlatten > 0;
$childLevelsToFlatten = $levelsToFlatten > 0 ? $levelsToFlatten - 1 : $levelsToFlatten;
foreach ($collection as $key => $value) {
if ($flattenNextLevel && (is_array($value) || $value instanceof Traversable)) {
foreach (flatten($value, $childLevelsToFlatten) as $childKey => $childValue) {
yield $childKey => $childValue;
}
} else {
yield $key => $value;
}
}
};
return new Collection($generatorFactory);
}
/**
* Returns a non-lazy collection sorted using $collection($item1, $item2, $key1, $key2 ). $collection should
* return true if first item is larger than the second and false otherwise.
*
* @param array|Traversable $collection
* @param callable $function ($value1, $value2, $key1, $key2)
* @return Collection
*/
function sort($collection, callable $function)
{
$array = iterator_to_array(
values(
map(
$collection,
function ($value, $key) {
return [$key, $value];
}
)
)
);
uasort(
$array,
function ($a, $b) use ($function) {
return $function($a[1], $b[1], $a[0], $b[0]);
}
);
return dereferenceKeyValue($array);
}
/**
* Returns a lazy collection that is a part of $collection starting from $from position and ending in $to position.
* If $to is not provided, the returned collection is contains all items from $from until end of $collection. All items
* before $from are iterated over, but not included in result.
*
* @param array|Traversable $collection
* @param int $from
* @param int $to -1 to slice until end
* @return Collection
*/
function slice($collection, $from, $to = -1)
{
$generatorFactory = function () use ($collection, $from, $to) {
$index = 0;
foreach ($collection as $key => $value) {
if ($index >= $from && ($index < $to || $to == -1)) {
yield $key => $value;
} elseif ($index >= $to && $to >= 0) {
break;
}
$index++;
}
};
return new Collection($generatorFactory);
}
/**
* Returns a non-lazy collection of items grouped by the result of $function.
*
* @param array|Traversable $collection
* @param callable $function ($value, $key)
* @return Collection
*/
function groupBy($collection, callable $function)
{
$result = [];
foreach ($collection as $key => $value) {
$newKey = $function($value, $key);
$result[$newKey][] = $value;
}
return Collection::from($result)
->map(function ($entry) {
return new Collection($entry);
});
}
/**
* Returns a non-lazy collection of items grouped by the value at given key. Ignores non-collection items and items
* without the given keys
*
* @param array|Traversable $collection
* @param mixed $key
* @return Collection
*/
function groupByKey($collection, $key)
{
$generatorFactory = function () use ($collection, $key) {
return groupBy(
filter(
$collection,
function ($item) use ($key) {
return isCollection($item) && has($item, $key);
}
),
function ($value) use ($key) {
return get($value, $key);
}
);
};
return new Collection($generatorFactory);
}
/**
* Executes $function for each item in $collection
*
* @param array|Traversable $collection
* @param callable $function ($value, $key)
* @return Collection
*/
function each($collection, callable $function)
{
$generatorFactory = function () use ($collection, $function) {
foreach ($collection as $key => $value) {
$function($value, $key);
yield $key => $value;
}
};
return new Collection($generatorFactory);
}
/**
* Returns an item with $key key from $collection. If that key is not present, throws ItemNotFound.
*
* @param array|Traversable $collection
* @param mixed $key
* @return mixed
*/
function get($collection, $key)
{
foreach ($collection as $valueKey => $value) {
if ($key === $valueKey) {
return $value;
}
}
throw new ItemNotFound;
}
/**
* Returns an item with $key key from $collection. If that key is not present, returns $default.
*
* @param array|Traversable $collection
* @param mixed $key
* @param mixed $default value returned if key is not found
* @return mixed
*/
function getOrDefault($collection, $key, $default)
{
try {
return get($collection, $key);
} catch (ItemNotFound $e) {
return $default;
}
}
/**
* Returns the first item from $collection for which $function returns true. If item like that is not present, returns
* $default.
*
* @param array|Traversable $collection
* @param callable $function ($value, $key)
* @param mixed $default
* @return mixed
*/
function find($collection, callable $function, $default = null)
{
foreach ($collection as $key => $value) {
if ($function($value, $key)) {
return $value;
}
}
return $default;
}
/**
* Returns a lazy collection by changing keys of $collection for each item to the result of $function for
* that item.
*
* @param array|Traversable $collection
* @param callable $function ($value, $key)
* @return Collection
*/
function indexBy($collection, callable $function)
{
$generatorFactory = function () use ($collection, $function) {
foreach ($collection as $key => $value) {
yield $function($value, $key) => $value;
}
};
return new Collection($generatorFactory);
}
/**
* Returns a non-lazy collection of items whose keys are the return values of $function and values are the number of
* items in this collection for which the $function returned this value.
*
* @param array|Traversable $collection
* @param callable $function ($value, $key)
* @return Collection
*/
function countBy($collection, callable $function)
{
return map(
groupBy($collection, $function),
'\DusanKasan\Knapsack\size'
);
}
/**
* Returns true if $function returns true for every item in $collection
*
* @param array|Traversable $collection
* @param callable $function ($value, $key)
* @return bool
*/
function every($collection, callable $function)
{
foreach ($collection as $key => $value) {
if (!$function($value, $key)) {
return false;
}
}
return true;
}
/**
* Returns true if $function returns true for at least one item in $collection.
*
* @param array|Traversable $collection
* @param callable $function ($value, $key)
* @return bool
*/
function some($collection, callable $function)
{
foreach ($collection as $key => $value) {
if ($function($value, $key)) {
return true;
}
}
return false;
}
/**
* Returns true if $needle is found in $collection values.
*
* @param array|Traversable $collection
* @param mixed $needle
* @return bool
*/
function contains($collection, $needle)
{
foreach ($collection as $key => $value) {
if ($value === $needle) {
return true;
}
}
return false;
}
/**
* Reduce that walks from right to the left.
*
* @param array|Traversable $collection
* @param callable $function
* @param mixed $startValue
* @return mixed
*/
function reduceRight($collection, callable $function, $startValue)
{
return reduce(reverse($collection), $function, $startValue);
}
/**
* Returns a lazy collection of first $numberOfItems items of $collection.
*
* @param array|Traversable $collection
* @param int $numberOfItems
* @return Collection
*/
function take($collection, $numberOfItems)
{
return slice($collection, 0, $numberOfItems);
}
/**
* Returns a lazy collection of all but first $numberOfItems items of $collection.
*
* @param array|Traversable $collection
* @param int $numberOfItems
* @return Collection
*/
function drop($collection, $numberOfItems)
{
return slice($collection, $numberOfItems);
}
/**
* Returns a lazy collection of values, where first value is $value and all subsequent values are computed by applying
* $function to the last value in the collection. By default this produces an infinite collection. However you can
* end the collection by throwing a NoMoreItems exception.
*
* @param mixed $value
* @param callable $function ($value, $key)
* @return Collection
*/
function iterate($value, callable $function)
{
$duplicated = duplicate($value);
$generatorFactory = function () use ($duplicated, $function) {
$value = $duplicated;
yield $value;
while (true) {
try {
$value = $function($value);
yield $value;
} catch (NoMoreItems $e) {
break;
}
}
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection of items from $collection for which $function returned true.
*
* @param array|Traversable $collection
* @param callable $function ($value, $key)
* @return Collection
*/
function reject($collection, callable $function)
{
return filter(
$collection,
function ($value, $key) use ($function) {
return !$function($value, $key);
}
);
}
/**
* Returns a lazy collection of items in $collection without the last $numberOfItems items.
*
* @param array|Traversable $collection
* @param int $numberOfItems
* @return Collection
*/
function dropLast($collection, $numberOfItems = 1)
{
$generatorFactory = function () use ($collection, $numberOfItems) {
$buffer = [];
foreach ($collection as $key => $value) {
$buffer[] = [$key, $value];
if (count($buffer) > $numberOfItems) {
$val = array_shift($buffer);
yield $val[0] => $val[1];
}
}
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection of items from $collection separated by $separator.
*
* @param array|Traversable $collection
* @param mixed $separator
* @return Collection
*/
function interpose($collection, $separator)
{
$generatorFactory = function () use ($collection, $separator) {
foreach (take($collection, 1) as $key => $value) {
yield $key => $value;
}
foreach (drop($collection, 1) as $key => $value) {
yield $separator;
yield $key => $value;
}
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection of first item from first collection, first item from second, second from first and
* so on. Accepts any number of collections.
*
* @param array|Traversable ...$collections
* @return Collection
*/
function interleave(...$collections)
{
$generatorFactory = function () use ($collections) {
/* @var Iterator[] $iterators */
$iterators = array_map(
function ($collection) {
$it = new IteratorIterator(new Collection($collection));
$it->rewind();
return $it;
},
$collections
);
do {
$valid = false;
foreach ($iterators as $it) {
if ($it->valid()) {
yield $it->key() => $it->current();
$it->next();
$valid = true;
}
}
} while ($valid);
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection of items in $collection with $value added as first element. If $key is not provided
* it will be next integer index.
*
* @param array|Traversable $collection
* @param mixed $value
* @param mixed|null $key
* @return Collection
*/
function prepend($collection, $value, $key = null)
{
$generatorFactory = function () use ($collection, $value, $key) {
if ($key === null) {
yield $value;
} else {
yield $key => $value;
}
foreach ($collection as $key => $value) {
yield $key => $value;
}
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection of items in $collection with $value added as last element. If $key is not provided
* it will be next integer index.
*
* @param array|Traversable $collection
* @param mixed $value
* @param mixed|null $key
* @return Collection
*/
function append($collection, $value, $key = null)
{
$generatorFactory = function () use ($collection, $value, $key) {
foreach ($collection as $k => $v) {
yield $k => $v;
}
if ($key === null) {
yield $value;
} else {
yield $key => $value;
}
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection by removing items from $collection until first item for which $function returns false.
*
* @param array|Traversable $collection
* @param callable $function ($value, $key)
* @return Collection
*/
function dropWhile($collection, callable $function)
{
$generatorFactory = function () use ($collection, $function) {
$shouldDrop = true;
foreach ($collection as $key => $value) {
if ($shouldDrop) {
$shouldDrop = $function($value, $key);
}
if (!$shouldDrop) {
yield $key => $value;
}
}
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection of items from $collection until first item for which $function returns false.
*
* @param array|Traversable $collection
* @param callable $function ($value, $key)
* @return Collection
*/
function takeWhile($collection, callable $function)
{
$generatorFactory = function () use ($collection, $function) {
$shouldTake = true;
foreach ($collection as $key => $value) {
if ($shouldTake) {
$shouldTake = $function($value, $key);
}
if ($shouldTake) {
yield $key => $value;
}
}
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection. A result of calling map and flatten(1)
*
* @param array|Traversable $collection
* @param callable $function ($value, $key)
* @return Collection
*/
function mapcat($collection, callable $function)
{
return flatten(map($collection, $function), 1);
}
/**
* Returns a lazy collection [take($collection, $position), drop($collection, $position)]
*
* @param array|Traversable $collection
* @param int $position
* @return Collection
*/
function splitAt($collection, $position)
{
$generatorFactory = function () use ($collection, $position) {
yield take($collection, $position);
yield drop($collection, $position);
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection [takeWhile($collection, $function), dropWhile($collection, $function)]
*
* @param array|Traversable $collection
* @param callable $function ($value, $key)
* @return Collection
*/
function splitWith($collection, callable $function)
{
$generatorFactory = function () use ($collection, $function) {
yield takeWhile($collection, $function);
yield dropWhile($collection, $function);
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection with items from $collection but values that are found in keys of $replacementMap
* are replaced by their values.
*
* @param array|Traversable $collection
* @param array|Traversable $replacementMap
* @return Collection
*/
function replace($collection, $replacementMap)
{
$generatorFactory = function () use ($collection, $replacementMap) {
foreach ($collection as $key => $value) {
$newValue = getOrDefault($replacementMap, $value, $value);
yield $key => $newValue;
}
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection of reduction steps.
*
* @param array|Traversable $collection
* @param callable $function
* @param mixed $startValue
* @return Collection
*/
function reductions($collection, callable $function, $startValue)
{
$generatorFactory = function () use ($collection, $function, $startValue) {
$tmp = duplicate($startValue);
yield $tmp;
foreach ($collection as $key => $value) {
$tmp = $function($tmp, $value, $key);
yield $tmp;
}
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection of every nth ($step) item in $collection.
*
* @param array|Traversable $collection
* @param int $step
* @return Collection
*/
function takeNth($collection, $step)
{
$generatorFactory = function () use ($collection, $step) {
$index = 0;
foreach ($collection as $key => $value) {
if ($index % $step == 0) {
yield $key => $value;
}
$index++;
}
};
return new Collection($generatorFactory);
}
/**
* Returns a lazy collection of collections of $numberOfItems items each, at $step step
* apart. If $step is not supplied, defaults to $numberOfItems, i.e. the partitions
* do not overlap. If a $padding collection is supplied, use its elements as
* necessary to complete last partition up to $numberOfItems items. In case there are
* not enough padding elements, return a partition with less than $numberOfItems items.
*
* @param array|Traversable $collection
* @param int $numberOfItems
* @param int $step
* @param array|Traversable $padding
* @return Collection