开发者

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

开发者 https://www.devze.com 2023-03-02 14:36 出处:网络
I\'m exploring clojure and am puzzled. What fills in the blank to make the following expression eval to true?

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

(= "foo"开发者_Go百科 ___ (str (seq "foo")))


You need to use apply function:

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消