Fork me on GitHub
#aleph
<
2017-06-02
>
mping17:06:51

Hia, I’m having trouble converting the following code to d/loop:

(loop [messages (fetch-messages auth-token query nil)
            result []]
       (let [next-token (:nextPageToken messages)]
         (if (nil? next-token)
           (d/future (concat result (:messages @messages)))
           (recur (fetch-messages auth-token query next-token)
                  result))
         ))

mping17:06:19

I must have misunderstood something, this is my attempt:

(d/loop [messages (fetch-messages auth-token query nil)
              result   []]
       (-> messages
           (d/chain 
            (fn [msgs]
              (let [next-token (:nextPageToken msgs)]
                (println "NEXT:" next-token)
                (if (nil? next-token)
                  (concat result (:messages msgs))
                  (d/recur (fetch-messages auth-token query next-token))
                  )
                )))
           (d/catch 
               (fn [ex]
                 (println "ERROR" ex)
                 ex))
))

gsnewmark17:06:17

mping: it looks like you're d/recur is missing one argument, it should probably be something like this (didn't check with compiler):

(d/loop [token nil
         result []]
  (-> (fetch-messages auth-token query token)
      (d/chain 
       (fn [msgs]
         (let [next-token (:nextPageToken msgs)]
           (println "NEXT:" next-token)
           (if (nil? next-token)
             (concat result (:messages msgs))
             (d/recur next-token (concat result (:messages msgs)))))))
      (d/catch 
          (fn [ex]
            (println "ERROR" ex)
            ex))))

mping19:06:57

nvmind, found a bug

mping19:06:12

was missing an argument to d/recur troll