nyc

http://clojure.nyc
2019-09-11T14:40:04.001300Z

does anybody else feel great regret when they need to resort to as-> ?

2019-09-11T14:41:39.002300Z

it's bitten me when i want to mix map/reduce/filter with other functions that don't follow that same format

2019-09-11T14:42:13.002900Z

maybe I'm missing something, even. i'll paste it next time i run into it

2019-09-11T14:55:42.005700Z

I think ideally it's used when you have a single operation in a thread-first chain of operations that has a different/weird signature

(-> {}
    (assoc :x 3)
    repeat
    (as-> x (take 5 x))
    (conj :hello))
This is a pretty horrible example, and ->> would work better here anyway. The clojure core functions tend to be designed to avoid these situtions. But I do hit this in the wild sometimes.

2019-09-11T14:58:12.007400Z

This is an actual real world example I used it in lately where I needed to pass the same request data around internally ina lot of places. A let might be better here too but I think this is a place where as-> is ok

(as-> req-vec x
    (ibg/add-id conn x)
    (ibg/add-default-args x)
    (ibg/req-tap conn x (a/chan 1 (comp (ibg/sync-xf x) (ibg/payload-xf x))))
    (a/into [] x)
    (a/<!! x))

2019-09-11T15:00:05.008100Z

ah yeah last last second last last. it'd be nicer if there were a way to override this in the nested form itself somehow

2019-09-11T15:00:40.008800Z

that's probably a world of pain to do, though

2019-09-11T15:02:11.011100Z

i often find myself wanting to make a #() that gets threaded into a ->. i wonder if that's legit

avi 2019-09-11T15:02:49.011500Z

I recently replaced an awkward usage of as-> with cond->> Before:

(as-> main it
            (parse-string it)
            (if format (with-msg "formatting" (reformat it)) it)
            (if snap (with-msg "snapping" (snap-to-grid it to-closest min-margin)) it)
            (stringify it)
            (assemble front it)
            (spit file-path it)))))
After:
(-> (parse-string main)
              (cond->>
               format (with-msg "formatting" reformat)
               snap   (with-msg "snapping" #(snap-to-grid % to-closest min-margin)))
              (->> (stringify)
                   (assemble front)
                   (spit file-path))))))
cond->> and cond-> were new to me…

πŸ‘ 1
2019-09-11T15:03:03.011600Z

2019-09-11T15:04:56.012300Z

oh i see. needing "it" in both branches of the if

2019-09-11T15:04:59.012500Z

yeah that's a bummer

2019-09-11T15:06:26.013Z

i don't think i've used cond->>, myself

2019-09-11T15:09:23.013900Z

You can use #() if you just wrap it in parens

(-> 123
    (#(inc %))
    ((fn [x] (str x))))
not pretty but i use this in the repl sometimes

2019-09-11T15:10:44.014300Z

Oh I see you meant starting with the function

2019-09-11T15:11:15.014700Z

yeah it feels like some hack just to keep the consistency of the ->

2019-09-11T15:11:30.015100Z

alas, it's not stopped me from using it! πŸ™‚

2019-09-11T15:12:09.015700Z

There was some sort of related discussion on clojureverse recently on some alternative threading macros https://clojureverse.org/t/looking-for-opinion-about-some-macro-ive-been-working-on/4782/19

πŸ‘ 1
2019-09-11T15:13:11.016200Z

oh wow

2019-09-11T15:17:45.016400Z

https://github.com/rplevy/swiss-arrows

2019-09-11T15:18:24.017500Z

so diamond wand looks like what i've been missing. where if you don't include a <> in the nested forms it defaults to the behavior of the non-diamond version

πŸ‘ 1
2019-09-11T16:45:37.020200Z

(as-> coll $
      (map transform-data $)
      (filter filter-data $)
      (doseq [item $]
       (println item)))
That's where I think the as-> macro shines. When you're using it with something like doseq where none of the other thread macros would work.

πŸ‘ 1
2019-09-11T17:12:31.021100Z

the diamond wand doesn't seem to work here:

(-<>> data
  (map identity)  
  (filter (constantly 1))
  (doseq [item <>]
    (println item)))

2019-09-11T17:15:32.021800Z

as-> must do nested replacements where -<>> doesn't, i guess