Skip to content

Instantly share code, notes, and snippets.

@jeremyheiler
Last active December 18, 2015 10:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyheiler/5770079 to your computer and use it in GitHub Desktop.
Save jeremyheiler/5770079 to your computer and use it in GitHub Desktop.
(require '[paprika.core :as adn])
;; This returns a sequence your of vectors, one for each of the people you follow.
;; Each vector contains the user's username and the date of their last post.
;; The sequence is in ascending order by the date of their last post
;; Careful, though. The lookup is N+1.
(sort-by first
(map (juxt :created-at #(get-in % [:user :username]))
(apply concat
(map #(adn/lookup-user-posts % {:count 1 :access-token t}))
(adn/lookup-following-ids "me" {:access-token t}))))
@jeremyheiler
Copy link
Author

(sort-by first
  (map (juxt :created-at #(get-in % [:user :username]))
    (apply concat
      (map #(:data (adn/lookup-user-posts % {:count 1 :access-token t})))
        (:data (adn/lookup-following-ids "me" {:access-token t})))))

@jeremyheiler
Copy link
Author

Potential return options:

(lookup-user "@jeremyheiler" {:return :data}) ;; default
(lookup-user "@jeremyheiler" {:return :envelope})
(lookup-user "@jeremyheiler" {:return :response})

@jeremy-w
Copy link

Using a combined monadic bind + thread-last operator:

(m/->> adn/Either
  (adn/lookup-following-ids "me" {:access-token t})
  (map #(adn/lookup-user-posts % {:count 1 :access-token t}))
  (apply concat)
  (map (juxt :created-at #(get-in % [:user :username]))
  (sort-by first))

(Using core's thread-last ->> macro would flatten out the nesting in your examples a lot.)

I based this off distant memories of the monad stuff in Marick's fp-oo book. The adn/Either tells the monad library which monad function implementations to use. adn/Either would be a Paprika-specific Either that knows to treat :data as Right and :error as Left. Its bind either extracts Right and feeds that to the function, or skips invoking the function and returns Left directly.

(re: https://alpha.app.net/jeremyheiler/post/6815186)

@jeremyheiler
Copy link
Author

(->> (:data (p/lookup-following-ids "me" {:token t})) (map #(:data (p/lookup-user-posts % {:count 1 :token t}))) (apply concat) (map (juxt :created-at #(get-in % [:user :username]))) (sort-by first))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment