Clojure的成阵遍历数组 [英] Clojure into-array iterate over array

查看:156
本文介绍了Clojure的成阵遍历数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着从一个主要由Java仅凭经验进行更改Clojure的,我试图用Clojure来解决问题。我能做到这一点在Java中,这不应该是太更难转换为Clojure的,但我就是不能看到它是如何工作....

我想要做什么是迭代字符串数组,然后永远串放入一个地图,我的伪code是低于但看起来很多像Java

 私有静态无效putToMap(字符串W)
      {
        串thisw = getthisw(重量);
        如果(!map.containsKey(thisw))
        {
          map.put(thisw,w)的;
          map.add(重量);
        }
      }      私有静态字符串getthisw(字符串W)
      {
        的char [] = wArray w.toLowerCase()toCwarArray()。
        Arrays.sort(wArray);
        返回新的String(wArray);
      }

这就是我如何试图做它用Clojure但林不知道我怎么会从那个在输出中给出的阵列中的数据是否有?

输出我在逃避什么我已经是这样的....获得我怎么会用这个来遍历?如果是的话会怎么我用给定??阵列IM

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

我的Clojure等功能。

 (DEF字符串[一二三四十二五])(DEFN排序[字符串]
  (成阵列串串))

输入示例:

 字符串数组[ONE] [两会] [三化] [四] [十二五]

输出的例子:

 地图与转换为小写一二三四十二五的字符串


解决方案

这真的是很难说清楚你想要的这里,但似乎你正在考虑在Java的方式。一个简单的地图将您的字符串转换成他们的小写同行的列表。

 (DEF字符串[一二三四十二五])(DEFN转换[字符串]
  (PRN(图clojure.string /小写的字符串)))

然后就可以调用它在你的REPL并获得:

 用户=> (字符串转换)
(一二三四五)

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"

解决方案

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

这篇关于Clojure的成阵遍历数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