Last active
August 29, 2015 14:16
-
-
Save imbcmdth/82faa814ab17f1c4c15d to your computer and use it in GitHub Desktop.
Masala Example
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_MODULE.js */ | |
var masala = require('masala'); | |
// The default arguments have a lot of unbound parameters (something like 12) | |
// They specify a myriad of arguments from invocation-specific parameters | |
// to general options that should be mostly shared between all invocations | |
var defaultArguments = { ... }; | |
function genericFunction (options, callback) { ... } | |
// masala automatically assumes the first parameter of "generalFunction" | |
// is the options-object unless you specify otherwise | |
var generalFnMasala = masala(generalFunction, defaultArguments); | |
// By partially-applying the generic function we can create very | |
// specialized variants that each do *markedly different* work | |
var specializedFnA = generalFnMasala({ ..specific arguments.. }); | |
var specializedFnB = generalFnMasala({ ..specific arguments.. }); | |
// Here we partially-apply the functions with arguments that should be specified | |
// only once per module. Things like logging functions, directory paths, etc. | |
module.exports = function makeExportObject (moduleOptions) { | |
return { | |
fnA: specializedFnA(moduleOptions), | |
fnB: specializedFnB(moduleOptions) | |
}; | |
}; | |
//-------------------------------------------------------------------------------- | |
/* USING_MODULE.js */ | |
var async = require('async'); | |
// Even after the module-level options there are unbound parameters so masala continues | |
// to return functions here instead of executing them | |
var specialFns = require('./SOME_MODULE.js')({ ..module-level arguments.. }); | |
// The key trick below is that masala ALSO automatically curries any remaining arguments | |
// That means that in the invocations below, though the option-object is completely | |
// specified (every argument that was unbound is now bound) masala still returns an function | |
// expecting one more argument - the `callback` parameter. | |
// The waterfall supplies that callback and execution of the very-very specialized functions | |
// can finally occur. | |
async.waterfall([ | |
specialFns.fnA({ ..final arguments.. }), | |
specialFns.fnB({ ..final arguments.. }) | |
], | |
function done () { ... }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment