Is str supposed to be able to output lazy strings?

811 views
Skip to first unread message

Folcon

unread,
Dec 6, 2009, 12:57:45 PM12/6/09
to Clojure
I just called str on a function and it outputted:

"SELECT * FROM QUERY WHERE TYPE='query' clojure.lang.LazySeq@a600e679"

if this is expected behavior how do you force it to evaluate the
sequence before turning it into a string?

Regards,
Folcon

CuppoJava

unread,
Dec 6, 2009, 1:20:40 PM12/6/09
to Clojure
This is expected behavior.

eg. (str (map identity [1 2 3]))
returns "clojure.lang.LazySeq@7861"

The way to think about it is, (str) asks for the string representation
of an object. The string representation of a lazy sequence in this
case is "clojure.lang.LazySeq@7861".

If you want the string representations of what's inside the lazy seq,
you can use the apply function.

(apply str (map identity [1 2 3]))
"123"

Alternatively, you can convert the lazy-seq to a vector. The string
representation for a vector shows the elements inside of it.

(str (vec (map identity [1 2 3])))
"[1 2 3]"

Hope that makes sense.
-Patrick

samppi

unread,
Dec 6, 2009, 2:03:40 PM12/6/09
to Clojure
Also, you should also consider simply using the seq function, which is
what you should use when you want just to evaluate a lazy sequence:

(str (seq (map identity [1 2 3])))
"(1 2 3)"

Dave M

unread,
Dec 6, 2009, 2:13:59 PM12/6/09
to Clojure

On Dec 6, 1:20 pm, CuppoJava <patrickli_2...@hotmail.com> wrote:
> This is expected behavior.
>
> eg. (str (map identity [1 2 3]))
> returns "clojure.lang.LazySeq@7861"
>
> The way to think about it is, (str) asks for the string representation
> of an object. The string representation of a lazy sequence in this
> case is "clojure.lang.LazySeq@7861".
>
> If you want the string representations of what's inside the lazy seq,
> you can use the apply function.
>
> (apply str (map identity [1 2 3]))
> "123"

API docs for the arguments to apply - "([f args* argseq])"; so the
following is perhaps more straight-forward:

(apply str (seq [1 2 3]))
=> "123"

Additionally, the following works too in Cloure 1.0.0:

(apply str [1 2 3])
=> "123"

...though it does not follow from the API docs, which imply that the
last argument to apply should be seq, and "(seq? [1 2 3])" => false.


-Dave

Meikel Brandmeyer

unread,
Dec 6, 2009, 3:24:02 PM12/6/09
to clo...@googlegroups.com
Hi,

Am 06.12.2009 um 20:13 schrieb Dave M:

>> (apply str (map identity [1 2 3]))
>> "123"
>
> API docs for the arguments to apply - "([f args* argseq])"; so the
> following is perhaps more straight-forward:

Ad the "straight-forward": I think this was just an example to show the lazy-seq issue since map returns one.

> Additionally, the following works too in Cloure 1.0.0:
>
> (apply str [1 2 3])
> => "123"
>
> ...though it does not follow from the API docs, which imply that the
> last argument to apply should be seq, and "(seq? [1 2 3])" => false.

The clojure sequence library is nice on you in that it calls seq on its arguments if it's supposed to be one. So you can also use eg. vectors were seqs are expected.

Sincerely
Meikel

Jim Blomo

unread,
Dec 7, 2009, 3:23:08 AM12/7/09
to clo...@googlegroups.com
clojure-contrib also has a str-join function, which could be helpful
for joining SQL conditions:

(use '[clojure.contrib.str-utils :only (str-join)])
(str-join " AND " (map identity [1 2 3]))

Jim
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
Reply all
Reply to author
Forward
0 new messages