mark
mark

Reputation: 233

How do I turn (seq "foo") back into a string?

I'm exploring clojure and am puzzled. What fills in the blank to make the following expression eval to true?

(= "foo" ___ (str (seq "foo")))

Upvotes: 23

Views: 6289

Answers (3)

isakkarlsson
isakkarlsson

Reputation: 1109

Or you could use the convient clojure.string/join

(require 'clojure.string)
(clojure.string/join (seq "foo"))

Will print "foo" as well. You could off course define your own join using the previously mentioned technique (to avoid write (apply str seq) all the time).

Upvotes: 4

Jaskirat
Jaskirat

Reputation: 1094

Actually nothing can fill in the blank to make the expression (= "foo" _ (str (seq "foo"))) eval to true because (str (seq "foo")) => "(\f \o \o)" which is not equal to "foo" so we have an inequality already and a third item, no matter what value, to fill the blank cannot make the expression evaluate to true

If you meant to ask

(= "foo"
    (____ str (seq "foo")))

Then the answer would rightly be apply as answered by alex.

user> (doc apply)
-------------------------
clojure.core/apply
([f args* argseq])
  Applies fn f to the argument list formed by prepending args to argseq.

Apply takes a function (in this case str) and calls str with the args present in the seq

user> (seq "foo")
(\f \o \o)

user> (str \f \o \o)
"foo"

And oh, btw:

user> (= 1 1 1)
true

user> (= 1 2 1)
false

Upvotes: 5

Alex Ott
Alex Ott

Reputation: 87174

You need to use apply function:

user=> (apply str (seq "foo"))
"foo"

Upvotes: 33

Related Questions