Completed
Push — master ( fa405b...7b8826 )
by Woody
23s
created

invert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Dryist;
4
5
/**
6
 * Create a modifier that always returns the same value.
7
 *
8
 * Also known as the Kestrel or "K" combinator.
9
 *
10
 * @see https://en.wikipedia.org/wiki/SKI_combinator_calculus
11
 *
12
 * @param mixed $x
13
 * @return callable () => x
14
 */
15
function always($x): callable
16
{
17
    return function () use ($x) {
18 1
        return $x;
19 1
    };
20
}
21
22
/**
23
 * Create a composition of two modifiers.
24
 *
25
 * Also known as the Substition or "S" combinator.
26
 *
27
 * @return callable (z) => x(y(z))
28
 */
29
function compose(callable $x, callable $y): callable
30
{
31
    return function ($z) use ($x, $y) {
32 1
        return $x($y($z));
33 1
    };
34
}
35
36
/**
37
 * @alias identity()
38
 */
39
function id($x)
40
{
41 1
    return identity($x);
42
}
43
44
/**
45
 * Return any given variable.
46
 *
47
 * Also known as the Identity or "I" combinator.
48
 *
49
 * @see https://en.wikipedia.org/wiki/SKI_combinator_calculus
50
 *
51
 * @param mixed $x
52
 * @return mixed The value of $x
53
 */
54
function identity($x)
55
{
56 1
    return $x;
57
}
58
59
/**
60
 * Create a negated predicate.
61
 *
62
 * @return callable (y) => ! x(y)
63
 */
64
function invert(callable $x): callable
65
{
66
    return function ($y) use ($x): bool {
67 1
        return ! $x($y);
68 1
    };
69
}
70