Re: newbee mystery

2016-10-09 Thread Colin Yates
I wouldn't say it is different, only that the REPL is doing an extra
step (printing) which forces the evaluation of that lazy sequence. As
James mentions, your code can force the evaluation of a lazy sequence
using doseq or dorun.

Lazy sequences are great when you get used to them but they can trip
people up, particularly in the fact that often a fn will return a lazy
seq which is then processed into another lazy seq which is then
processed into another lazy seq etc. etc. Finally it is evaluated and
an exception is thrown, and whilst the stack trace will contain the
call site that threw the exception it may be far away from the fn that
actually evaluates the seq.

You might also want to ensure that your REPL has a sensible value for
*print-length* (https://clojuredocs.org/clojure.core/*print-length*)
which stops the REPL evaluating the entire lazy sequence.

Without it, (range) will do nothing in your code but will blow up your
REPL :-).

On 9 October 2016 at 03:29, Phil Virgo  wrote:
> Thanks James.  So the repl behaves differently from some compiled code -
> good to know!
>
> I don't yet know about the "safe" issues "read-eval".  This came with the
> little template built by lein.  But I'll read up on it.
>
> On Saturday, October 8, 2016 at 10:11:29 PM UTC-4, James Reeves wrote:
>>
>>
>>
>> On 9 October 2016 at 03:00, Phil Virgo  wrote:
>>>
>>> WHY?
>>>
>>> The following prints 1/n 2/n 3/n fine in the repl
>>>
>>> (for [f [1 2 3]] (println f))
>>>
>>> But does nothing in this lein project (the "Hello World???" does print)
>>>
>>> (ns slide.core
>>>   (:gen-class))
>>>
>>> (defn -main
>>>   [& args]
>>>   ;; work around dangerous default behaviour in Clojure
>>>   (alter-var-root #'*read-eval* (constantly false))
>>> (for [f [1 2 3]] (println f))
>>>   (println "Hello, World??"))
>>
>>
>> The "for" macro is lazy. It only evaluates the items in the list when it's
>> consumed. You want the "doseq" macro instead, which is used for
>> side-effects.
>>
>> Incidentally, setting *read-eval* to false doesn't make clojure.core/read
>> safe. You should always consider clojure.core/read to be unsafe for use with
>> data from sources you don't control. Instead use something like
>> clojure.edn/read, which is designed to be safe.
>>
>> - James
>
> --
> 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.


Re: newbee mystery

2016-10-08 Thread Phil Virgo
Thanks James.  So the repl behaves differently from some compiled code - 
good to know!

I don't yet know about the "safe" issues "read-eval".  This came with the 
little template built by lein.  But I'll read up on it.

On Saturday, October 8, 2016 at 10:11:29 PM UTC-4, James Reeves wrote:
>
>
>
> On 9 October 2016 at 03:00, Phil Virgo  
> wrote:
>
>> WHY?  
>>
>> The following prints 1/n 2/n 3/n fine in the repl 
>>
>> (for [f [1 2 3]] (println f))
>>
>> But does nothing in this lein project (the "Hello World???" does print)
>>
>> (ns slide.core
>>   (:gen-class))
>>
>> (defn -main
>>   [& args]
>>   ;; work around dangerous default behaviour in Clojure
>>   (alter-var-root #'*read-eval* (constantly false))
>> (for [f [1 2 3]] (println f))
>>   (println "Hello, World??"))
>>
>
> The "for" macro is lazy. It only evaluates the items in the list when it's 
> consumed. You want the "doseq" macro instead, which is used for 
> side-effects.
>
> Incidentally, setting *read-eval* to false doesn't make clojure.core/read 
> safe. You should always consider clojure.core/read to be unsafe for use 
> with data from sources you don't control. Instead use something like 
> clojure.edn/read, which is designed to be safe.
>
> - James
>

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


Re: newbee mystery

2016-10-08 Thread James Reeves
On 9 October 2016 at 03:00, Phil Virgo  wrote:

> WHY?
>
> The following prints 1/n 2/n 3/n fine in the repl
>
> (for [f [1 2 3]] (println f))
>
> But does nothing in this lein project (the "Hello World???" does print)
>
> (ns slide.core
>   (:gen-class))
>
> (defn -main
>   [& args]
>   ;; work around dangerous default behaviour in Clojure
>   (alter-var-root #'*read-eval* (constantly false))
> (for [f [1 2 3]] (println f))
>   (println "Hello, World??"))
>

The "for" macro is lazy. It only evaluates the items in the list when it's
consumed. You want the "doseq" macro instead, which is used for
side-effects.

Incidentally, setting *read-eval* to false doesn't make clojure.core/read
safe. You should always consider clojure.core/read to be unsafe for use
with data from sources you don't control. Instead use something like
clojure.edn/read, which is designed to be safe.

- James

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


newbee mystery

2016-10-08 Thread Phil Virgo
WHY?  

The following prints 1/n 2/n 3/n fine in the repl 

(for [f [1 2 3]] (println f))

But does nothing in this lein project (the "Hello World???" does print)

(ns slide.core
  (:gen-class))

(defn -main
  [& args]
  ;; work around dangerous default behaviour in Clojure
  (alter-var-root #'*read-eval* (constantly false))
(for [f [1 2 3]] (println f))
  (println "Hello, World??"))

(Thanks !)

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