Instantly share code, notes, and snippets.

Embed
What would you like to do?
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