Skip to content

Instantly share code, notes, and snippets.

View rajaraodv's full-sized avatar

Raja Rao DV rajaraodv

View GitHub Profile
//TODO Write this in Imperative v/s Functional style
const getUrlForUser = (user) => {
//todo
}
//User object
let joeUser = {
name: 'joe',
email: '[email protected]',
prefs: {
languages: {
@rajaraodv
rajaraodv / Maybe-ramda-fantasy.js
Last active November 14, 2016 11:28
Maybe Monad sample implementation
//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;
}
@rajaraodv
rajaraodv / Monad-sample.js
Created November 9, 2016 00:28
A sample implementation of Monad
//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));
@rajaraodv
rajaraodv / MyFunctor.js
Last active February 8, 2019 13:45
A sample custom functor
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
//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 {
//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"
@rajaraodv
rajaraodv / currying-example.js
Last active November 18, 2016 23:16
The below gist shows how to take care of global variables and also make funcs chainable
//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
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) {
//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) => {
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
});