Skip to content

Instantly share code, notes, and snippets.

@statianzo
Last active September 6, 2018 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statianzo/efd733782dbf57b58c68cacab354159f to your computer and use it in GitHub Desktop.
Save statianzo/efd733782dbf57b58c68cacab354159f to your computer and use it in GitHub Desktop.
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