Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active December 26, 2015 23:09
Show Gist options
  • Select an option

  • Save wilmoore/7228490 to your computer and use it in GitHub Desktop.

Select an option

Save wilmoore/7228490 to your computer and use it in GitHub Desktop.
Strategy vs Pattern Matching (compare to: http://www.dofactory.com/javascript-strategy-pattern.aspx)
-module(greeter).
-export([say_hi/1]).
say_hi({bored}) -> "Sup...";
say_hi({polite}) -> "Welcome sir.";
say_hi({friendly}) -> "Hey...".
% erl
% c(greeter).
% greeter:say_hi({polite}).
%=> "Welcome sir."
exports.bored = function () {
this.sayHi = function () { return "Sup..." };
};
exports.polite = function () {
this.sayHi = function () { return "Welcome sir." };
};
exports.friendly = function () {
this.sayHi = function () { return "Hey..." };
};
module.exports = function (type) {
return new exports[type];
};
// node
// var greeter = require('./greeter')('polite');
// greeter.sayHi();
//=> 'Welcome sir.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment