Completed
Push — master ( bb2114...3df708 )
by Dave
9s
created

Either.php ➔ either()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 11
rs 9.4285
1
<?php
2
3
namespace DaveRoss\FunctionalProgrammingUtils;
4
5
/**
6
 * Class Either
7
 * Base class for an Either Monad
8
 * @package DaveRoss\FunctionalProgrammingUtils
9
 */
10
abstract class Either extends Monad
11
{
12
}
13
14
/**
15
 * Helper function to apply one of a pair of function to Either a Left or Right . Returns the value directly rather
16
 * than wrapping it in another Either.
17
 *
18
 * @param callable $f function to apply if $e is a Left
19
 * @param callable $g function to apply if $e is a Right
20
 * @param Either   $e
21
 *
22
 * @return mixed
23
 */
24
function either(callable $f, callable $g, Either $e)
25
{
26
27
    switch (get_class($e)) {
28
        case 'DaveRoss\FunctionalProgrammingUtils\Left':
29
            return $f( $e->value() );
30
        case 'DaveRoss\FunctionalProgrammingUtils\Right':
31
            return $g( $e->value() );
32
    }
33
34
}