doall vs. dorun

431 views
Skip to first unread message

Mark Volkmann

unread,
Jan 20, 2009, 3:32:29 PM1/20/09
to clo...@googlegroups.com
Can someone describe a situation where it is preferable to use doall
instead of dorun? I see in the documentation that it "retains the head
and returns it, thus causing the entire seq to reside in memory at one
time", but I'm not sure why I'd want that.

--
R. Mark Volkmann
Object Computing, Inc.

Nathanael Cunningham

unread,
Jan 20, 2009, 4:10:39 PM1/20/09
to clo...@googlegroups.com
Pretty much any lazy-seq thats reading data from somewhere that might give up on you if you take to long. For example: Your using line-seq to read from a socket, but the sequence wont be read through until the user does something.
--
-Nate

Stuart Sierra

unread,
Jan 20, 2009, 4:14:31 PM1/20/09
to Clojure
On Jan 20, 3:32 pm, Mark Volkmann <r.mark.volkm...@gmail.com> wrote:
> Can someone describe a situation where it is preferable to use doall
> instead of dorun?

Here's one:

(defn read-my-file []
(with-open [reader (BufferedReader. (FileReader. "my-file.txt"))]
(doall (line-seq reader))))

"line-seq" returns a lazy sequence, but you have to consume that
sequence before "with-open" closes the file.

-Stuart Sierra

Mark Triggs

unread,
Jan 20, 2009, 4:31:16 PM1/20/09
to clo...@googlegroups.com
In addition to what others have said, I also tend to use doall when
working with agent actions that return sequences (i.e. to force any
computation to happen in the agent's thread and not in the caller's)

Cheers,

Mark

Mark Volkmann

unread,
Jan 20, 2009, 5:32:57 PM1/20/09
to clo...@googlegroups.com

How is it different if you change "doall" to "dorun"? According to
their doc strings, they both "can be used to force any effects. Walks
through the successive rests of the seq"

Stephen C. Gilardi

unread,
Jan 20, 2009, 5:53:20 PM1/20/09
to clo...@googlegroups.com

On Jan 20, 2009, at 5:32 PM, Mark Volkmann wrote:

>> Here's one:
>>
>> (defn read-my-file []
>> (with-open [reader (BufferedReader. (FileReader. "my-file.txt"))]
>> (doall (line-seq reader))))
>>
>> "line-seq" returns a lazy sequence, but you have to consume that
>> sequence before "with-open" closes the file.
>
> How is it different if you change "doall" to "dorun"? According to
> their doc strings, they both "can be used to force any effects. Walks
> through the successive rests of the seq"

Using doall will cause read-my-file to return a seq of all the lines.
dorun will return nil.

Here's a simpler example:

user=> (doall (map #(do (prn %) %) [1 2 3]))
1
2
3
(1 2 3)
user=> (dorun (map #(do (prn %) %) [1 2 3]))
1
2
3
nil
user=>

(Note the difference in return value.)

--Steve

Cosmin Stejerean

unread,
Jan 20, 2009, 6:13:45 PM1/20/09
to clo...@googlegroups.com
Use dorun when you want to do something purely for the side effects. If you don't need what doall would return then you can use dorun instead to clearly indicate your intent.

--
Cosmin Stejerean
http://offbytwo.com

Mark Volkmann

unread,
Jan 20, 2009, 6:25:39 PM1/20/09
to clo...@googlegroups.com
Thanks Steve and Cosmin! I understand it now.

--

Reply all
Reply to author
Forward
0 new messages