Clojure into-array iterate over array [closed]

2019-09-21 18:01发布

问题:

Im trying to make the change to Clojure from a primarily Java only experience and i am trying to solve a problem in Clojure. I can do it in Java and it shouldnt be too much harder to convert to Clojure but i just cannot see how it would work....

What i want to do is iterate over an array of strings and then for ever string put into a map, my pseudo code is below but looks alot like Java

      private static void putToMap(String w) 
      {
        String thisw = getthisw(w);
        if (!map.containsKey(thisw)) 
        {
          map.put(thisw, w); 
          map.add(w);
        } 
      }

      private static String getthisw(String w) 
      {
        char [] wArray = w.toLowerCase().toCwarArray();
        Arrays.sort(wArray);
        return new String(wArray);
      }

This is how i am trying to do it in Clojure but im not sure how i would get data from the array that is given in the output if there is any?

The output i get from running what i have is this.... How would i use this to iterate over?? if so how would i use the array im given??

#<String[] [Ljava.lang.String;@790da477>

My Clojure functions....

(def strings '["one" "two" "three" "four" "five" ])

(defn sort [strings]
  (into-array String strings))

Example of input:

Array of Strings ["ONE"] ["TWO"] ["THREE"] ["FOUR"] ["FIVE"]

Example of output:

Map with strings converted to lowercase "one" "two" "three" "four" "five"

回答1:

It really is tough to say exactly what you want here, but it seems you are thinking in the Java way. A simple map will convert your strings into a list of their lower-case counterparts.

(def strings ["ONE" "TWO" "THREE" "FOUR" "FIVE"])

(defn convert [strings]
  (prn (map clojure.string/lower-case strings)))

Then you can call it in your REPL and get:

user=> (convert strings)
("one" "two" "three" "four" "five")