Last active
June 8, 2016 19:49
-
-
Save TrevorBasinger/1473fafe157859070df470bd9a2b9a12 to your computer and use it in GitHub Desktop.
This file contains 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 | |
R = require ('ramda'), | |
request = require ('request'), | |
logI = function (x) { console.log (x); return x; }, | |
id = function (x) { return x; }, | |
konst = R.curry (function (a, b) { return a; }), | |
bind = function (a) { return R.chain (konst (a)); }, | |
chain = R.chain, | |
map = R.map, | |
ap = R.curry (function (ma, mf) { return mf.ap (ma); }), | |
random = R.curry (function (min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }), | |
runTaskIO = R.curry (function (rej, res, t) { return IO (function () { t.fork (rej, res); }); }), | |
du = function (M) { | |
return function () { | |
return R.apply (R.pipe, arguments)(M.of ({})); | |
}; | |
}, | |
addOne = function (a) { return a + 1; }, | |
//maybeAddTwoIfEven :: Int -> Maybe Int | |
maybeAddTwoIfEven = function (a) { | |
if (a % 2 === 0) { return new Just (a + 2); } | |
return new Nothing (); | |
}, | |
//maybe | |
//useThisWIthTask = R.curry (function (addToCache, incomingVal) { | |
// return new Task (function (reject, resolve) { | |
// http.get (incomingValue..., funtion (er, val) { | |
// x.val = val; | |
// x('key', val) | |
// }); | |
// }); | |
//}) | |
//TaskOne.chain (useThisWithTask (cacheFun)) | |
nil = null; | |
function Maybe () {} | |
function Nothing () {} | |
function Just (a) { this.value = a; } | |
function Task (computation) { this.fork = computation; } | |
Task.prototype.map = function (f) { | |
const fork = this.fork; | |
return new Task (function (reject, resolve) { | |
fork ( | |
function (err) { | |
reject (err); | |
}, | |
function (data) { | |
resolve (f (data)); | |
} | |
); | |
}); | |
} | |
Task.prototype.chain = function (f) { | |
const fork = this.fork; | |
return new Task (function (reject, resolve) { | |
fork ( | |
function (err) { | |
reject (err); | |
}, | |
function (data) { | |
f (data).fork (reject, resolve); | |
} | |
); | |
}); | |
} | |
// map :: (a -> b) -> F a -> F b | |
// chain :: (a -> F b) -> F a -> F b | |
maybe = R.curry (function (d, M) { | |
if (M.value === undefined) { return d; } | |
return M.value; | |
}); | |
Just.prototype = Object.create (Maybe.prototype); | |
Just.prototype.map = function (f) { | |
return new Just (f (this.value)); | |
}; | |
Just.prototype.chain = function (f) { | |
return f (this.value); | |
}; | |
Nothing.prototype = Object.create (Maybe.prototype); | |
Nothing.prototype.map = function (x) { return new Nothing(); }; | |
Nothing.prototype.chain = function (f) { return Nothing (); }; | |
(function io () { | |
const maybeNum = new Just (2) | |
.map (addOne) // Maybe Int | |
.chain (maybeAddTwoIfEven) // Maybe Int | |
.map (addOne); | |
//const runMe = du (Maybe) ( m1, m2, m3); | |
// t1 :: Task String | |
const t1 = new Task (function (reject, resolve) { | |
request.get ('http://google.com', function (err, res, body) { | |
if (err) { return reject (err); } | |
resolve (body); | |
}); | |
}); | |
t1 | |
.map (R.length) | |
.fork (errorCB, successCB); | |
//console.log (maybe (-9999, maybeNum)) | |
function errorCB (err) { logI ('ERROR: ' + err); }; | |
function successCB (x) { logI (x); } | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment