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 different
yourself.


lvh

On Tue, Apr 25, 2017 at 2:34 PM, John Gabriele <jmg3...@gmail.com> wrote:

> 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
>   []
>   (let [res (reductions (fn [accum x]
>                           (if (< accum 100)
>                             (+ accum x)
>                             (reduced [x accum])))
>                         (range))]
>     (prn res)
>     (prn (reduced? res)) ;=> false
>     (prn (reduced? (last res))))) ;=> false --- why isn't this `true`?
> ~~~
>
> Also, what's the use of `reduced?`, and on what in the above snippet would
> I call it to return true?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to