Clojure Atoms,我想返回最终值
好的,在我的函数中放入一个数组,我保留了一个数组,然后做一些事情返回一个hashmap,其中一个字符串作为键,一个列表作为值。我只想得到最终值,并将其作为参数传递给另一个函数 下面是我试图翻译成clojure的java代码Clojure Atoms,我想返回最终值,clojure,Clojure,好的,在我的函数中放入一个数组,我保留了一个数组,然后做一些事情返回一个hashmap,其中一个字符串作为键,一个列表作为值。我只想得到最终值,并将其作为参数传递给另一个函数 下面是我试图翻译成clojure的java代码 contents=documentContent.split(“[,\\.]+”); for(int i=0;i(clojure.string/split s#“[,\\.]+”)(映射索引列表))您将看到结果(我猜这就是您正在谈论的列表).这对大序列不是有问题吗?请参阅和(
contents=documentContent.split(“[,\\.]+”);
for(int i=0;i
Clojure代码到目前为止,它有些工作,但我不能返回最终值并将其传递给另一个函数
(defn indexFinder [contents]
(let [mem (def xs (atom {}))]
(map-indexed
(fn [index element]
(if (contains? @xs element)
(swap! xs assoc element (conj (@xs element) index))
(swap! xs assoc element [index])))
contents)))
首先我建议不要使用原子
(->> (zipmap (range) contents) ;contents is a list of words
(reduce (fn [index [location word]]
(merge-with concat index {word (list location)})) {}))
例如:
user=> (def contents (clojure.string/split "hello there these are some words and some repeated words" #" "))
#'user/contents
user=> contents
["hello" "there" "these" "are" "some" "words" "and" "some" "repeated" "words"]
user=> (->> (zipmap (range) contents)
#_=> (reduce (fn [index [location word]]
#_=> (merge-with concat index {word (list location)})) {}))
{"hello" (0), "some" (7 4), "there" (1), "and" (6), "are" (3), "these" (2), "words" (9 5), "repeated" (8)}
首先我建议不要使用原子
(->> (zipmap (range) contents) ;contents is a list of words
(reduce (fn [index [location word]]
(merge-with concat index {word (list location)})) {}))
例如:
user=> (def contents (clojure.string/split "hello there these are some words and some repeated words" #" "))
#'user/contents
user=> contents
["hello" "there" "these" "are" "some" "words" "and" "some" "repeated" "words"]
user=> (->> (zipmap (range) contents)
#_=> (reduce (fn [index [location word]]
#_=> (merge-with concat index {word (list location)})) {}))
{"hello" (0), "some" (7 4), "there" (1), "and" (6), "are" (3), "these" (2), "words" (9 5), "repeated" (8)}
原始indexFinder缺少一个doall来强制执行其副作用(更新atom)。此外,不需要声明mem。因此,工作版本可能如下所示:
(defn indexFinder [contents]
(let [xs (atom {})]
(doall (map-indexed
(fn [index element]
(if (contains? @xs element)
(swap! xs assoc element (conj (@xs element) index))
(swap! xs assoc element [index])))
contents))
@xs))
还可以简化地图索引部分:
(defn indexFinder [contents]
(let [xs (atom {})]
(doall (map-indexed
(fn [i e] (swap! xs update e conj i))
contents))
@xs))
或者,如果您不想依赖副作用,您可以使用此选项(与Brandon的答案类似,但使用group by):
其中,fmap用于将函数映射到值上(参见算法通用):
原始indexFinder缺少一个doall来强制执行其副作用(更新atom)。此外,不需要声明mem。因此,工作版本可能如下所示:
(defn indexFinder [contents]
(let [xs (atom {})]
(doall (map-indexed
(fn [index element]
(if (contains? @xs element)
(swap! xs assoc element (conj (@xs element) index))
(swap! xs assoc element [index])))
contents))
@xs))
还可以简化地图索引部分:
(defn indexFinder [contents]
(let [xs (atom {})]
(doall (map-indexed
(fn [i e] (swap! xs update e conj i))
contents))
@xs))
或者,如果您不想依赖副作用,您可以使用此选项(与Brandon的答案类似,但使用group by):
其中,fmap用于将函数映射到值上(参见算法通用):
我会这样写:
(defn index-finder [s]
(->> (clojure.string/split s #"[ ,\\.]+")
(map-indexed list)
(reduce (fn [acc [i w]]
(update acc w conj i)) {})))
示例用法:
(index-finder "aaa bbb ccc ddd aaa bbb") ;; {"aaa" (4 0), "bbb" (5 1), "ccc" (2), "ddd" (3)}
注意:当开始学习Clojure时,人们往往认为他需要经常学习原子。以下是我的写作方式:
(defn index-finder [s]
(->> (clojure.string/split s #"[ ,\\.]+")
(map-indexed list)
(reduce (fn [acc [i w]]
(update acc w conj i)) {})))
示例用法:
(index-finder "aaa bbb ccc ddd aaa bbb") ;; {"aaa" (4 0), "bbb" (5 1), "ccc" (2), "ddd" (3)}
注意:当开始学习Clojure时,人们倾向于认为他需要经常使用原子。不要在这样的let中使用def。或者在函数内部。不要在let内部使用def。或者在函数内部。你好,nha,你的代码似乎做了我想做的,但我很难理解。地图索引dalso@nha中的列表的目的是什么?在完成句子后,向量是如何工作的split@user3602477使用单个函数了解它们,查找地图上的文档索引、减少、更新、合并。。。例如,尝试
(映射索引列表[“a”“b”“c”“d]”)
(conj nil 1)
(conj'(1)2)
。clojure文档中有很多例子,例如更新:我确实不了解这些函数,但我对clojure是新手,所以请耐心等待。我只是想知道这个列表包含了什么。因为在我们分开句子后,s被分配给向量,但我看不到s被使用again@user3602477然后尝试以下操作:(defn index finder[s](->(clojure.string/split s#“[,\\.]+”)(映射索引列表))
您将看到结果(我猜这就是您正在谈论的列表)。您好,nha,你的代码似乎符合我的要求,但我很难理解。地图索引dalso@nha中的列表的目的是什么?在完成句子后,向量是如何工作的split@user3602477使用单个函数了解它们,查找地图上的文档索引、减少、更新、合并。。。例如,尝试(映射索引列表[“a”“b”“c”“d]”)
(conj nil 1)
(conj'(1)2)
。clojure文档中有很多例子,例如更新:我确实不了解这些函数,但我对clojure是新手,所以请耐心等待。我只是想知道这个列表包含了什么。因为在我们分开句子后,s被分配给向量,但我看不到s被使用again@user3602477然后尝试以下操作:(defn index finder[s](->(clojure.string/split s#“[,\\.]+”)(映射索引列表))
您将看到结果(我猜这就是您正在谈论的列表).这对大序列不是有问题吗?请参阅和(基本上使用reduce和concat似乎很危险(知道merge with uses reduce))这对大序列不是有问题吗?请参阅和(基本上使用reduce和concat似乎很危险(知道merge with使用reduce))