Fork me on GitHub
#beginners
<
2016-04-15
>
bojan.matic08:04:48

I’m going through the “Clojure for the brave and true book”, and now I’m on the core-async chapter

bojan.matic08:04:01

at the end it gives an example of using channels to create a process pipeline

bojan.matic08:04:14

(def in-chan (chan))
(def upper-caser-out (upper-caser in-chan))
(def reverser-out (reverser upper-caser-out))
(printer reverser-out)

bojan.matic08:04:43

this is nice but my question is, is there a macro or some way to make this more readable?

bojan.matic08:04:18

it just feels to me that this can look more concise

lgastako08:04:43

if you don’t need the intermediate pieces:

lgastako08:04:44

(-> (chan)
    upper-caser
    reverser
    printer)

bojan.matic08:04:46

sort of like the -> macro?

lgastako08:04:54

oh would that not work?

lgastako08:04:10

are upper-caser, reverser and printer functions?

bojan.matic08:04:26

yes, they take an in channel

lgastako08:04:34

then I think that should work

bojan.matic08:04:35

(defn upper-caser
  [in]
  (let [out (chan)]
    (go (while true (>! out (clojure.string/upper-case (<! in)))))
    out))

(defn reverser
  [in]
  (let [out (chan)]
    (go (while true (>! out (clojure.string/reverse (<! in)))))
    out))

(defn printer
  [in]
  (go (while true (println (<! in)))))

lgastako08:04:27

since presumably you need to be able to put stuff into the channel:

lgastako08:04:29

(let [c (chan)]
  (-> c
      upper-caser
      reverser
      printer)
  ;; put stuff into c here
  )

lgastako08:04:26

Does anyone know if there’s a way to do what I’m trying to do here? https://www.refheap.com/117586

lgastako08:04:54

Essentially do different work in the macro before actually emitting the output depending on whether it’s generating clojure code or clojurescript code?

lgastako08:04:46

(assuming a suitable namespaced-alist->map function)

lgastako08:04:02

I just need to replace the *clojure-version* test

bojan.matic08:04:52

@lgastako: thanks for your help!

bojan.matic08:04:58

is your problem the fact that clojure-version is not bound on the client?

lgastako08:04:20

well, it’s more that even when generating clojurescript code, the macro itself is executing in a clojure environment (the compiler) and so clojure-version is always bound there… if I reference inside the emitted code then it’s bound/not bound accordingly and works great, but in this case I need to be able to filter out some bits incoming args depending on which type of code is being emitted

lgastako08:04:49

thanks, but unfortunately not in this case

lgastako09:04:52

I was able to get it working using this if-cljs macro that everyone else seems to use: http://blog.nberger.com.ar/blog/2015/09/18/more-portable-complex-macro-musing