clojure.core/chars¶ View this page on ClojureDocs Type: function Added: Clojure 1.1 Examples: 3 Runnable: 1 ([xs]) Casts to chars[] Examples¶ by zezhenyan on 2017-02-10 . May have evaluation errors. ▶ (seq (chars (char-array "this is a good one"))) (\t \h \i \s \space \i \s \space \a \space \g \o \o \d \space \o \n \e) by tomdl89 on 2022-07-01 . May have evaluation errors. ▶ ;; char-array will convert where possible (char-array "foo");; => [\f, \o, \o] ;; chars will not (try (chars "foo") (catch ClassCastException e (ex-message e))) ;; => "java.lang.String cannot be cast to [C" by tomdl89 on 2023-04-23 ▶ ;; Casting avoids expensive reflection, so to see the benefit, enable warning: (set! *warn-on-reflection* true) ;; We'll def a char-array but won't type-hint the var: (def my-array (char-array [\f \g \h \i \j])) ;; and try to amap over it without using `chars` or type hinting: (amap my-array i _ (-> my-array (aget i) clojure.string/capitalize first unchecked-char)) ;; Reflection warning… call to static method alength on clojure.lang.RT can't be resolved (argument types: unknown). ;; Reflection warning… call to static method aclone on clojure.lang.RT can't be resolved (argument types: unknown). ;; Reflection warning… call to static method aget on clojure.lang.RT can't be resolved (argument types: unknown, int). ;; Reflection warning… call to static method aset on clojure.lang.RT can't be resolved (argument types: unknown, int, char). ;; => [\F, \G, \H, \I, \J] ;; We can use `chars` to avoid reflection: (amap (chars my-array) i _ (-> (chars my-array) (aget i) clojure.string/capitalize first unchecked-char)) ;; => [\F, \G, \H, \I, \J] ;; Just as we can type hint in place: (amap ^chars my-array i _ (-> ^chars my-array (aget i) clojure.string/capitalize first unchecked-char)) ;; => [\F, \G, \H, \I, \J] ;; Or type hint the var: (def ^"[C" my-array (char-array [\f \g \h \i \j])) (amap my-array i _ (-> my-array (aget i) clojure.string/capitalize first unchecked-char)) ;; => [\F, \G, \H, \I, \J] See also¶ clojure.core/char-array (0) Content from the matching ClojureDocs page, with authors credited on each contribution.