Last active
September 6, 2018 21:43
-
-
Save statianzo/efd733782dbf57b58c68cacab354159f to your computer and use it in GitHub Desktop.
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 Maybe = { | |
of (val) { | |
return val === null || val === undefined ? new Nothing : new Just(val) | |
}, | |
catMaybes(xs) { | |
return xs | |
.filter(x => x instanceof Just) | |
.map(x => x.unwrap()) | |
} | |
} | |
class Nothing { | |
map(fn) { | |
return new Nothing; | |
} | |
unwrap() { | |
throw Error('attempted to unwrap Nothing') | |
} | |
} | |
class Just { | |
constructor(val) { | |
this.val = val; | |
} | |
map(fn) { | |
return Maybe.of(fn(this.val)) | |
} | |
unwrap() { | |
return this.val; | |
} | |
} | |
const query = table => where => order => limit => | |
Maybe.catMaybes([ | |
Maybe.of(`select * from ${table}`), | |
Maybe.of(where).map(w => ` where ${w}`), | |
Maybe.of(order).map(o => ` order by ${o}`), | |
Maybe.of(limit).map(l => ` limit ${l}`) | |
]).join('') | |
const albums = query(`albums`); | |
const queenAlbums = albums(`artist = 'Queen'`); | |
console.log(queenAlbums(`title`)(1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment