Clojurians
#lumo
<
2017-06-11
>

This page is not created by, affiliated with, or supported by Slack Technologies, Inc.

mattford01:06:17

With these two macros

mattford01:06:36

(ns guller.macros)

(defmacro then->>
  [x & forms]
  (loop [x x, forms forms]
    (if forms
      (let [form (first forms)
            threaded (if (seq? form)
                       `(.then ~x #(~(first form) [email protected](next form) %))
                       `(.then ~x #(~form %)))]
        (recur threaded (next forms)))
      x)))

(defmacro then->
  [x & forms]
  (loop [x x, forms forms]
    (if forms
      (let [form (first forms)
            threaded (if (seq? form)
                       `(.then ~x #(~(first form) % [email protected](next form)))
                       `(.then ~x #(~form %)))]
        (recur threaded (next forms)))
      x)))

mattford01:06:48

I was able to write the code like this

mattford01:06:08

(ns guller.core
  (:require-macros [guller.macros :refer [then-> then->>]]))

(require '[cljs.pprint :as pp :refer [pprint]])

(def github (js/require "github"))
(def githubapi (github.))

(defn flip
  "given a function, create a flipped 2-argument function"
  [f]
  (fn [a b] (f b a)))

(defn getOrgs
  "Get the organisations for an authenticated user"
  []
  (then-> (.. githubapi -users (getOrgs {}))
          (js->clj :keywordize-keys true)
          :data
          ((flip map) :login)))

(defn getReposForOrg
  "Get all the repos for an Org"
  [org]
  (then-> (.. githubapi -repos (getForOrg #js {:org org}))
          (js->clj :keywordize-keys true)
          :data
          ((flip map) :ssh_url)
          pprint))

(defn -main []
  (.. githubapi (authenticate #js {:type "netrc"}))
  (then->> (getOrgs)
           (mapv getReposForOrg)))

mattford01:06:21

Seems to do a good job of hiding call-backs and promises.

mattford01:06:03

The threading looks familiar.

mattford01:06:46

Not sure if I prefer the reduce (see history) version or not as gives more control over the position of the response.

mattford09:06:31

Actually it’s not that good; it works over synchronous call backs i.e., doesn’t actually chain promises. Hmmmmm.

pesterhazy13:06:48

Interesting stuff