Fork me on GitHub

Just wanted to let you folks in on my happiness: I successfully created an executable from my minimalistic deps.edn based cli tool via graalvm


really satisfying. process took 15mins to learn, which is a nice selling point for clojure based apps at work

ūüéČ 45

I'm using the (time exp) macro to find execution times, and I'd like to capture what it returns to standard output to a string. How do that do that?

Darin Douglass15:07:39

user> (let [out (with-out-str (time (+ 1 1)))]
        (println "HERE" out)) 
HERE "Elapsed time: 0.010066 msecs"


ūüĎć 3

Thanks! The name of that funtion with-out-str is great!

Grant Isom16:07:38

Hey all, I am working on a new re-frame app and am at the point I want to start writing some tests, I am having a hard time determining what libraries to go with. Any suggestions? I am using shadow, lein, pushy, and bidi mainly


I tried the following (def timeit (comp with-out-str time)) and am getting the following error: Can't take value of a macro: #'clojure.core/time Does this mean that comp cannot take macros as arguments? If not, what's the alternative?

Alex Miller (Clojure team)16:07:27

you can't use macros as higher order functions (because they're not functions)

Alex Miller (Clojure team)16:07:40

or rather they are, but eval'ed at compile time, not run time

Alex Miller (Clojure team)16:07:57

one technique is to wrap in a function

Alex Miller (Clojure team)16:07:14

but here, you probably want timeit to be a macro too

Alex Miller (Clojure team)16:07:21

for the same reasons time is

ūüĎć 3

Understood, I'll give that a shot. Thanks!


I'm looking for what must be a pretty basic core.async function. I'd like to do this:

(let [input-ch (load-data-into-chan> my-data)
      valid-ch (SOME-FUNC validation-func input-ch)]
     (go-loop [val (<! valid-ch)]
         (;; do stuff to val...)))


why is core.async being used here?


Basically return a new channel with a function (...a transducer?) applied to each value taken from the another channel. I'm looking for it to be lazy. It kind of seems like async/map is what I'm looking for, but it requires putting a single channel (like input-ch) into a list, which I haven't seen a lot of examples do. pipeline also does what I'm looking for, but it seems kind of verbose (and creates a whole to channel to <! for) for what I'm trying to do.


It's a NodeJS program, and both the loading the data and validating the data are async operations. I could use callbacks because it's such a small program, but I'm trying to take the opportunity to learn about core.async.


so SOME-FUNC doesn't validate the chan, it validates the items on the chan one by one?


SOME-FUNC would be the function I'm looking for, a higher-order function like async/map or pipeline. It does what you said, yes.


what would it do for invalid input?


It's higher-order, so I'd think whatever its function argument does for invalid input, just like regular map.


so it doesn't actually validate, it just returns what validation-func returns for each element


That's right.


It seems like pipeline is the closest thing, I was just wondering if there was something shorter.


the simplest way to do that is to put a map transudcer onto the chan


Or some slick way to use transducers that I don't know about. Like one of those pleasant surprises where a shorthand like (my-transducer my-chan) actually returns a new channel with the transducer applied to each value.


(onto-chan! (chan (map validate)) coll)


you'd want to separate the chan construction out so you can use the chan actually


