Last active
February 6, 2016 22:43
-
-
Save kwijibo/84c3b39d70eb79a6b610 to your computer and use it in GitHub Desktop.
Future that can have a value applied to its map function
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
// submit :: event -> IO[ HttpGet[ Either [ HttpPost []]]] | |
const submit = (event) => { | |
return getFormFields() | |
.map(incrementVersionNumber) | |
.map(saveRecord) | |
} | |
// getFormFields :: () -> IO | |
const getFormFields = () => IO(()=>{ | |
return lift() | |
}) | |
const split = SingleChar( | |
c => AnyString( | |
s => s.split(c) | |
) | |
) |
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 f = Future((reject,resolve)=> setTimeout(m=>resolve("hello"),1000)) | |
.map(x=>x+' world') | |
.map(s=>s.toUpperCase()) | |
f.fork(console.error, console.log) |
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 Future = function(){} | |
Future.of = (forkr, mapr=x=>x) => { | |
return Object.assign(new Future(), { | |
map: (f)=> Future(forkr, x=>f(mapr(x))), | |
fork: (err,success)=> { | |
forkr(err,x=>success(mapr(x))) | |
}, | |
apply: x => mapr(x) | |
}) | |
} | |
Future.subType = function(makeForkr,argNames){ | |
function ctor(...args){ | |
const objProps = argNames.reduce((obj,k,i)=>{obj[k]= args[i]; return obj},{}) | |
return Object.assign(Object.create(ctor.prototype), objProps, Future.of(makeForkr(...args))) | |
} | |
ctor.prototype = Future.prototype | |
return ctor | |
} |
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 HttpGet = function(){} | |
HttpGet.of = (url,headers={accept:'application/json'}) => { | |
return Object.assign( | |
new HttpGet() | |
, {url,headers} | |
, Future((rej,res)=>{ request({url,headers},(err,succ)=>err? rej(err) : res(succ) )}) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment