Report abuse

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
196
197
198
199
200
package intelligentpathways.utils
{
    /**
     * Holds functional-style helpers.
     */
    public class F
    {
        /**
         * Generates a simple function that ignores all parameters and returns the requested value.
         */
        public static function functionReturning(returnValue : *) : Function
        {
            return function(...rest) : *
                {
                    return returnValue;
                }
        }

        /**
         * Does nothing.
         * @param ignored
         */
        public static function noop(...ignored) : void {}

        /**
        * Reduces the incoming parameter count when the method is invoked. Sort of an uncurry(?), but throws out the extra parameters.
        *
        * Example use : Array.forEach() calls a Function of type f(item : *, i : Number, source : Array) - but you're only interested
        * in the item parameter. You call would call: myArray.forEach(F.ignoreParameters(1, myOneParamFunction));
        *
        * @param targetFunction The function you wish to wrap
        * @param toCount The number of parameters you wish to pass through when calling the targetFunction
        */
        public static function ignoreParameters(targetFunction : Function, toCount : uint = 0) : Function
        {
            if (toCount == 0)
            {
                return function (...rest) : *
                    {
                        targetFunction();
                    };
            }

            return function (...rest) : *
                {
                    if (rest.length <= toCount)
                    {
                        //Not enough params to throw out some, but try anyway, target function might be able to deal with it.
                        return targetFunction.apply(null, rest);
                    }

                    var tmp : Array = rest.slice(0, toCount);
                    return targetFunction.apply(null, tmp);
               }
        }

        /**
         * Calls a function n times, returning the results in an array
         *
         * @parameter times The repeat count
         * @parameter callback A function. Signature is "f(i : Number) : *" - return null, it goes in the list. Return void or undefined and it won't.
         *
         * @return an array of all callback results that weren't undefined or null.
         */
        public static function repeat(times : uint, callback : Function) : Array
        {
            var collectedResults : Array = [];
            var i : uint;

            for (i = 0; i < times; i++)
            {
                var funcResult : * = undefined;
                funcResult = callback(i);
                if (funcResult !== undefined) //NB: !== instead of !=
                    collectedResults.push(funcResult);
            }

            return collectedResults;
        }

        /**
        * Beef rendang
        */
        public static function curry(targetFunction : Function, firstParameter : *) : Function
        {
            return function(...rest) : *
                {
                    (rest as Array).unshift(firstParameter);
                    targetFunction.apply(null, rest);
                }
        }

        /**
        * Reduce / fold left. Non-recursive version since we've no tail-calls in AS, even if it's far less pretty than the proper version.
        *
        * @param f a function of signature f(lastValue : *, nextInput : *) : *
        *
        * TODO: Test this
        */
        public static function reduceLeft(f : Function, seedInput : *, input : Array) : *
        {
            var lastValue : * = seedInput;
            var i : uint = 0;
            input = input ? input : [];

            for (i = 0; i < input.length; i++)
            {
                lastValue = f(lastValue, input[i]);
            }

            return lastValue;
        }

        /**
        * Reduce / fold right. Non-recursive version since we've no tail-calls in AS, even if it's far less pretty than the proper version.
        *
        * @param f a function of signature f(lastValue : *, nextInput : *) : *
        *
        * TODO: Test this
        */
        public static function reduceRight(f : Function, seedInput : *, input : Array) : *
        {
            var lastValue : * = seedInput;
            var i : uint = 0;
            input = input ? input : [];

            for (i = input.length; i >=0 ; i--)
            {
                lastValue = f(lastValue, input[i]);
            }

            return lastValue;
        }

        /**
        * This is just here as a Function in order to be reused with these helpers
        */
        public static function equal(left : *, right : *) : Boolean
        {
            return left == right;
        }

        /**
        * This is just here as a Function in order to be reused with these helpers
        */
        public static function inequal(left : *, right : *) : Boolean
        {
            return !equal(left, right);
        }

        /**
        * A helper forEach that's nicer than Array.forEach (target only takes one param) and supports any iterable.
        *
        * @param targetFunction a funciont of signature f(item : *) : void {}
        */
        public static function forEach(list : Object, targetFunction : Function) : void
        {
            for each (var item : * in list)
            {
                targetFunction(item);
            }
        }

        /**
         * TODO: DOCUMENT ME!
         *
         * @param list
         * @param targetFunction
         * @return
         *
         */
        public static function map(list : Object, targetFunction : Function) : Array
        {
            var arr : Array = [];

            for each (var item : * in list)
            {
                arr.push(targetFunction(item));
            }

            return arr;
        }

        /**
        * A helper eqiv of Array.every() that's nicer than Array.every (target only takes one param) and supports any iterable.
        *
        * @param targetFunction a funciont of signature f(item : *) : Boolean {}
        */
        public static function all(list : Object, targetFunction : Function) : Boolean
        {
            for each (var item : * in list)
            {
                if (!targetFunction(item))
                    return false;
            }

            return true;
        }
    }
}