GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

functions.php ➔ _chunks()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace FunctionalPHP\Parallel;
4
5
/**
6
 * @param int $threads number of threads to use
7
 * @param callable $callable the callable used for mapping
8
 * @param array|\Traversable $collection the collection you want to map over
9
 * @return mixed the result
10
 */
11
function map($threads, callable $callable, $collection)
12
{
13
    $results = _parallel($threads, function(array $chunk) use($callable) {
14
        return array_map($callable, $chunk);
15
    }, $collection);
16
17
    return call_user_func_array('array_merge', $results);
18
}
19
20
/**
21
 * @param int $threads number of threads to use
22
 * @param callable $predicate the predicate used for filtering
23
 * @param array|\Traversable $collection the collection you want to filter
24
 * @return mixed the result
25
 */
26
function filter($threads, callable $predicate, $collection)
27
{
28
    $results = _parallel($threads, function(array $chunk) use($predicate) {
29
        return array_filter($chunk, $predicate);
30
    }, $collection);
31
32
    return call_user_func_array('array_merge', $results);
33
}
34
35
/**
36
 * @param int $threads number of threads to use
37
 * @param callable $callable the callable used for folding
38
 * @param array|\Traversable $collection the collection you want to fold
39
 * @param mixed $initial initial value for the fold
40
 * @return mixed the result
41
 */
42
function fold($threads, callable $callable, $collection, $initial)
43
{
44
    $func = function(array $chunk) use($callable, $initial) {
45
        return array_reduce($chunk, $callable, $initial);
46
    };
47
48
    $results = _parallel($threads, $func, $collection);
49
50
    return $func($results);
51
}
52
53
/**
54
 * @param int $threads
55
 * @param callable $callable
56
 * @param array|\Traversable $collection
57
 * @return array
58
 */
59
function _parallel($threads, callable $callable, $collection)
60
{
61
    $threads = array_map(function($chunk) use($callable) {
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $threads. This often makes code more readable.
Loading history...
62
        return new Parallel($callable, $chunk);
63
    }, _chunks($threads, $collection));
64
65
    return array_map(function(Parallel $t) {
66
        return $t->get();
67
    }, $threads);
68
}
69
70
/**
71
 * @param int $size
72
 * @param array|\Traversable  $collection
73
 * @return array
74
 */
75
function _chunks($size, $collection)
76
{
77
    $collection = is_array($collection) ? $collection : iterator_to_array($collection);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $collection. This often makes code more readable.
Loading history...
78
    return array_chunk($collection, ceil(count($collection) / $size));
79
}
80