私はあなたがおそらく生産で実行し、これを持つべきではないというジョシュに同意するが、私は実際に私は私のデバッグにこれをコピーすると思う(REPLで利便性としてそれを持つに害が表示されません-replキッチンシンクライブラリ)。
(それらは通常必要としていないが)、私はマクロを書く楽しむので、私は実装をホイップ。 let
のようなバインディング形式を受け入れます。約
(私はこのスペック-最初に書いていますが、Clojureの< 1.9.0-alpha17にしている場合は、あなただけの仕様のものを削除することができ、それは同じように動作します。)
(ns macro-fun
(:require
[clojure.spec.alpha :as s]
[clojure.core.specs.alpha :as core-specs]))
(s/fdef syms-in-binding
:args (s/cat :b ::core-specs/binding-form)
:ret (s/coll-of simple-symbol? :kind vector?))
(defn syms-in-binding
"Returns a vector of all symbols in a binding form."
[b]
(letfn [(step [acc coll]
(reduce (fn [acc x]
(cond (coll? x) (step acc x)
(symbol? x) (conj acc x)
:else acc))
acc, coll))]
(if (symbol? b) [b] (step [] b))))
(s/fdef defs
:args (s/cat :binding ::core-specs/binding-form, :body any?))
(defmacro defs
"Like def, but can take a binding form instead of a symbol to
destructure the results of the body.
Doesn't support docstrings or other metadata."
[binding body]
`(let [~binding ~body]
[email protected](for [sym (syms-in-binding binding)]
`(def ~sym ~sym))))
;; Usage
(defs {:keys [foo bar]} {:foo 42 :bar 36})
foo ;=> 42
bar ;=> 36
(defs [a b [c d]] [1 2 [3 4]])
[a b c d] ;=> [1 2 3 4]
(defs baz 42)
baz ;=> 42
あなたのREPL主導の開発コメント:
私はIpythonに関する経験はありませんが、私はREPLワークフローについて簡単に説明し、Ipythonとの比較/対比についてコメントすることができます。
私は、コマンドを入力し、応答を待って、ターミナルのような私のREPLを使用することはありません。私のエディタは、s式の終わりにカーソルを置いてreplに送信し、カーソルの後に結果を「印刷」する(emacsだが、clojureエディタはすべきである)。
私は通常ちょうど何を入力し、それを評価し、私が作業を開始したファイルにcomment
ブロックを持っています。そして、私が結果に幸せになれば、それを「repl-area」から「real-code」に引き出します。
(ns stuff.core)
;; Real code is here.
;; I make sure that this part always basically works,
;; ie. doesn't blow up when I evaluate the whole file
(defn foo-fn [x]
,,,)
(comment
;; Random experiments.
;; I usually delete this when I'm done with a coding session,
;; but I copy some forms into tests.
;; Sometimes I leave it for posterity though,
;; if I think it explains something well.
(def some-data [,,,])
;; Trying out foo-fn, maybe copy this into a test when I'm done.
(foo-fn some-data)
;; Half-finished other stuff.
(defn bar-fn [x] ,,,)
(keys 42) ; I wonder what happens if...
)
あなたはclojure core source codeでこの例を見ることができます。
re:私のREPLコメント。ライブラリやバックエンドの開発者よりも "データ科学者"のワークフローを考えてみてください。私はclojureが好きで、それがより広く採用されることを願っています。 Emacs/cider replは良い、非常に良いですが、それはIDE固有のものです(proto-replなど) Emacs/ciderは、その言語に来る人には登り過ぎる壁があります。 1)複数の行の編集や履歴なし、2)自動終了のブレースなし、3)pprintの使用量の増加、4)ファイル名のタブ補完、5) pwd、ls、editのようなシェルの基本を行う能力(宇宙の自由) –