aleph

2017-06-02T17:34:51.597941Z

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))
         ))

2017-06-02T17:35:19.607206Z

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))
))

2017-06-02T17:40:17.706750Z

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))))

2017-06-02T19:11:57.548541Z

nvmind, found a bug

2017-06-02T19:12:12.552868Z

was missing an argument to d/recur :troll: