fn.php

Gesponsorde koppelingen

PHP script bestanden

  1. fn.php

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php

namespace Wj\fn;

function
operator($operator)
{

    if ('^' === $operator) {
        return function ($a, $b) { return pow($a, $b); };
    }


    return eval('return function ($a, $b) { return $a '.$operator.' $b; };');
}

function
curry()
{

    $boundArgs = func_get_args();
    if (is_callable(current($boundArgs))) {
        // left curry
        $func = current($boundArgs);
        array_shift($boundArgs);

        return function () use ($func, $boundArgs) {
            $callArgs = func_get_args();

            return call_user_func_array($func, array_merge($callArgs, $boundArgs));
        };
    }


    if (is_callable(end($boundArgs))) {
        // right curry
        $func = end($boundArgs);
        array_pop($boundArgs);

        return function () use ($func, $boundArgs) {
            $callArgs = func_get_args();

            return call_user_func_array($func, array_merge($boundArgs, $callArgs));
        };
    }
}


class SuccessiveIterator implements \Iterator
{
    private $acc;
    private $initialValue;
    private $step;
    private $end;

    public function __construct($initialValue = 0, $step = 1, $end = false)
    {

        $this->initialValue = $initialValue;
        $this->step = $step;
        $this->end = $end;
    }


    public function current()
    {

        return $this->acc;
    }


    public function next()
    {

        $this->acc += $this->step;
    }


    public function prev()
    {

        $this->acc -= $this->step;
    }


    public function valid()
    {

        if (false !== $this->end) {
            return ($this->step > 0 ? $this->acc <= $this->end : $this->acc >= $this->end);
        }


        return true;
    }


    public function rewind()
    {

        $this->acc = $this->initialValue;
    }


    public function key()
    {

        return null;
    }
}


abstract class CallableIterator extends \IteratorIterator
{
    protected $callback;

    public function __construct(\Traversable $iterator, $callback)
    {

        if (!is_callable($callback)) {
            throw new \InvalidArgumentException('CallbackIterator::__construct() expects second argument to be callable.');
        }


        $this->callback = $callback;
        parent::__construct($iterator);
    }
}


class CallbackIterator extends CallableIterator
{
    public function current()
    {

        $f = $this->callback;

        return $f(parent::current());
    }
}


class TakeIterator extends CallableIterator
{
    public function valid()
    {

        if (!parent::valid()) {
            return false;
        }


        $f = $this->callback;

        return $f($this->current());
    }
}

function
range($start, $end)
{

    switch (func_num_args()) {
    case
2:
        return new SuccessiveIterator($start, 1, $end);
    case
3:
        $args = func_get_args();

        return new SuccessiveIterator($start, $end - $start, $args[2]);
    default:

        throw new \InvalidArgumentException(sprintf('Range expected two or three arguments, %s given.', func_num_args()));
    }
}

function
map($callback, \Traversable $subject)
{

    return new CallbackIterator($subject, $callback);
}

function
reduce($callback, \Traversable $subject, $acc = null)
{

    foreach ($subject as $s) {
        $acc = $callback($acc, $s);
    }


    return $acc;
}

function
filter($callback, \Traversable $subject)
{

    return new \CallbackFilterIterator($subject, $callback);
}

function
takeWhile($callback, \Traversable $subject)
{

    return new TakeIterator($subject, $callback);
}

function
to_iterator(array $subject)
{

    return new \ArrayIterator($subject);
}

function
to_array(\Traversable $subject)
{

    $array = array();

    foreach ($subject as $s) {
        $array[] = $s;
    }


    return $array;
}


// som van de eerste 5 even getallen.
// 2 + 4 + 6 + 8 + 10 = 30
//     reduce(operator('+'), map(curry(operator('*'), 2), range(1,5)));

// eerste tien tientallen
//     to_array(map(curry(operator('*'), 10), range(1, 10)));

// aantal even getallen kleiner of gelijk aan 100
//     count(to_array(takeWhile(curry(operator('<='), 100), map(curry(operator('*'), 2), range(1, INF)))));

// aantal kwadraten onder de 100

//     count(to_array(takeWhile(curry(operator('<='), 100), map(curry(operator('^'), 2), range(1, INF)))));

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.