explode a string to a list

949 views
Skip to first unread message

clint.laskowski

unread,
Aug 22, 2009, 4:54:49 AM8/22/09
to Clojure
Sorry if this is a FAQ. I'm a Clojure newbie.

What is the best way to iterate through the characters of a string? Is
there some kind of EXPLODE function such that:

=> (explode "test")
(\t \e \s \t)

I did a Google search but the closest thing I found was SUBS:

=>(subs "test" 1 2)
"t"

Chouser

unread,
Aug 22, 2009, 8:16:10 AM8/22/09
to clo...@googlegroups.com
On Sat, Aug 22, 2009 at 4:54 AM,
clint.laskowski<clint.l...@gmail.com> wrote:
>
> Sorry if this is a FAQ. I'm a Clojure newbie.
>
> What is the best way to iterate through the characters of a string? Is
> there some kind of EXPLODE function such that:
>
> => (explode "test")
> (\t \e \s \t)

'seq' creates a sequence out of almost anything, including strings:

user=> (seq "test")


(\t \e \s \t)

Note this is a lazy seq, though the laziness doesn't matter
much on a string. A seq is not exactly the same as a list,
but will generally do.

--Chouser

Michel Salim

unread,
Aug 22, 2009, 8:16:53 AM8/22/09
to clo...@googlegroups.com
On Sat, 2009-08-22 at 01:54 -0700, clint.laskowski wrote:

> What is the best way to iterate through the characters of a string? Is
> there some kind of EXPLODE function such that:
>
> => (explode "test")
> (\t \e \s \t)
>
> I did a Google search but the closest thing I found was SUBS:
>
> =>(subs "test" 1 2)
> "t"

The key rule is that in Clojure, if a data structure can reasonably be
viewed as a sequence, it can be turned into one. Thus

=> (seq "abcde")
(\a \b \c \d \e)

HTH,

--
Michel Salim

Sean Devlin

unread,
Aug 22, 2009, 8:20:00 AM8/22/09
to Clojure
Welcome to Clojure!

A String is a form of a Sequence, so the correct function is seq.

user=>(seq "test")
(\t \e \s \t)

The sequence abstraction is on of may favorite things about Clojure.
It is an interface most collections implement, and it makes it very
consistent to manipulate any "collection-like" object. For mroe read
here:

http://clojure.org/sequences

Also, watching Rich's presentation for Java and/or Lisp developers
will help a lot.


I hope this helps.
Sean

On Aug 22, 4:54 am, "clint.laskowski" <clint.laskow...@gmail.com>
wrote:

Rich Hickey

unread,
Aug 22, 2009, 10:07:12 AM8/22/09
to clo...@googlegroups.com
On Sat, Aug 22, 2009 at 8:20 AM, Sean Devlin<francoi...@gmail.com> wrote:
>
> Welcome to Clojure!
>
> A String is a form of a Sequence, so the correct function is seq.
>

I think we all need to be very careful about calling things sequences
which are not. seq can give you a sequential view of many things, but
that doesn't make those things sequences. Very few of the Clojure data
structures are actual sequences, e.g. vectors, maps, strings etc are
not. Nor do most Clojure collections implement the sequence (ISeq)
interface. They implement the Seqable interface, which means seq
works, and the thing that seq returns implements ISeq. The fact that
first/rest/other-sequence-fns work on most collections falls out of
the fact that they call seq on their args.

It may seem like a nit, but I've seen a lot of confusion, e.g. people
listing The Sequence Types: Vector, Map etc. Each of the data
structures has important and unique characteristics (insertion, lookup
etc) when not used sequentially. It just makes a mess of the
abstractions to call them sequences.

Rich

Sean Devlin

unread,
Aug 22, 2009, 10:32:43 AM8/22/09
to Clojure
Good point. Obviously java.lang.String doesn't implement any extra
interfaces.

The correct thing to say is that a String is something seq works on.

On Aug 22, 10:07 am, Rich Hickey <richhic...@gmail.com> wrote:

clint.laskowski

unread,
Aug 22, 2009, 12:36:33 PM8/22/09
to Clojure
Thank you all for your replies and your help. I never expected Rich
Hickey would respond :-)

-- Clint

On Aug 22, 9:07 am, Rich Hickey <richhic...@gmail.com> wrote:

Conrad

unread,
Sep 8, 2009, 6:05:48 PM9/8/09
to Clojure
Although this shows how to convert a string into a seq of chars, for
the life of me I can't find a function in any libraries (or any info
in the newsgroup) to do the reverse, i.e. (\t \e \s \t) => "test"...
The closest I can find is (print-str [\t \e \s \t])=>"t e s t"

...can anyone give me a pointer?

Thanks in advance!

On Aug 22, 8:20 am, Sean Devlin <francoisdev...@gmail.com> wrote:
> Welcome to Clojure!
>
> A String is a form of a Sequence, so the correct function is seq.
>
> user=>(seq "test")
> (\t \e \s \t)
>
> The sequence abstraction is on of may favorite things about Clojure.
> It is an interface most collections implement, and it makes it very
> consistent to manipulate any "collection-like" object.  For mroe read
> here:
>
> http://clojure.org/sequences
>
> Also, watching Rich's presentation for Java and/or Lisp developers
> will help a lot.
>
> I hope this helps.
> Sean
>
> On Aug 22, 4:54 am, "clint.laskowski" <clint.laskow...@gmail.com>
> wrote:
>
> > Sorry if this is a FAQ. I'm a Clojure newbie.
>
> > What is the best way to iterate through thecharactersof a string? Is

Conrad

unread,
Sep 8, 2009, 6:08:19 PM9/8/09
to Clojure
Never mind- I figured it out looking at the str-utils source...

the answer is (apply str [\t \e \s \t])
Reply all
Reply to author
Forward
0 new messages