When to halt-when?

264 views
Skip to first unread message

Leon Grapenthin

unread,
Nov 3, 2016, 2:06:36 PM11/3/16
to Clojure
What is the intended usage of halt-when? 

The naming of halt-when hints at imperative / non-collection contexts but I'm failing to guess what such a context could look like? 

What is an intended usecase for the default behavior "the input that triggered the predicate will be returned"? 

For collection contexts, it seems odd that the transduced result is not opaque to retf because it requires one to make the transducer depend on the later application which breaks promise/design of transducers as reusable algorithmic transformations in many contexts.

To illustrate

(into [] (halt-when true? conj) [false false true])

Doesn't work because into internally uses transients. One has to (halt-when true? conj!), then [false false true] is returned - but it is a hack using the internals of into.

(sequence (halt-when true? conj) [false false true])

Returns (false false), a different result.

For core.async channels I can imagine that it doesn't work at all, but have not tried.

Alex Miller

unread,
Nov 3, 2016, 3:03:16 PM11/3/16
to Clojure
halt-when does require some knowledge about the transducible context where it is used, particularly because it will be exposed to the completed result of the computation. I think it's really primarily applicable for something like transduce (and not as much for into, sequence, eduction, or chan for differing reasons).

(transduce (comp (halt-when :err) (map inc)) + 0

  (range 5))

;;=> 15


(transduce (comp (halt-when :err) (map inc)) + 0

   (concat (range 3) [{:err "oops"}] (range 3)))

;;=> {:err "oops"}

Leon Grapenthin

unread,
Nov 3, 2016, 4:40:12 PM11/3/16
to Clojure
Thanks for answering, Alex. I do already understand the limitations of halt-when from the implementation point of view.

To make my question more clear: What kind of practical problem is halt-when designed to solve, especially as a very limited transducer?

I am failing derive any practical problem from your examples and similar ones I have made up for myself - Inputs / transducers producing custom error objects that one wants to return don't really seem like something one encounters when writing Clojure, do they?

Kind regards,
 Leon.
Reply all
Reply to author
Forward
0 new messages