Completed
Push — master ( 713d54...d55531 )
by Amine
02:08
created

functions.php ➔ f()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace Tarsana\Functional;
2
/**
3
 * This file contains functions dealing with functions.
4
 */
5
6
use Tarsana\Functional\Exceptions\InvalidArgument;
7
8
/**
9
 * Returns a curried equivalent of the provided function.
10
 * ```php
11
 * $add = curry(function($x, $y){
12
 *     return $x + $y;
13
 * });
14
 * $addFive = $add(5); // a function
15
 * $addFive(5); // 10
16
 * $add(5, 5) // 10
17
 * ```
18
 *
19
 * @signature (* -> a) -> (* -> a)
20
 * @param  callable $fn
21
 * @return callable
22
 */
23
function curry(callable $fn) {
24
    return \Cypress\Curry\curry($fn);
25
}
26
27
/**
28
 * Argument placeholder to use with curried functions.
29
 * ```php
30
 * $minus = curry(function ($x, $y) { return $x - $y; });
31
 * $decrement = $minus(__(), 1);
32
 * $decrement(10) // 9
33
 *
34
 * $reduce = curry('array_reduce');
35
 * $sum = $reduce(__(), 'Tarsana\Functional\plus');
36
 * $sum([1, 2, 3, 4], 0) // 10
37
 * ```
38
 *
39
 * @signature * -> Placeholder
40
 * @return \Cypress\Curry\Placeholder
41
 */
42
function __() {
43
    return \Cypress\Curry\__();
44
}
45
46
/**
47
 * Non curried version of apply for internal use.
48
 *
49
 * @internal
50
 * @param  callable $fn
51
 * @param  array    $args
52
 * @return mixed
53
 */
54
function _apply($fn, $args) {
55
    return call_user_func_array($fn, $args);
56
}
57
58
/**
59
 * Apply the provided function to the list of arguments.
60
 * ```php
61
 * apply('strlen', ['Hello']) // 5
62
 * $replace = apply('str_replace');
63
 * $replace(['l', 'o', 'Hello']) // 'Heooo'
64
 * ```
65
 *
66
 * @signature (*... -> a) -> [*] -> a
67
 * @param  callable $fn
0 ignored issues
show
Bug introduced by
There is no parameter named $fn. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
68
 * @param  array    $args
0 ignored issues
show
Bug introduced by
There is no parameter named $args. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
69
 * @return mixed
70
 */
71
function apply() {
72
    return _apply(curry('Tarsana\Functional\_apply'), func_get_args());
73
}
74
75
/**
76
 * Performs left-to-right function composition.
77
 * The leftmost function may have any arity;
78
 * the remaining functions must be unary.
79
 * The result of pipe is curried.
80
 * **Calling pipe() without any argument throws Tarsana\Functional\Exceptions\InvalidArgument**
81
 * ```php
82
 * function add($x, $y) { return $x + $y; }
83
 * $double = function($x) { return 2 * $x; };
84
 * $addThenDouble = pipe('add', $double);
85
 * $addThenDouble(2, 3) // 10
86
 * ```
87
 *
88
 * @signature (((a, b, ...) -> o), (o -> p), ..., (y -> z)) -> ((a, b, ...) -> z)
89
 * @param  callable ...$fns
90
 * @return callable
91
 */
92
function pipe() {
93
    $fns = func_get_args();
94
    if(count($fns) < 1)
95
        throw new InvalidArgument("pipe() requires at least one argument");
96
    return curry(function () use ($fns) {
97
        $result = _apply(array_shift($fns), func_get_args());
98
        foreach ($fns as $fn) {
99
            $result = $fn($result);
100
        }
101
        return $result;
102
    });
103
}
104
105
/**
106
 * A function that takes one argument and
107
 * returns exactly the given argument.
108
 * ```php
109
 * identity('Hello') // 'Hello'
110
 * identity([1, 2, 3]) // [1, 2, 3]
111
 * identity(null) // null
112
 * ```
113
 *
114
 * @signature * -> *
115
 * @return mixed
116
 */
117
function identity() {
118
    return apply(curry(function($value) {
119
        return $value;
120
    }), func_get_args());
121
}