Skip to content

Instantly share code, notes, and snippets.

@christiantakle
Last active December 21, 2015 20:19
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 christiantakle/6360456 to your computer and use it in GitHub Desktop.
Save christiantakle/6360456 to your computer and use it in GitHub Desktop.
Simple Functional Example in JavaScript Using: `curry` and `compose`
//-- Example 1 --------------------------------------------------------
//-- Note - We do not need to reference the data `email` when
//-- using a 'Point free style' of programming
//-- Note - `curry` is from Prelude. `compose` is from Oliver Steele --
//-- http://preludels.com/
//-- http://osteele.com/sources/javascript/functional/
//-- Imparative -------------------------------------------------------
var getUserName = function(email) {
return email.split('@')[0];
};
//-- Vanilla functionel -----------------------------------------------
//+ split :: Char → String → [String]
var split = function(char) {
return function(string) {
return string.split(char);
};
};
//-- Curry ------------------------------------------------------------
//+ split :: Char → String → [String]
var split = curry(function(char, string) {
return string.split(char)
});
//+ first :: [a] → a
var first = function(array) {
return array[0];
};
//-- Point Free -------------------------------------------------------
var getUserName = compose(first, split('@'));
// getUserName('takle@functions.com') //=> 'takle'
//-- Prelude Lib, Standard Haskell Lib port ---------------------------
var getUserName = compose(Prelude.first, Prelude.split('@'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment