Created
April 7, 2016 08:49
-
-
Save luislee818/5d5f9202ede385efbdbefe890c1cb775 to your computer and use it in GitHub Desktop.
Mostly adequate guide experiments
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 R = require('ramda'); | |
var log = console.log; | |
var Maybe = function(val) { | |
this.__val = val; | |
} | |
Maybe.of = (val) => new Maybe(val); | |
Maybe.prototype.map = function(fn) { | |
if (this.__val === undefined) { | |
return this; | |
} else { | |
return Maybe.of(fn(this.__val)); | |
} | |
} | |
var IO = function(fn) { | |
this.__val = fn; | |
}; | |
IO.of = (val) => new IO(val); | |
IO.prototype.map = function (fn) { | |
return IO.of(R.compose(fn, this.__val)); | |
}; | |
var map = R.curry((fn, functor) => functor.map(fn)) | |
var dateIO = IO.of(() => Date.now()); | |
var date = dateIO.map((d) => new Date(d)).map((d) => d.toString()).map(R.take(3)) | |
log(date.__val()); | |
var url = IO.of(() => "http://foo.com?bar=123&baz=999"); | |
// toPairs :: String -> [[String]] | |
var toPairs = R.compose(R.map(R.split('=')), R.split('&')); | |
// params :: String -> [[String]] | |
var params = R.compose(toPairs, R.last, R.split('?')); | |
// getQueryVal :: Maybe [[String]] -> Maybe String | |
var getQueryVal = map(R.compose(R.last, R.head)); | |
// findParam :: String -> IO Maybe [String] | |
var findParam = function(key) { | |
return map(R.compose(getQueryVal, Maybe.of, R.filter(R.compose(R.equals(key), R.head)), params), url); | |
} | |
log(findParam('baz').__val()); // 999 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment