Ray*_*yne 6 coding-style newline clojure
这是一些示例代码:
(deftype Deck52 [suits] :as this
DeckOfCards
(check-empty []
(Deck52. (apply hash-map
(apply concat (remove (-> nil?)
(for [[key val] suits]
(if (empty? val) nil [key val])))))))
(remove-card [suit card]
(assoc suits suit (remove #(= card %) (suit suits))))
(get-card [suit]
(let [suitd (suit suits)]
[(first suitd) (check-empty (Deck52. (assoc suits suit (rest suitd))))]))
(random-card []
(let [suitn (+ 1 (rand-int 4))]
(cond (= suitn 1) (get-card this :hearts)
(= suitn 2) (get-card this :diamonds)
(= suitn 3) (get-card this :clubs)
(= suitn 4) (get-card this :spades)))))
Run Code Online (Sandbox Code Playgroud)
如果它更容易阅读,我也在这里发布了这个代码:http://gist.github.com/307425(不会去任何地方).
这里的主要例子是check-empty.我很难知道应该在哪里,不应该回来,我仍然不知道我是否做得对.它有可能走出屏幕的右侧,但这就是clojure-mode缩进的方式,我认为它应该是这样的.
所以,问题是,什么时候在Clojure/Lisp代码中添加换行符?我是在做'它'吗'?
注意:我不能保证我发布的代码有效.我一直在做一些实验,有些事情可能很简单,如果没有破坏的话.
你打破线条的方式很正常.我会做一些微小的改变.
->和->>帮手,condp,何时,......在这里我将如何解决这个问题.(免责声明:我的风格.你的风格可能会有所不同.YMMV!)
(deftype Deck52 [suits] :as this
DeckOfCards
(check-empty
[]
(->> (for [[key val] suits]
(when-not (empty? val)
[key val]))
(remove nil?)
(apply concat)
(apply hash-map)
Deck52.))
(remove-card
[suit card]
(assoc suits suit (remove #(= card %) (suit suits))))
(get-card
[suit]
(let [suitd (suit suits)]
[(first suitd)
(->> (rest suitd)
(assoc suits suit)
Deck52.
check-empty)]))
(random-card
[]
(let [suitn (+ 1 (rand-int 4))]
(condp = suitn
1 (get-card this :hearts)
2 (get-card this :diamonds)
3 (get-card this :clubs)
4 (get-card this :spades)))))
Run Code Online (Sandbox Code Playgroud)
虽然以下不是您问题的一部分,但我无法抗拒:
(deftype Deck52 [suits] :as this
DeckOfCards
(check-empty
[]
(->> suits (remove (comp nil? seq val)) (into {}) Deck52.))
(remove-card
[suit card]
(update-in suits [suit] #(remove %2 %1) #(= card %)))
(get-card
[suit]
(let [suitd (get suits suit)]
[(first suitd)
(->> (rest suitd) (assoc suits suit) Deck52. check-empty)]))
(random-card
[]
(case (rand-int 4)
0 (get-card this :hearts)
1 (get-card this :diamonds)
2 (get-card this :clubs)
3 (get-card this :spades))))
Run Code Online (Sandbox Code Playgroud)