A Pen by Dave Atchley on CodePen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Some common utility combinators and helpers | |
| // | |
| function flip(fn) { | |
| return function() { | |
| var args = [].slice.call(arguments); | |
| return fn.apply(this, args.reverse()); | |
| }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Primary application logic for our Functional Programming blog example | |
| * See related blog series at: http://www.datchley.name/tag/functional-programming/ | |
| * Version: 2.0 | |
| */ | |
| // A simple, resuable comparison for '>=' | |
| function greaterThanOrEqual(a, b) { | |
| return a >= b | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // This is the actual implementation using our minimal functional library | |
| // | |
| var validate = { | |
| 'values': function(o) { return !isNull(o) && !isUndefined(o) && (isBoolean(o) || isNumber(o) || isString(o)); }, | |
| 'arrays': function(o) { return !isNull(o) && !isUndefined(o) && isArray(o); } | |
| } | |
| var build = { | |
| 'values': function(prop, val) { return [prop,"=",qs(val.toString())].join(''); }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ['Function','Array'].forEach(function(type) { | |
| var checkFn = 'is'+type; | |
| window[checkFn] = function(o){ return Object.prototype.toString.call(o) == '[object ' + type + ']'; }; | |
| }); | |
| // Return new list as combination of the two lists passed | |
| // The second list can be a function which will be passed each item | |
| // from the first list and should return an array to permute that item | |
| // with. If either argument is not a list, it will be treated as a list. | |
| // |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let isFunction = function(obj) { | |
| return typeof obj == 'function' || false; | |
| }; | |
| class EventEmitter { | |
| constructor() { | |
| this.listeners = new Map(); | |
| } | |
| addListener(label, callback) { | |
| this.listeners.has(label) || this.listeners.set(label, []); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function range(s, e, res) { | |
| if (!res) { res = []; } | |
| res.push(s); | |
| if (s == e ) { | |
| return res; | |
| } | |
| else { | |
| if (s<e) { s++; } | |
| else { s--; } | |
| return range(s, e, res); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function int2roman(n) { | |
| var map = [ | |
| { factor: 1000, letter: 'M' }, | |
| { factor: 900, letter: 'CM' }, | |
| { factor: 500, letter: 'D' }, | |
| { factor: 400, letter: 'CD' }, | |
| { factor: 100, letter: 'C' }, | |
| { factor: 90, letter: 'XC' }, | |
| { factor: 50, letter: 'L' }, | |
| { factor: 40, letter: 'XL' }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # disable builtin echo so we can use '-en' | |
| enable -n echo | |
| SCRIPT=$(basename "$0") | |
| SCRIPT_DIR=$(dirname "$0") | |
| echo "$SCRIPT running from $SCRIPT_DIR" | |
| # Determine if we're interactive or not |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Sources... | |
| let arr = [1,2,3,4]; | |
| // thunk returning function that takes a callback | |
| // and applies the arg to it after ms milliseconds timeout | |
| function delay(arg, ms=1000) { | |
| return function(callback) { | |
| setTimeout(_=>callback(arg), ms); | |
| }; | |
| } |