Last active
December 21, 2015 20:19
-
-
Save christiantakle/6360456 to your computer and use it in GitHub Desktop.
Simple Functional Example in JavaScript
Using: `curry` and `compose`
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
//-- 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('[email protected]') //=> '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