openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

index.html (2550B)


      1 <!doctype html>
      2 
      3 <title>CodeMirror: Clojure mode</title>
      4 <meta charset="utf-8"/>
      5 <link rel=stylesheet href="../../doc/docs.css">
      6 
      7 <link rel="stylesheet" href="../../lib/codemirror.css">
      8 <script src="../../lib/codemirror.js"></script>
      9 <script src="clojure.js"></script>
     10 <style>.CodeMirror {background: #f8f8f8;}</style>
     11 <div id=nav>
     12   <a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a>
     13 
     14   <ul>
     15     <li><a href="../../index.html">Home</a>
     16     <li><a href="../../doc/manual.html">Manual</a>
     17     <li><a href="https://github.com/codemirror/codemirror">Code</a>
     18   </ul>
     19   <ul>
     20     <li><a href="../index.html">Language modes</a>
     21     <li><a class=active href="#">Clojure</a>
     22   </ul>
     23 </div>
     24 
     25 <article>
     26 <h2>Clojure mode</h2>
     27 <form><textarea id="code" name="code">
     28 ; Conway's Game of Life, based on the work of:
     29 ;; Laurent Petit https://gist.github.com/1200343
     30 ;; Christophe Grand http://clj-me.cgrand.net/2011/08/19/conways-game-of-life
     31 
     32 (ns ^{:doc "Conway's Game of Life."}
     33  game-of-life)
     34 
     35 ;; Core game of life's algorithm functions
     36 
     37 (defn neighbours
     38   "Given a cell's coordinates, returns the coordinates of its neighbours."
     39   [[x y]]
     40   (for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])]
     41     [(+ dx x) (+ dy y)]))
     42 
     43 (defn step
     44   "Given a set of living cells, computes the new set of living cells."
     45   [cells]
     46   (set (for [[cell n] (frequencies (mapcat neighbours cells))
     47              :when (or (= n 3) (and (= n 2) (cells cell)))]
     48          cell)))
     49 
     50 ;; Utility methods for displaying game on a text terminal
     51 
     52 (defn print-board
     53   "Prints a board on *out*, representing a step in the game."
     54   [board w h]
     55   (doseq [x (range (inc w)) y (range (inc h))]
     56     (if (= y 0) (print "\n"))
     57     (print (if (board [x y]) "[X]" " . "))))
     58 
     59 (defn display-grids
     60   "Prints a squence of boards on *out*, representing several steps."
     61   [grids w h]
     62   (doseq [board grids]
     63     (print-board board w h)
     64     (print "\n")))
     65 
     66 ;; Launches an example board
     67 
     68 (def
     69   ^{:doc "board represents the initial set of living cells"}
     70    board #{[2 1] [2 2] [2 3]})
     71 
     72 (display-grids (take 3 (iterate step board)) 5 5)
     73 
     74 ;; Let's play with characters
     75 (println \1 \a \# \\
     76          \" \( \newline
     77          \} \" \space
     78          \tab \return \backspace
     79          \u1000 \uAaAa \u9F9F)
     80 
     81 ;; Let's play with numbers
     82 (+ 1 -1 1/2 -1/2 -0.5 0.5)
     83 
     84 </textarea></form>
     85     <script>
     86       var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
     87     </script>
     88 
     89     <p><strong>MIME types defined:</strong> <code>text/x-clojure</code>.</p>
     90 
     91   </article>