Demo entry 6353109

test

   

Submitted by anonymous on Mar 29, 2017 at 11:28
Language: Swift. Code size: 2.5 kB.

func raiseError(msg) {
    Fiber.abort(msg);
}

func reverseArgs(fn) {
    return func(/* args */) {
        var revertedArgs = [];
        for (var i in (_args.count - 1) ... 0) {
            revertedArgs.push(_args[i]);
        }
        return fn.apply(null, revertedArgs);
    }
}

func compose(/* fns */) {
    if (_args.count < 1) {
        return raiseError('Arguments must contain at least one function.');
    }
    var lastFn = _args.pop();
    var otherFns = _args;

    return func(/* first fn args */) {
        var result = lastFn.apply(null, _args);
        for (var i in (otherFns.count - 1) ... 0) {
            var fn = otherFns[i];
            result = fn(result);
        }
        return result;
    }
}

var pipe = reverseArgs(compose);

func curry (arity, fn) {
    var accumulatedArgs = [];
    return func() {
        accumulatedArgs = concat(accumulatedArgs, _args);

        if (accumulatedArgs.count == arity) {
            return fn.apply(null, accumulatedArgs);
        }
        return _func;
    };
}

func concat(arr1, arr2) {
    var result = [];
    for (var item in arr1) {
        result.push(item);
    }
    for (var item in arr2) {
        result.push(item);
    }
    return result
}

func partial(fn /*...args*/) {
    var argsToApply = [];

    for (var i in 1 ..< _args.count) {
        argsToApply.push(_args[i]);
    }

    return func() {
        var args = concat(argsToApply, _args);
        return fn.apply(null, args);
    }
}

var map = curry(2, func(fn, data) {
    if (!(fn is Closure)) {
        return raiseError('First argument must be a function.');
    }

    var results = [];
    var idx = 0;

    for (var item in data) {
        results[idx] = fn(item, idx);
        idx += 1;
    }

    return results;
});

var filter = curry(2, func(fn, data) {
    if (!(fn is Closure)) {
        return raiseError('First argument must be a function.');
    }

    var results = [];
    var idx = 0;

    for (var item in data) {
        if ( fn(item, idx) ) {
            results[idx] = item;
            idx += 1;
        }
    }

    return results;
});


var reduce = curry(3, func(fn, accumulator, data) {
    if (!(fn is Closure)) {
        return raiseError('First argument must be a function.');
    }

    var result = accumulator;
    var idx = 0;

    for (var item in data) {
        result = fn(result, item, idx);
        idx += 1;
    }

    return result;
});

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).