Skip to content

Instantly share code, notes, and snippets.

@wlib
Last active March 8, 2019 21:23
Show Gist options
  • Save wlib/1168801b68e291dbd2ef0814efe7e239 to your computer and use it in GitHub Desktop.
Save wlib/1168801b68e291dbd2ef0814efe7e239 to your computer and use it in GitHub Desktop.

Revisions

  1. wlib revised this gist Mar 7, 2019. 1 changed file with 19 additions and 17 deletions.
    36 changes: 19 additions & 17 deletions task.js
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,37 @@
    const Task = run => ({
    // Task(a) ~> (e -> x) -> (a -> b) -> b
    // (a -> e) -> (a -> r) -> Task(a) // Might be inaccurate idk
    run,

    // Task(a) ~> () -> Void
    // undefined -> undefined
    log: () =>
    run(console.error, console.log),
    run(console.error)
    (console.log),

    // Task(a) ~> (a -> b) -> Task(b)
    // (a -> b) -> Task(b)
    map: f =>
    Task((reject, resolve) =>
    run(reject,
    x => resolve(f(x)))),
    Task(err => res =>
    run(err)
    (x => res(f(x)) )),

    // Task(a) ~> (a -> Task(b)) -> Task(b)
    // (a -> Task(b)) -> Task(b)
    runMap: f =>
    Task((reject, resolve) =>
    run(reject,
    x => f(x).run(reject, resolve)))
    Task(err => res =>
    run(err)
    (x => f(x).run(err)
    (res) ))
    })

    /* Sample Usage:
    const http = (uri, method = "GET") =>
    Task((reject, resolve) => {
    Task(err => res => {
    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
    ? 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(e => console.error(e),
    x => console.log(x))
    app.run(err => console.error(err))
    (res => console.log(res))
    // Shorthand for convenience
    app.log()
  2. wlib created this gist Feb 6, 2019.
    46 changes: 46 additions & 0 deletions task.js
    Original 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()
    */