Last active
December 18, 2015 10:39
-
-
Save jeremyheiler/5770079 to your computer and use it in GitHub Desktop.
This file contains 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 characters
(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})))) |
Author
jeremyheiler
commented
Jun 15, 2013
Potential return options:
(lookup-user "@jeremyheiler" {:return :data}) ;; default
(lookup-user "@jeremyheiler" {:return :envelope})
(lookup-user "@jeremyheiler" {:return :response})
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.
(->> (: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