Skip to content

Instantly share code, notes, and snippets.

@fuzzyalej
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save fuzzyalej/cf3870103cd28206f780 to your computer and use it in GitHub Desktop.

Select an option

Save fuzzyalej/cf3870103cd28206f780 to your computer and use it in GitHub Desktop.

Revisions

  1. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -206,3 +206,10 @@ function fork(predicate, f, g) {
    return g.apply(null, arguments);
    };
    }

    function serial(f, g) {
    return function() {
    f.apply(null, arguments);
    g.apply(null, arguments);
    };
    }
  2. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -197,3 +197,12 @@ function curry(fn) {
    }
    return stillCurrying([]);
    }

    function fork(predicate, f, g) {
    return function() {
    if(predicate.apply(null, arguments))
    return f.apply(null, arguments);
    else
    return g.apply(null, arguments);
    };
    }
  3. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -182,3 +182,18 @@ function partial(fn) {
    };
    }

    function curry(fn) {
    n = fn.length;

    function stillCurrying(prev) {
    return function(arg) {
    var args = prev.concat(arg);
    if (args.length < n) {
    return stillCurrying(args);
    } else {
    return fn.apply(this, args);
    }
    };
    }
    return stillCurrying([]);
    }
  4. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -181,4 +181,4 @@ function partial(fn) {
    return fn.apply(this, args.concat(args2));
    };
    }
    // This is the simpler one, we can create partial functions to fix middle or end arguments!

  5. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -174,3 +174,11 @@ function memoize(fn) {
    };
    }

    function partial(fn) {
    var args = Array.prototype.slice.call(arguments, 1);
    return function() {
    var args2 = Array.prototype.slice.call(arguments);
    return fn.apply(this, args.concat(args2));
    };
    }
    // This is the simpler one, we can create partial functions to fix middle or end arguments!
  6. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -164,4 +164,13 @@ function groupBy(list, fn) {
    recurr(groups, list.next, fn);
    return groups;
    }({}, list, fn));
    }
    }

    function memoize(fn) {
    var cache = {};
    return function(argument) {
    cache[argument] = cache[argument] || fn(argument);
    return cache[argument];
    };
    }

  7. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 0 additions and 7 deletions.
    7 changes: 0 additions & 7 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -81,13 +81,6 @@ function append(list1, list2) {
    return cons(first(list1), append(rest(list1), list2));
    }

    function append(list1, list2) {
    if(not(listp(list1)) && not(listp(list2))) return null;

    if(rest(list1) === null) { return cons(first(list1), list2); }
    return cons(first(list1), append(rest(list1), list2));
    }

    function reverse(list) {
    if(not(listp(list))) return null;
    if(rest(list) === null) return cell(first(list)); //!
  8. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 0 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -140,12 +140,6 @@ function every(list, fn) {
    });
    }

    function every(list, fn) {
    return reduce(list, true, function(acc, el) {
    return acc && fn(el);
    });
    }

    function any(list, fn) {
    return reduce(list, false, function(acc, el) {
    return acc || fn(el);
  9. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -166,3 +166,15 @@ function partition(list, fn) {
    return [group1, group2];
    }([], [], list, fn));
    }

    function groupBy(list, fn) {
    return(function recurr(groups, list, fn){
    result = fn(list.value);
    (groups[result] = groups[result] || []).push(list.value);

    if(list.next === null) return groups;

    recurr(groups, list.next, fn);
    return groups;
    }({}, list, fn));
    }
  10. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -151,3 +151,18 @@ function any(list, fn) {
    return acc || fn(el);
    });
    }

    function partition(list, fn) {
    return(function recurr(group1, group2, list, fn){
    if(fn(list.value)) {
    group1.push(list.value);
    } else {
    group2.push(list.value);
    }

    if(list.next === null) return;

    recurr(group1, group2, list.next, fn);
    return [group1, group2];
    }([], [], list, fn));
    }
  11. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -146,3 +146,8 @@ function every(list, fn) {
    });
    }

    function any(list, fn) {
    return reduce(list, false, function(acc, el) {
    return acc || fn(el);
    });
    }
  12. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -140,3 +140,9 @@ function every(list, fn) {
    });
    }

    function every(list, fn) {
    return reduce(list, true, function(acc, el) {
    return acc && fn(el);
    });
    }

  13. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -134,4 +134,9 @@ function reduceRight(list, initial_value, fn) {
    }
    var foldR = reduceRight;

    function every(list, fn) {
    return reduce(list, true, function(acc, el) {
    return acc && fn(el);
    });
    }

  14. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -128,5 +128,10 @@ function reduce(list, initial_value, fn) {
    }
    var fold = reduce;

    function reduceRight(list, initial_value, fn) {
    if(list === null) return initial_value;
    return fn(reduceRight(rest(list), initial_value, fn), first(list));
    }
    var foldR = reduceRight;


  15. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -122,5 +122,11 @@ function find(list, predicatefn) {
    return first(filter(list, predicatefn));
    }

    function reduce(list, initial_value, fn) {
    if(list === null) return initial_value;
    return reduce(rest(list), fn(initial_value, first(list)), fn);
    }
    var fold = reduce;



  16. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -118,6 +118,9 @@ function count(predicate, list) {
    return length(filter(list, predicate));
    }

    function find(list, predicatefn) {
    return first(filter(list, predicatefn));
    }



  17. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -114,6 +114,10 @@ function filter(list, predicatefn) {
    }
    }

    function count(predicate, list) {
    return length(filter(list, predicate));
    }




  18. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -107,8 +107,11 @@ function map(list, fn) {

    function filter(list, predicatefn) {
    if(not(listp(list))) return null;
    if(predicatefn(first(list))) return cons(first(list), filter(rest(list), predicatefn));
    return filter(rest(list), predicatefn);
    if(predicatefn(first(list))) {
    return cons(first(list), filter(rest(list), predicatefn));
    } else {
    return filter(rest(list), predicatefn);
    }
    }


  19. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -105,6 +105,12 @@ function map(list, fn) {
    return cons(fn(first(list)), map(rest(list), fn));
    }

    function filter(list, predicatefn) {
    if(not(listp(list))) return null;
    if(predicatefn(first(list))) return cons(first(list), filter(rest(list), predicatefn));
    return filter(rest(list), predicatefn);
    }




  20. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -100,5 +100,11 @@ function remove(value, list) {
    return cons(first(list), remove(value, rest(list)));
    }

    function map(list, fn) {
    if(not(listp(list))) return null;
    return cons(fn(first(list)), map(rest(list), fn));
    }




  21. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -94,5 +94,11 @@ function reverse(list) {
    return append(reverse(rest(list)), cell(first(list)));
    }

    function remove(value, list) {
    if(not(listp(list))) return null;
    if(first(list) === value) return remove(value, rest(list));
    return cons(first(list), remove(value, rest(list)));
    }



  22. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -88,6 +88,11 @@ function append(list1, list2) {
    return cons(first(list1), append(rest(list1), list2));
    }

    function reverse(list) {
    if(not(listp(list))) return null;
    if(rest(list) === null) return cell(first(list)); //!
    return append(reverse(rest(list)), cell(first(list)));
    }



  23. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -81,4 +81,13 @@ function append(list1, list2) {
    return cons(first(list1), append(rest(list1), list2));
    }

    function append(list1, list2) {
    if(not(listp(list1)) && not(listp(list2))) return null;

    if(rest(list1) === null) { return cons(first(list1), list2); }
    return cons(first(list1), append(rest(list1), list2));
    }




  24. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -73,3 +73,12 @@ function print(list) {
    if(listp(first(list))) return '(' + print(first(list)) + '' + print(rest(list)) + ')';
    return ('' + first(list) + ' ' + print(rest(list)));
    }

    function append(list1, list2) {
    if(not(listp(list1)) && not(listp(list2))) return null;

    if(rest(list1) === null) { return cons(first(list1), list2); }
    return cons(first(list1), append(rest(list1), list2));
    }


  25. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -68,4 +68,8 @@ function predicatep(input) { return input === true || input === false; }

    function not(predicate) { return !!!predicate; }


    function print(list) {
    if(list === null) return '';
    if(listp(first(list))) return '(' + print(first(list)) + '' + print(rest(list)) + ')';
    return ('' + first(list) + ' ' + print(rest(list)));
    }
  26. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 15 additions and 3 deletions.
    18 changes: 15 additions & 3 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -53,7 +53,19 @@ function nth(position, list) {
    return nth(position-1, rest(list));
    }

    function print(list) {
    if(list === null) return '';
    return ('' + first(list) + ' ' + print(rest(list)));
    function listp(input) {
    if(input === null) return null;
    return (typeof(input) === 'object')
    && input.hasOwnProperty('value')
    && input.hasOwnProperty('next');
    }

    function atomp(input) { return !listp(input); }

    function nullp(input) { return input === null; }

    function predicatep(input) { return input === true || input === false; }

    function not(predicate) { return !!!predicate; }


  27. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -53,4 +53,7 @@ function nth(position, list) {
    return nth(position-1, rest(list));
    }


    function print(list) {
    if(list === null) return '';
    return ('' + first(list) + ' ' + print(rest(list)));
    }
  28. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 13 additions and 2 deletions.
    15 changes: 13 additions & 2 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -38,8 +38,19 @@ function last(list) {
    return last(rest(list));
    }


    function length(list) {
    if(list === null) return 0;
    return 1 + length(rest(list));
    }
    }

    function replace_first(value, list) {
    return cons(value, rest(list));
    }

    function nth(position, list) {
    if(position > length(list) || position < 1) return null;
    if(position === 1) return first(list);
    return nth(position-1, rest(list));
    }


  29. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -36,4 +36,10 @@ var tail = rest;
    function last(list) {
    if(rest(list) === null) return first(list);
    return last(rest(list));
    }


    function length(list) {
    if(list === null) return 0;
    return 1 + length(rest(list));
    }
  30. Alejandro Andrés revised this gist Feb 27, 2015. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion functional_javascript.js
    Original file line number Diff line number Diff line change
    @@ -31,4 +31,9 @@ function rest(list) {
    return list.next;
    }
    var cdr = rest;
    var tail = rest;
    var tail = rest;

    function last(list) {
    if(rest(list) === null) return first(list);
    return last(rest(list));
    }