Re: What sorcery is this? (`reduced`, `reduced?`)

2017-04-25 Thread John Gabriele
Thanks, all! Luke, in `(source reduce)`, that code makes use of clojure.core.protocols/coll-reduce, which I see is in src/clj/clojure/core/protocols.clj. And although I haven't yet made sense of all that, I see that `coll-reduce` calls `seq-reduce`/`iter-reduce`, which in turn call

Re: What sorcery is this? (`reduced`, `reduced?`)

2017-04-25 Thread Alex Miller
The source for both functions is pretty simple (use the source!), although admittedly `reduced?` does some work to optimize perf that obscures it a little. `reduced` is used by a reducing function to indicate to the outer reducing process that a return value should end the reduce. It does this

Re: What sorcery is this? (`reduced`, `reduced?`)

2017-04-25 Thread Luke Burton
To add to Ivh's answer, the source code isn't too hard to follow here. Cursive makes this really easy because you can follow the source right into clojure.lang.RT, but you don't need to go that far to see how `reduced?` is used. You can see that all implementations of `reduce` in

Re: What sorcery is this? (`reduced`, `reduced?`)

2017-04-25 Thread lvh
Hi John, reduced really just wraps a value in a special container; nothing magical. All it does is signal to reduce to stop. reduce knows to stop by checking with reduced?. reduced? is otherwise extremely narrow-use: it's basically only useful if you're trying to write something like reduce but

What sorcery is this? (`reduced`, `reduced?`)

2017-04-25 Thread John Gabriele
Just recently stumbled upon `reduced` and `reduced?`. It seems rather magical... how does the outer `reduce` (or `reductions`) know to stop? That is, how does the function which is being called (`reduced`) affect the function that's calling it (below, `reductions`)? : ~~~clojure (defn main