Skip to content

Instantly share code, notes, and snippets.

@dtipson
Last active November 16, 2016 21:56
Show Gist options
  • Save dtipson/dddb739984a125f380d98bcf14842a9a to your computer and use it in GitHub Desktop.
Save dtipson/dddb739984a125f380d98bcf14842a9a to your computer and use it in GitHub Desktop.
Chainable, pure DOM manipulation operations married to a powerful DOM selector engine, just 202 bytes gzipped!
const IO = fn =>({
runIO: fn,//run the effect
map: f => IO( a => f(fn(a)) ),//transform the inner value
chain: f => IO( _ => f(fn()).runIO() ),//transform the inner value into another pure operation
fork: _ => IO(x => new Promise(r => window.setTimeout(_ => r(fn(x)), 0) ))//fork the process!
});
IO.of = x => IO(_ => x);
IO.$ = selectors => IO(_=>Array.from(document.querySelectorAll(selectors)));
//Examples:
//const ruinBody = IO(_=>document.baseURI).map(x=>x.replace(/\//g,' :P ')).chain(x=>IO(_=>document.body.innerHTML=x));
//const ruinBody2 = IO.of('hi there').chain(x=>IO.$('body').map(xs=>xs[0]).map(b=>b.innerHTML=x));
//const lazyURI = IO(_=>document.baseURI).fork().map(p=>p.then(str=>str.toUpperCase()));
//ruinBody.runIO();
//ruinBody2.runIO();
//lazyURI.runIO().then(x=>console.log(x));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment