Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a list from a string in Clojure

I'm looking to create a list of characters using a string as my source. I did a bit of googling and came up with nothing so then I wrote a function that did what I wanted:

(defn list-from-string [char-string]
  (loop [source char-string result ()]
    (def result-char (string/take 1 source))
    (cond
     (empty? source) result
     :else (recur (string/drop 1 source) (conj result result-char)))))

But looking at this makes me feel like I must be missing a trick.

  1. Is there a core or contrib function that does this for me? Surely I'm just being dumb right?
  2. If not is there a way to improve this code?
  3. Would the same thing work for numbers too?
like image 964
robertpostill Avatar asked Jan 29 '11 12:01

robertpostill


3 Answers

You can just use seq function to do this:

user=> (seq "aaa")
(\a \a \a)

for numbers you can use "dumb" solution, something like:

user=> (map (fn [^Character c] (Character/digit c 10)) (str 12345))
(1 2 3 4 5)

P.S. strings in clojure are 'seq'able, so you can use them as source for any sequence processing functions - map, for, ...

like image 91
Alex Ott Avatar answered Nov 19 '22 23:11

Alex Ott


if you know the input will be letters, just use

user=> (seq "abc")
(\a \b \c)

for numbers, try this

user=> (map #(Character/getNumericValue %) "123")
(1 2 3)
like image 35
Matthew Boston Avatar answered Nov 19 '22 22:11

Matthew Boston


Edit: Oops, thought you wanted a list of different characters. For that, use the core function "frequencies".

clojure.core/frequencies
([coll])
  Returns a map from distinct items in coll to the number of times they appear.

Example:

user=> (frequencies "lazybrownfox")
{\a 1, \b 1, \f 1, \l 1, \n 1, \o 2, \r 1, \w 1, \x 1, \y 1, \z 1}

Then all you have to do is get the keys and turn them into a string (or not).

user=> (apply str (keys (frequencies "lazybrownfox")))
"abflnorwxyz"
like image 6
Markc Avatar answered Nov 19 '22 23:11

Markc