Fork me on GitHub
#beginners
<
2021-04-28
>
walterl00:04:57

Is there something in core like cond-> that threads the expression value through the test(s) too? I.e. I want to do something like this:

(cond->-> (big-operation-maybe-returns-future!)
  future? deref)

walterl00:04:58

Best alternative I could come up with is:

(let [result (big-operation-maybe-results-future!)
      result (if (future? result) @result result)]
  ;; ...
  )

walterl00:04:01

could also use cond-> there, but that seems a bit forced

seancorfield02:04:29

No, but we released a small library at work that has condp-> that does this…

seancorfield02:04:46

It has condq (a unary version of condp) and condp-> / condp->> to match cond-> / cond->> but thread the expression through the predicate (plus other stuff).

Eric Ihli02:04:16

What's the idiomatic way to do the following. I have a huge list of text files I need to process efficiently. Can't keep everything in memory. So I'm going with transducers.

(transduce
  (comp (remove #(.isDirectory %))
        (map slurp)
        (map #(string/split % #"[\n+\?\.]"))
        (map (partial map #(string/split % #" +")))
        (map (partial map (partial map syllabify-word)))
        (map (partial map (partial map (partial map process-syllable)))))
  conj
  (file-seq (io/file "data-directory")))
What's going on is each step of the transduction creates a new level of a nested sequence. I start with a file, split that on newlines, then split each line of each file on spaces, then split each word of each line of each file into syllables... etc... The nesting gets confusing to reason about, hard to code complicated transformations for. If I could have everything in memory at one time, I'd just have a flatten step somewhere in there. Is there some simplifying alternative that I'm not seeing? Edit: I may have just seen an obvious answer. I can combine most of those into a process-file function which can flatten and do whatever the heck it wants since it will have at most 1 file in memory at a time. Something like:
(defn process-file
  [text]
  (->> text
       (#(string/split % #"[\n+\?\.]"))
       (mapcat #(string/split % #" +"))
       (mapcat syllabify-word)
       (map process-syllable)))

(transduce
  (comp (remove #(.isDirectory %))
        (map slurp)
        (map process-file))
  conj
  (file-seq (io/file "data-dir")))

1
👍 1
tom03:04:17

Trying to use luminus web framework. After checking the password against the password that's been hashed in the db with buddy.....how exactly do you tell buddy that the user is now logged in and authenticated?

simongray06:04:11

I don't use Luminus or buddy, but usually you do this by setting a cookie with a session id.