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
//TODO Write this in Imperative v/s Functional style | |
const getUrlForUser = (user) => { | |
//todo | |
} | |
//User object | |
let joeUser = { | |
name: 'joe', | |
email: '[email protected]', | |
prefs: { | |
languages: { |
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
//Showing relevant parts from the Maybe implementation from ramda-fantasy lib | |
//See https://github.com/ramda/ramda-fantasy/blob/master/src/Maybe.js for full source | |
function Maybe(x) { //<-- main constructor that returns Maybe of Just or Nothing | |
return x == null ? _nothing : Maybe.Just(x); | |
} | |
function Just(x) { | |
this.value = x; | |
} |
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
//Monad - a sample implementation | |
class Monad { | |
constructor(val) { | |
this.__value = val; | |
} | |
static of(val) {//Monad.of is simpler than "new Monad(val) | |
return new Monad(val); | |
}; | |
map(f) {//Applies the function but returns another Monad! | |
return Monad.of(f(this.__value)); |
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
const add1 = (a) => a + 1; | |
class MyFunctor { //Custom "Functor" | |
constructor(value) { | |
this.val = value; | |
} | |
map(fn) { //Applies function to this.val + returns new Myfunctor | |
return new Myfunctor(fn(this.val)); | |
} | |
} | |
//temp is a Functor instance that's storing value 1 |
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
//Imperative: | |
//Too many if-else and null checks; relying on global indexURLs; decided that "en" urls are default for any country | |
const getUrlForUser = (user) => { | |
if (user == null) { //not logged in | |
return indexURLs['en']; //return default page | |
} | |
if (user.prefs.languages.primary && user.prefs.languages.primary != 'undefined') { | |
if (indexURLs[user.prefs.languages.primary]) {//if translation exists, | |
return indexURLs[user.prefs.languages.primary]; | |
} else { |
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
//Step 1. Instead of.. | |
if (user == null) { //not logged in | |
return indexURLs['en']; //return default page | |
} | |
//Use: | |
Maybe(user) //Returns Maybe({userObj}) or Maybe(null). i.e. data wrapped INSIDE "Maybe" | |
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
//The below gist shows how to take care of global variables and also make funcs chainable | |
//Global indexURLs map for different languages | |
let indexURLs = { | |
'en': 'http://mysite.com/en', //English | |
'sp': 'http://mysite.com/sp', //Spanish | |
'jp': 'http://mysite.com/jp' //Japanese | |
} | |
//Imperative |
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
class Maybe { | |
constructor(val) { | |
this.val = val; | |
} | |
... | |
... | |
//"ap" takes another "maybe" and applies the function it's holding in itself. | |
//this.val MUST be a function or Nothing (and can't be some string or int) | |
ap(differentMayBe) { |
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
//imperative | |
//Returns error or price including tax | |
const tax = (tax, price) => { | |
if (!_.isNumber(price)) return new Error("Price must be numeric"); | |
return price + (tax * price); | |
}; | |
//Returns error or price indluding discount | |
const discount = (dis, price) => { |
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
var Either = require('ramda-fantasy').Either; | |
var Left = Either.Left; | |
var Right = Either.Right; | |
const tax = R.curry((tax, price) => { | |
if (!_.isNumber(price)) return Left(new Error("Price must be numeric")); //<--Wrap Error in Either.Left | |
return Right(price + (tax * price)); //<--Wrap result in Either.Right | |
}); |