(let [ch (chan (map validate))]
  (onto-chan ch coll)
  (go-loop [...]


then, for the go loop, you could probably just use pipeline there


if you do a raw loop, don't forget to call recur


Hmm let me describe what I'm trying to get done. I have load-image-folder> that returns a channel, scans all the files in a folder, loads them with an image library (jimp), and puts them on a channel.


maybe "conform" is a better name than "validate" here?


and if you use mapcat instead of map the validate function can return nil for an item to make it be skipped


Then a function validate-image takes images off that channel and validates them. I'd like this to happen "as they become available", so that I can fail as early as possible in case load-image-folder> is loading hundreds of images.


this is getting kind of in depth, let's move it to a thread as a favor to other people using the channel


if validate-image fails, does the whole thing fail?


To me, that function looks like:

(let [input-ch (load-image-folder> my-path)
      valid-ch (SOME-FUNC validate-image input-ch)]
        (loop [im (<! valid-ch)]
         ;; Do stuff or throw error on invalid im.))


you're giving me a possible syntax but I don't fully understand the behavior needed


Sorry I sent that too early, I just edited.


so the error is just on the one image


Yes, if validate-image fails the whole thing fails, like you said.


I realize this whole thing needs to be in a larger go-try block or check for the returned error to properly throw.


is go-try actually a thing? I don't see it in the core.async api


From what I've read up on, it's a commonly-used macro to catch and return errors thrown in go blocks.


Your onto-chan example from before was helpful, but as you can see in my case I'm working with data that hasn't come out of the channel yet.


Just realized another mistake in the example I just posted above. Sorry I'm being so unclear.


I'll try another go for my version here:

(let [input-ch (load-image-folder> my-path)
      validation-ch (chan (map #(or (validate-image %) {::error %})))
      result (chan (map (fn [el]
                          (if (::error el)
                              (ret-error el)
                              (process el)))))]
  (pipe result validation-ch)


I only suggested onto-chan because I thought hte input was a collection


this version uses a channel


Totally, glad to clarify that.


fixed it to explicitly return result (the chan that gets everything after processing)


This version looks great, how are values actually getting taken from input-ch and put onto validation-ch?


oh - I'm still missing some pieces - making another go...


I'm untangling the error handling, I'm getitng it kind of wrong still


but this is a work in progress

(let [input-ch (load-image-folder> my-path)
      validation-ch (chan (map #(or (validate-image %)
                                    {::error %})))
      result (chan (comp (take-while (complement ::error))
                         (map process-image)))]
  (pipe validation-ch input-ch)
  (pipe result validation-ch)
  ;; TODO - consume / acquire the ::error keyed element from validation
  ;; if any?


it explicitly stops consuming on the first thing with the ::error key


one solution could be that the consumer get two chans "result", and "overflow", and they can check "overflow" for errors when result closes


also, NB, these uses of pipe assume that the input ch is closed when there's no data left to acquire, and then they propagate that by closing their output ch (built in behavior)


the basic concept here is I'm using pipe + chan with transducer as building blocks of a data flow


piple rather than pipeline because I think you really want one-in / one-out rather than batching logic, and that's what pipe does


Amazing. This gets me in the right direction. I guess you can see how I was looking for something that also did the "piping" within the top let block, to save the separate pipe calls after the let block.


But this is so much more elegant that what I was trying before.


I really appreciate your time! Thanks so much for this.


nb. this is not tested code, but I think it's the right usage of the core.async provided ops


Is there a browsable documentation site for cljs.core.async? I've been using the core.async docs at, but I keep running into discrepancies like cljs.core.async/to-chan is deprecated, where I'm supposed to be using async/to-chan!.


what's the discrepancy?


For example, it seems that is still used in JVM Clojure's core.async, where ClojureScript is telling me it's deprecated, and that I should be using to-chan! instead.


This is the same for onto-chan and onto-chan!. Little things like cljs not having <!! and >!! have also tripped me up, and I've had to stumble across the info in forums.


if nothing else, the real docs will be more up to date, I don't know if there's. asimilar site for cljs.core.async though


Oh you're right, there's no discrepency. just must be out of date.


Thank you for the second time today!


Okay, last dumb question today. A have that async/pipe returns its to argument, a channel. This seems like it would be very useful, but the async/pipe return value (in cljs) seems to be producing nil when I take! from it. Is anyone else using async/pipe this way?


I've always explicitly used the to-chan I pass to pipe, rather than using the return value


Got it. It just seemed like a useful one-line solution to the problem you and I were discussing before.


you could use a small function to return the to-chan


also - my simple example from before had the channels flipped, oops


I'm used to destinations on the left, sources on the right (even in assembly which I've been punishing myself with lately)


I'm really sorry to hear that. You're right, I think a small function like that is going to be my solution today. The I posted a moment ago has an example of a pipe-trans function that should fit the bill.


pipe definitely does return the to chan, getting nil from a channel means you are taking from a closed channel


by default pipe closes the to channel after the from channel closes (which terminates the piping)


the other thing to do is to double check you use of take!, because it always returns nil, the taken value is passed to the callback you pass in (I forget if cljs does arity checks, or just doesn't because js doesn't, so you might accidentally leave out a parameter)


@hiredman just tried out everything you said, and you're right on all counts. And cljs does indeed do an arity check if I forget the take! callback, which is handy.


I was going nuts because I was doing this:

(def a (async/to-chan! (range 100)))
(def b (async/chan 16 #(map inc)))
(def c (async/pipe a b))
;; hiredman is correct
(identical? b c) ;; => true
(poll! a) ;; => nil
(poll! b) ;; => nil
(poll! c) ;; => nil


Tearing my hair out because my poll! calls were returning nil, but realized it was because I put #(map inc) inside an anonymous function, where a transducer should just be written as (map inc). I don't really understand why that closed all my channels, but really happy that pipe works this way!