我正在处理一些数据并在最后使用了 interpose。但是当涉及到一些字母时,它的表现就不一样了,` ~
预期行为
(interpose '! ["a" "b" "c"])
=> ("a" ! "b" ! "c")
(interpose 'k ["a" "b" "c"])
=> ("a" k "b" k "c")
(interpose '- ["a" "b" "c"])
=> ("a" - "b" - "c")
Run Code Online (Sandbox Code Playgroud)
非预期行为(它们返回函数而不是数据结果)
(interpose ', ["a" "b" "c"])
=> #object[clojure.core$interpose$fn__6471 0x6f0c27f3 "clojure.core$interpose$fn__6471@6f0c27f3"]
(interpose '` ["a" "b" "c"])
=> #object[clojure.core$interpose$fn__6471 0x7b94fbcb "clojure.core$interpose$fn__6471@7b94fbcb"]
(interpose '~ ["a" "b" "c"])
=> #object[clojure.core$interpose$fn__6471 0x36966945 "clojure.core$interpose$fn__6471@36966945"]
Run Code Online (Sandbox Code Playgroud) (defn DoubleFrequency []
(def s (slurp "Example.txt"))
(def m (reduce #(assoc %1 %2 (inc (%1 %2 0)))
{}
(re-seq #".." s)))
(def c (count m))
(doseq [[k x] m]
(println k ":" (/ x c))))
Run Code Online (Sandbox Code Playgroud)
我正在尝试将并发应用于我的程序,并且我想使用 pmap,但我不确定如何将它应用到我当前的代码中。该功能对于单核是正确的,但理想情况下,我想以某种方式用 pmap 替换 reduce 并获得相同的结果。
为什么我们使用 fn 和 NullPointerException 使用 # ??
Clojure 1.8.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_91-b14
> ((fn [x] (println x) (println x)) "I have a question")
I have a question
I have a question
=> nil
> (#((println %) (println %)) "I have a question")
I have a question
I have a question
NullPointerException user/eval1155/fn--1156 (form-init5537026255318038171.clj:1)
Run Code Online (Sandbox Code Playgroud) 在clojure中绑定python时,遇到如下错误。请评论如何解决此错误。
Exception in thread "main" java.lang.RuntimeException:
Unable to resolve symbol: boolean? in this context, compiling (tech/v2/datatype/casting.clj:154:5)
Run Code Online (Sandbox Code Playgroud)
配置如下
一种。项目.clj
.....
:dependencies [[org.clojure/clojure "1.8.0"]
[environ "1.1.0"]
[clj-python/libpython-clj "1.44"]]
.....
Run Code Online (Sandbox Code Playgroud)
湾 核心文件
(:require [clojure.core.async :refer [<!!]]
[clojure.string :as str]
[environ.core :refer [env]]
[libpython-clj.require :refer [require-python]]
[libpython-clj.python :as py :refer [py. py.. py.-]])
.....
Run Code Online (Sandbox Code Playgroud) 我得到了两个向量[shoes milk shoes]和[1 3 1],而我想要得到的地图是{shoes 2, milk 3}。我尝试了zipmap两个向量并且只{shoes 1 milk 3}显示。没有循环和迭代,还有另一种方法吗?
我们有一个带有一些键和布尔值的原子:
(def btns (reagent.core/atom {:a true :b true :c true}))
Run Code Online (Sandbox Code Playgroud)
我需要更改所有键的状态,如下所示: {:a false :b false :c false}
我试过这个,但不是很好的解决方案,并且不起作用:
(for [btn @btns]
(swap! btns assoc (key btn) false))
Run Code Online (Sandbox Code Playgroud) 我的尝试:
(defn inc-by-f [v]
map #(+ (first v) %) v)
Run Code Online (Sandbox Code Playgroud)
(最初的问题很愚蠢;我错过了括号。我还在留下这个问题,以便我学习一些新的方法来处理它。)
(defn inc-by-f [v]
(map #(+ (first v) %) v))
Run Code Online (Sandbox Code Playgroud)
还有什么其他很酷的“Clojure”方法可以达到预期的效果?
(def mine '(a b c))
(def yours '(a b))
(remove yours mine)
; should return (c)
Run Code Online (Sandbox Code Playgroud)
有人建议我在另一个线程中使用 remove ,但它不起作用。任何人都可以建议?
正在看clojure第 9 章底部的练习,寻找勇敢和真实的人 (特别是搜索多个引擎并返回每个引擎的第一次命中的最后一个)
我用 slurp 部分嘲笑实际搜索是这样的:
(defn search-for
[query engine]
(Thread/sleep 2000)
(format "https://www.%s.com/search?q%%3D%s", engine query))
Run Code Online (Sandbox Code Playgroud)
并实现了这样的行为:
(defn get-first-hit-from-each
[query engines]
(let [futs (map (fn [engine]
(future (search-for query engine))) engines)]
(doall futs)
(map deref futs)))
Run Code Online (Sandbox Code Playgroud)
(我知道这里的返回是一个列表,练习要求一个向量,但可以into为此做一个......)
但是当我在 REPL 中运行它时
(time (get-first-hit-from-each "gray+cat" '("google" "bing")))
Run Code Online (Sandbox Code Playgroud)
添加后似乎需要 2 秒doall(因为 map 返回一个惰性 seq,我认为除非我使用 seq,否则任何期货都不会启动,(last futs)似乎也有效)但是当我time在 REPL 中使用宏时,它会报告即使需要 2 秒,也几乎不消耗时间:
(time (get-first-hit-from-each "gray+cat" '("google" "bing")))
"Elapsed time: 0.189609 msecs"
("https://www.google.com/search?q%3Dgray+cat" "https://www.bing.com/search?q%3Dgray+cat") …Run Code Online (Sandbox Code Playgroud) 如果我有一张看起来像的地图:
{:a 1 :b 2 :c 3}
Run Code Online (Sandbox Code Playgroud)
如何将其转换为向量,例如:
[:a 1 :b 2 :c 3]
Run Code Online (Sandbox Code Playgroud)