Last active
August 29, 2015 14:07
-
-
Save mattidupre/f281661fd522b21c308e to your computer and use it in GitHub Desktop.
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
// See http://www.es6fiddle.net/i0n48lf3/ | |
/* BACKGROUND | |
Sometimes with Javascript it makes more sense to define a | |
function's arguments before calling / defining that function, | |
much like (my very limited understanding of) Ruby. | |
An example: | |
: (function($){ ... })(jQuery); | |
Written as: | |
: jQuery do ($)=>{ ... }; | |
This would be more useful when dealing with chained libraries. | |
An example: | |
: var x = lib() | |
: .doThis() | |
: .get() | |
: ; | |
: x = (function(a){ ... })(x); | |
: x = lib(x) | |
: .dothat() | |
: ; | |
: console.log(x); | |
Written as: | |
: lib() | |
: .doThis() | |
: .get() // unwrap library | |
: do x=>{ ... } // perform external function | |
: do lib // rewrap library | |
: .doThat() | |
: do console.log | |
: ; | |
To play with this, I made a quick utility in the style of | |
Highland, Node streams, or Lazypipe. | |
*/ | |
// EXECUTING ((1+1)*2)+2 | |
console.log('Executing ((1+1)*2)+2'); | |
// Using declared functions | |
_(1+1) | |
.d(function (n) { return 2*n; }) | |
.d(function (x, y) { return x + y; }, 2) | |
.d(console.log) | |
; | |
// Using arrow functions (much prettier) | |
_(1+1) | |
.d(x => 2*x, 2) | |
.d((x, y) => x + y, 2) | |
.d(console.log) | |
; | |
// Extracting the value (.v) | |
var foo = _(1+1) | |
.d(x => 2*x, 2) | |
.d((x, y) => x + y, 2) | |
.v; | |
console.log(foo); | |
// Calling chained libraries | |
function lib(n) { | |
var self = { | |
add: function(y) { n += y; return self; }, | |
get: function() { return n; }, | |
}; | |
return self; | |
}; | |
var asdf = _(lib(1)) | |
.d('add', 1) | |
.d('get') | |
.d(x=>x*2) | |
.d(lib) | |
.d('add', 2) | |
.d('get') | |
.d(console.log) | |
; | |
/* HOW I WISH THIS WOULD WORK NATIVELY | |
// Using automatic currying | |
(1+1) | |
do (x => 2*x) // Note automatic execution | |
do ((x, y) => x + y, 2) // Automatic currying | |
do console.log | |
; | |
// Or pass single preceding value as @ | |
(1+1) | |
do (x => 2*x)(@) | |
do ((x, y) => x + y, @, 2) | |
do console.log | |
; | |
// Previous jQuery example | |
$('select') | |
.val() | |
do parseInt(@, 10) | |
do (x)=>{$('section:nth-child('+x+1+')')} | |
.remove() | |
; | |
*/ | |
function _(value) { | |
var initialized = false; | |
return d(value); | |
function d(fn) { // presumably, a "do" function | |
var args = Array.prototype.slice.call(arguments, 1); | |
if (typeof fn === 'function') { | |
value = fn.apply(this, [value].concat(args)); | |
} else if (initialized && typeof fn === 'string') { | |
fn = value[fn]; | |
if (!fn) throw(new Error('Function not found in method chain.')); | |
value = fn.apply(this, args); | |
} else if (arguments.length === 1) { | |
value = fn; | |
} else { | |
throw(new Error('Unknown parameters')); | |
} | |
initialized = true; | |
return { | |
v: value, | |
d: d, | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment