我不明白为什么会出现以下规范错误。
谁能告诉我为什么?
(ns spec-test
(:require [clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as stest]))
;;; spec definitions
(s/def :msds.fontspec/tag #{:fontspec})
(s/def ::id string?)
(s/def ::size int?)
(s/def ::family string?)
(s/def ::color string?)
(s/def :msds.fontspec/attrs
(s/keys :req-un [::id ::size ::family ::color]))
(s/def :msds.fontspec/content (s/cat))
(s/def ::fontspec
(s/keys :req-un [:msds.fontspec/tag :msds.fontspec/attrs :msds.fontspec/content]))
(s/def ::fontspecs (s/* ::fontspec))
;;; spec application
(defn transform-fontspecs
[fontspecs]
(->> fontspecs
(map (fn [{:keys [attrs] :as fontspec}]
[(get attrs :id) (dissoc attrs :id)] ))
(into {}) ))
(s/fdef transform-fontspecs
:args (s/cat :fontspec …Run Code Online (Sandbox Code Playgroud) 我是 EDN 的新手,正在浏览 EDN 规范 - https://github.com/edn-format/edn
'$ % &' 之类的 EDN 符号有什么用?在 Clojure 中阅读 EDN 时如何使用它们?
我已经搜索了该网站,但没有找到我关于子字符串替换的特定问题的合适答案。我知道如何通过替换子字符串/正则表达式clojure.string/replace但不确定在这种情况下使用它。
让我们说。我首先有一些字符串:
(def test-str "I am a test string. :name to replace, :age to replace. And there are strange symbols in this string. And here are the keyword shall not modify, such as :not-interested.")
(def another-test-str ":nothing :to :replace")
Run Code Online (Sandbox Code Playgroud)
我有一个翻译表:
(def translation-table {:name "Alice"
:age 19})
Run Code Online (Sandbox Code Playgroud)
我想,以取代:name在test-str与“爱丽丝”,:age与19,但不希望更换:not-interested。表很长,不同的字符串(要替换)包含不同的关键字。
鉴于翻译表,那么进行子串替换的可能系统方法是什么?即,我想要一个函数replace-with-translation:
(replace-with-translation test-str translation-table)
=> "I am a test string. Alice to replace, 19 to replace. And there are strange symbols …Run Code Online (Sandbox Code Playgroud) 他们是 Clojure 中从 int 到 int 作为字符的一种方式,例如 1 到 \1 ?
我有一个字符串s,我想解析出与数字匹配的数字n(n 将始终为 0 到 9)
例如
(let [n 1]
(filter #(= ??? %) "123123123"))
Run Code Online (Sandbox Code Playgroud)
在哪里 ???将 n 作为 \n,例如将返回“111”
或者也许有更好的方法将字符串过滤为仅单个数字的实例?
保证某些未知值在序列中的惯用方法是什么,该值可能已经或不可能已经在序列中?(into [] val)如果 val 尚未在 seq 中,则失败(conj [] val)始终有效,但可能会嵌套,即[[val...]](哦,并且未嵌套,如果已经在 seq 中)* 是简单的(flatten (conj吗?
(defn foo [[a] b]
(println a b))
(foo "one" "two") ; => o two
(foo 1 2) ; => Execution error (UnsupportedOperationException) at user/foo (REPL:1).
; nth not supported on this type: Long
Run Code Online (Sandbox Code Playgroud)
周围的第二对括号在a做什么?
或者,我遇到的一个真实世界的例子:
(parser/add-tag! :i18n
(fn [[k] context]
(->> k (keyword) (translate (or (:i18n/locale context) :en)))))
Run Code Online (Sandbox Code Playgroud) 如果我创建一个新项目
lein new quil foo
Run Code Online (Sandbox Code Playgroud)
然后将 src/foo/core.clj 减少到
(ns foo.core
(:require [quil.core :as q]
[quil.middleware :as m]))
(defn draw [state]
(if (< (rand) 0.5)
(q/line 0 0 500 500)
(q/line 0 500 500 0)))
(q/defsketch foo
:title "Try this at home"
:size [500 500]
:draw draw
:features [:keep-on-top]
:middleware [m/fun-mode])
Run Code Online (Sandbox Code Playgroud)
在评估程序lein repl与(use 'foo.core),无论是画线(即我得到一个大的X)。(if (< (rand) 0.5) true false)按预期工作,那么我错过了什么?
user-> (last '(First (Second (Last))))
-> (Second (Last))
Run Code Online (Sandbox Code Playgroud)
我通常会假设这个 ^ 只会返回(最后)。为什么是这样?有没有办法只返回 (Last) 或 Last。
还有什么对于:
(defn function
[input] ;assume input = '(First (Second (Last)))
(last input)
)
Run Code Online (Sandbox Code Playgroud) 我有通过 clojure 创建的文件
frontendapp.jar
backendapp.jar
Run Code Online (Sandbox Code Playgroud)
并取一个环境变量, APP_PORT
我想了解的是如何将变量传递给 jar 文件,以便它们与变量 APP_PORT 一起运行?
我的假设是这样的
java -jar frontenapp.jar APP_PORT=8080
Run Code Online (Sandbox Code Playgroud)
我的假设正确吗?
如果问题不好,我很抱歉,这是我第一次部署 clojure 应用程序
java clojure clojure-contrib clojure-java-interop clojurescript
我正在阅读一些源代码并locking在 Clojure 中遇到了用法。这让我想到了 atom 版本。那么2个代码片段之间有什么区别,我认为它们做同样的事情?
(def lock (Object.))
(locking lock
...some operation)
Run Code Online (Sandbox Code Playgroud)
(def state (atom true))
(when @state
(reset! state false)
...some operation
(reset! state true))
Run Code Online (Sandbox Code Playgroud)