Skip to content

Instantly share code, notes, and snippets.

@gordonbrander
Created July 12, 2012 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gordonbrander/3099349 to your computer and use it in GitHub Desktop.
Save gordonbrander/3099349 to your computer and use it in GitHub Desktop.
toMethod and toLambda - higher order helpers for codebases that use both functional and OOP styles
// Convert a function that does not use `this` context into a function that
// does. The category of information that was previously passed as the first
// parameter of the function is now its `this` context.
function toMethod(lambda) {
return function () {
var args = [this];
var concat = args.concat;
// Spread arguments over arity of concat, since arguments is not a true array.
var combined = concat.apply(args, arguments);
return lambda.apply(null, combined);
};
}
// Convert a method func that uses a `this` context into a function that takes
// the context object as the first parameter.
function toLambda(method) {
return function (context) {
var args = Array.prototype.slice.call(arguments, 1);
return method.apply(context, args);
};
}
@gordonbrander
Copy link
Author

I created these helpers after thinking about the functions as methods thread on ES Discuss. It seems to me, with Fat Arrows and classes, the language mechanics divide between methods and functions in ES will probably get wider, not narrower. This is unfortunate for code-bases that use a mix of functional and OOP styles, because it makes code-reuse more difficult. If it were possible to write functions that work in both a functional and OOP style, you could easily swap between the paradigms, using whatever style suites the application.

As it stands today, we have to wrap functions to massage away the difference. But hey, at least we don't have to do the wrapping by hand! Here are a couple of higher-order functions that will convert a function to work with one paradigm or the other. Be sure you know what the contract is for your function, though -- it wouldn't make sense to wrap a method in a method wrapper, you know!

Here's an example of these two helpers in action:

var o = {
    mSet: function (key, value) {
        this[key] = value;
        return this;
    }
};

var fGet = function (o, key) {
    return o[key];
};

o.mGet = toMethod(fGet);

o.mSet('bloop', true);
o.mGet('bloop'); // true

var fSet = toLambda(o.mSet);

fSet(o, 'bloop', false);
fGet(o, 'bloop'); // false

@gordonbrander
Copy link
Author

Also note that if a bound function is passed to toFunction, the resulting function will throw away the first argument when called. No way around that, but one can assume that functions which bind this know their own implementation and can fend for themselves.

@Gozala
Copy link

Gozala commented Jul 12, 2012

@gordonbrander
Copy link
Author

I like the use of the term "lambda" in your example to distinguish between methods and traditional functions. Maybe toLambda and toMethod are better names?

Update: changed the name to Lambda.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment