Last active
March 8, 2019 21:23
-
-
Save wlib/1168801b68e291dbd2ef0814efe7e239 to your computer and use it in GitHub Desktop.
Revisions
-
wlib revised this gist
Mar 7, 2019 . 1 changed file with 19 additions and 17 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,35 +1,37 @@ const Task = run => ({ // (a -> e) -> (a -> r) -> Task(a) // Might be inaccurate idk run, // undefined -> undefined log: () => run(console.error) (console.log), // (a -> b) -> Task(b) map: f => Task(err => res => run(err) (x => res(f(x)) )), // (a -> Task(b)) -> Task(b) runMap: f => Task(err => res => run(err) (x => f(x).run(err) (res) )) }) /* Sample Usage: const http = (uri, method = "GET") => Task(err => res => { const xhr = new XMLHttpRequest() xhr.open(method, uri, true) xhr.onload = () => (xhr.status < 300 && xhr.status >= 200) ? res(xhr.response) : err(xhr.status + " " + xhr.statusText) xhr.onerror = err xhr.send() }) @@ -38,8 +40,8 @@ app = http("https://randomuser.me/api") .map(x => x.results) // Run it when you want app.run(err => console.error(err)) (res => console.log(res)) // Shorthand for convenience app.log() -
wlib created this gist
Feb 6, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ const Task = run => ({ // Task(a) ~> (e -> x) -> (a -> b) -> b run, // Task(a) ~> () -> Void log: () => run(console.error, console.log), // Task(a) ~> (a -> b) -> Task(b) map: f => Task((reject, resolve) => run(reject, x => resolve(f(x)))), // Task(a) ~> (a -> Task(b)) -> Task(b) runMap: f => Task((reject, resolve) => run(reject, x => f(x).run(reject, resolve))) }) /* Sample Usage: const http = (uri, method = "GET") => Task((reject, resolve) => { const xhr = new XMLHttpRequest() xhr.open(method, uri, true) xhr.onload = () => (xhr.status < 300 && xhr.status >= 200) ? resolve(xhr.response) : reject(xhr.status + " " + xhr.statusText) xhr.onerror = reject xhr.send() }) app = http("https://randomuser.me/api") .map(JSON.parse) .map(x => x.results) // Run it when you want app.run(e => console.error(e), x => console.log(x)) // Shorthand for convenience app.log() */