English 中文(简体)
How can I convert a LazySeq of Characters to a String in Clojure?
原标题:

Let s say I have a LazySeq of java.lang.Character like

(  ! /   \% 1 9 / . i \% $ i space ^@)

How can I convert this to a String? I ve tried the obvious

(String. my-char-seq)

but it throws

java.lang.IllegalArgumentException: No matching ctor found for class java.lang.String (NO_SOURCE_FILE:0)
[Thrown class clojure.lang.Compiler$CompilerException]

I think because the String constructor expects a primitive char[] instead of a LazySeq. So then I tried something like

(String. (into-array my-char-seq))

but it throws the same exception. The problem now is that into-array is returning a java.lang.Character[] instead of a primitive char[]. This is frustrating, because I actually generate my character sequence like this

(map #(char (Integer. %)) seq-of-ascii-ints)

Basically I have a seq of ints representing ASCII characters; 65 = A, etc. You can see I explicitly use the primitive type coercion function (char x).

What this means is that my map function is returning a primitive char but the Clojure map function overall is returning the java.lang.Character object.

最佳回答

This works:

(apply str my-char-seq)

Basically, str calls toString() on each of its args and then concatenates them. Here we are using apply to pass the characters in the sequence as args to str.

问题回答

Another way is to use clojure.string/join, as follows:

(require  [clojure.string :as str] )
(assert (= (vec "abcd")                [a  c d] ))
(assert (= (str/join  (vec "abcd"))    "abcd" ))
(assert (= (apply str (vec "abcd"))    "abcd" ))

There is an alternate form of clojure.string/join which accepts a separator. See:

http://clojuredocs.org/clojure_core/clojure.string/join

For more complicated problems, you may also wish to lookat strcat from the Tupelo library:

(require  [tupelo.core :as t] )
(prn (t/strcat "I " [ h a nil v [e space (byte-array [97])
                  [ nil 32 "complicated" (Math/pow 2 5)  ( "str" nil "ing") ]]] ))
;=> "I have a complicated string"

As a special case, if the underlying type of the sequence in question is clojure.lang.StringSeq you can also do:

(.s (my-seq))

which is extremely performant as it is just pulling out the public final CharSequence field from the clojure StringSeq class.

Example:

(type (seq "foo"))
=> clojure.lang.StringSeq

(.s (seq "foo"))
=> "foo"

(type (.s (seq "foo")))
=> java.lang.String

an example of the timing implications (and note the difference when using a type hint):

(time 
  (let [q (seq "xxxxxxxxxxxxxxxxxxxx")]
    (dotimes [_ 1000000]
      (apply str q))))
"Elapsed time: 620.943971 msecs"
=> nil

(time 
  (let [q (seq "xxxxxxxxxxxxxxxxxxxx")]
    (dotimes [_ 1000000]
      (.s q))))
"Elapsed time: 1232.119319 msecs"
=> nil

(time 
  (let [^StringSeq q (seq "xxxxxxxxxxxxxxxxxxxx")]
    (dotimes [_ 1000000]
      (.s q))))
"Elapsed time: 3.339613 msecs"
=> nil




相关问题
Having many stacks with different types

I m making a C program that needs to use two stacks. One needs to hold chars, the other needs to hold doubles. I have two structs, node and stack: struct node { double value; struct node *...

Creating (boxed) primitive instance when the class is known

I need a method that returns an instance of the supplied class type. Let s assume that the supplied types are limited to such that an "empty" instance of them can be created. For instance, supplying ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

How to generate an instance of an unknown type at runtime?

i ve got the following in C#: string typename = "System.Int32"; string value = "4"; theses two strings should be taken to generate an object of the specified type with the specified value... result ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

热门标签