Last active
September 25, 2015 23:20
-
-
Save jaw6/0918fa4c615f40e7b49e to your computer and use it in GitHub Desktop.
This file contains 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.prototype.partial = function() { | |
var fn = this, args = Array.prototype.slice.call(arguments) | |
args = [].concat.apply([], args) // flatten the args array | |
return function() { | |
var args2 = Array.prototype.slice.call(arguments) | |
args2 = [].concat.apply([], args2) // flatten the args array | |
return fn.apply(fn, args.concat(args2)) | |
} | |
} | |
Function.prototype.curry = function() { | |
var fn = this, args = Array.prototype.slice.call(arguments) | |
args = [].concat.apply([], args) // flatten the args array | |
var argsLeft = fn.length - args.length | |
if (argsLeft == 1) { | |
return fn.partial(args) | |
} else { | |
return function() { | |
var args2 = Array.prototype.slice.call(arguments) | |
return fn.curry.apply(fn, args.concat(args2)) | |
} | |
} | |
} | |
function converter(symbol, factor, input) { | |
return input * factor + symbol | |
} | |
var milesToKM = converter.curry('km')(1.609) | |
var lbToKG = converter.curry('kg', 0.453) | |
var usdToEuro = converter.curry(['€', 0.90]) | |
console.log(milesToKM(500)) | |
console.log(lbToKG(200)) | |
console.log(usdToEuro(20)) |
This file contains 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 converter(symbol, factor, input) { | |
return input * factor + symbol | |
} | |
// km = miles * 1.609 | |
// kg = lb * 0.453 | |
// euro = USD * 0.90 | |
/* | |
Challenge: Implement Function.prototype.partial | |
and/or Function.prototype.curry so that we | |
can do things like: | |
milesToKm = converter.partial('km', 1.609) | |
lbsToKg = converter.curry('kg')(0.453) | |
*/ |
This file contains 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
// Actual JS (< ES6) | |
function Person(first, last) { | |
if (this instanceof Person) { | |
this.first = first | |
this.last = last | |
} else { | |
return new Person(first, last) | |
} | |
} | |
Person.prototype.name = function() { | |
return this.first + " " + this.last | |
} | |
joshua = new Person('Joshua', 'Wehner') | |
joshua.name() |
This file contains 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
// ES6 | |
class Person { | |
constructor(first, last) { | |
this.first = first | |
this.last = last | |
Person.count++ | |
} | |
get name() { | |
return this.first + " " + this.last | |
} | |
static get count() { | |
return !this._count ? 0 : this._count | |
} | |
static set count(n) { | |
this._count = n | |
} | |
} | |
class FancyDiv { | |
constructor(id) { | |
this.id = id | |
FancyDiv.myDivs = this | |
} | |
static get myDivs() { | |
return !this._myDivs ? [] : this._myDivs | |
} | |
static set myDivs(div) { | |
this._myDivs = this.myDivs | |
this._myDivs = this._myDivs.concat(div) | |
} | |
static find(id) { | |
for (var div of this.myDivs) { | |
if (div.id === id) { | |
return div | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment