Project

General

Profile

Statistics
| Revision:

root / trunk / scripts / codemirror / mode / clojure / index.html @ 216

History | View | Annotate | Download (1.93 KB)

1
<!doctype html>
2
<html>
3
  <head>
4
    <title>CodeMirror: Clojure mode</title>
5
    <link rel="stylesheet" href="../../lib/codemirror.css">
6
    <script src="../../lib/codemirror.js"></script>
7
    <script src="clojure.js"></script>
8
    <style>.CodeMirror {background: #f8f8f8;}</style>
9
    <link rel="stylesheet" href="../../doc/docs.css">
10
  </head>
11
  <body>
12
    <h1>CodeMirror: Clojure mode</h1>
13
    <form><textarea id="code" name="code">
14
; Conway's Game of Life, based on the work of:
15
;; Laurent Petit https://gist.github.com/1200343
16
;; Christophe Grand http://clj-me.cgrand.net/2011/08/19/conways-game-of-life
17

    
18
(ns ^{:doc "Conway's Game of Life."}
19
 game-of-life)
20

    
21
;; Core game of life's algorithm functions
22

    
23
(defn neighbours 
24
  "Given a cell's coordinates, returns the coordinates of its neighbours."
25
  [[x y]]
26
  (for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])]
27
    [(+ dx x) (+ dy y)]))
28

    
29
(defn step 
30
  "Given a set of living cells, computes the new set of living cells."
31
  [cells]
32
  (set (for [[cell n] (frequencies (mapcat neighbours cells))
33
             :when (or (= n 3) (and (= n 2) (cells cell)))]
34
         cell)))
35

    
36
;; Utility methods for displaying game on a text terminal
37

    
38
(defn print-board 
39
  "Prints a board on *out*, representing a step in the game."
40
  [board w h]
41
  (doseq [x (range (inc w)) y (range (inc h))]
42
    (if (= y 0) (print "\n")) 
43
    (print (if (board [x y]) "[X]" " . "))))
44

    
45
(defn display-grids 
46
  "Prints a squence of boards on *out*, representing several steps."
47
  [grids w h]
48
  (doseq [board grids]
49
    (print-board board w h)
50
    (print "\n")))
51

    
52
;; Launches an example board
53

    
54
(def 
55
  ^{:doc "board represents the initial set of living cells"}
56
   board #{[2 1] [2 2] [2 3]})
57

    
58
(display-grids (take 3 (iterate step board)) 5 5) </textarea></form>
59
    <script>
60
      var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
61
    </script>
62

    
63
    <p><strong>MIME types defined:</strong> <code>text/x-clojure</code>.</p>
64

    
65
  </body>
66
</html>