Skip to content

Commit 75c14f6

Browse files
finalfantasiamarijnh
authored andcommitted
[clojure mode] Modernize the style of the example code.
[The Clojure Style Guide](https://github.com/bbatsov/clojure-style-guide).
1 parent 8b0e326 commit 75c14f6

File tree

1 file changed

+41
-44
lines changed

1 file changed

+41
-44
lines changed

mode/clojure/index.html

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,63 +27,60 @@
2727
<article>
2828
<h2>Clojure mode</h2>
2929
<form><textarea id="code" name="code">
30-
; Conway's Game of Life, based on the work of:
31-
;; Laurent Petit https://gist.github.com/1200343
32-
;; Christophe Grand http://clj-me.cgrand.net/2011/08/19/conways-game-of-life
30+
(ns game-of-life
31+
"Conway's Game of Life, based on the work of
32+
Christophe Grand (http://clj-me.cgrand.net/2011/08/19/conways-game-of-life)
33+
and Laurent Petit (https://gist.github.com/1200343).")
3334

34-
(ns ^{:doc "Conway's Game of Life."}
35-
game-of-life)
35+
;;; Core game of life's algorithm functions
3636

37-
;; Core game of life's algorithm functions
38-
39-
(defn neighbours
40-
"Given a cell's coordinates, returns the coordinates of its neighbours."
37+
(defn neighbors
38+
"Given a cell's coordinates `[x y]`, returns the coordinates of its
39+
neighbors."
4140
[[x y]]
42-
(for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])]
41+
(for [dx [-1 0 1]
42+
dy (if (zero? dx)
43+
[-1 1]
44+
[-1 0 1])]
4345
[(+ dx x) (+ dy y)]))
4446

4547
(defn step
46-
"Given a set of living cells, computes the new set of living cells."
48+
"Given a set of living `cells`, computes the new set of living cells."
4749
[cells]
48-
(set (for [[cell n] (frequencies (mapcat neighbours cells))
49-
:when (or (= n 3) (and (= n 2) (cells cell)))]
50+
(set (for [[cell n] (frequencies (mapcat neighbors cells))
51+
:when (or (= n 3)
52+
(and (= n 2)
53+
(cells cell)))]
5054
cell)))
5155

52-
;; Utility methods for displaying game on a text terminal
53-
54-
(defn print-board
55-
"Prints a board on *out*, representing a step in the game."
56-
[board w h]
57-
(doseq [x (range (inc w)) y (range (inc h))]
58-
(if (= y 0) (print "\n"))
59-
(print (if (board [x y]) "[X]" " . "))))
60-
61-
(defn display-grids
62-
"Prints a squence of boards on *out*, representing several steps."
56+
;;; Utility methods for displaying game on a text terminal
57+
58+
(defn print-grid
59+
"Prints a `grid` of `w` columns and `h` rows, on *out*, representing a
60+
step in the game."
61+
[grid w h]
62+
(doseq [x (range (inc w))
63+
y (range (inc h))]
64+
(when (= y 0) (println))
65+
(print (if (grid [x y])
66+
"[X]"
67+
" . "))))
68+
69+
(defn print-grids
70+
"Prints a sequence of `grids` of `w` columns and `h` rows on *out*,
71+
representing several steps."
6372
[grids w h]
64-
(doseq [board grids]
65-
(print-board board w h)
66-
(print "\n")))
67-
68-
;; Launches an example board
69-
70-
(def
71-
^{:doc "board represents the initial set of living cells"}
72-
board #{[2 1] [2 2] [2 3]})
73-
74-
(display-grids (take 3 (iterate step board)) 5 5)
73+
(doseq [grid grids]
74+
(print-grid grid w h)
75+
(println)))
7576

76-
;; Let's play with characters
77-
(println \1 \a \# \\
78-
\" \( \newline
79-
\} \" \space
80-
\tab \return \backspace
81-
\u1000 \uAaAa \u9F9F)
77+
;;; Launches an example grid
8278

83-
;; Let's play with numbers
84-
(+ 1 -1 1/2 -1/2 -0.5 0.5)
79+
(def grid
80+
"`grid` represents the initial set of living cells"
81+
#{[2 1] [2 2] [2 3]})
8582

86-
</textarea></form>
83+
(print-grids (take 3 (iterate step grid)) 5 5)</textarea></form>
8784
<script>
8885
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
8986
autoCloseBrackets: true,

0 commit comments

Comments
 (0)