| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  | <!doctype html> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <title>CodeMirror: Clojure mode</title> | 
					
						
							|  |  |  | <meta charset="utf-8"/> | 
					
						
							|  |  |  | <link rel=stylesheet href="../../doc/docs.css"> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <link rel="stylesheet" href="../../lib/codemirror.css"> | 
					
						
							|  |  |  | <script src="../../lib/codemirror.js"></script> | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  | <script src="../../addon/edit/closebrackets.js"></script> | 
					
						
							|  |  |  | <script src="../../addon/edit/matchbrackets.js"></script> | 
					
						
							| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  | <script src="clojure.js"></script> | 
					
						
							|  |  |  | <style>.CodeMirror {background: #f8f8f8;}</style> | 
					
						
							|  |  |  | <div id=nav> | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  |   <a href="https://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a> | 
					
						
							| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   <ul> | 
					
						
							|  |  |  |     <li><a href="../../index.html">Home</a> | 
					
						
							|  |  |  |     <li><a href="../../doc/manual.html">Manual</a> | 
					
						
							|  |  |  |     <li><a href="https://github.com/codemirror/codemirror">Code</a> | 
					
						
							|  |  |  |   </ul> | 
					
						
							|  |  |  |   <ul> | 
					
						
							|  |  |  |     <li><a href="../index.html">Language modes</a> | 
					
						
							|  |  |  |     <li><a class=active href="#">Clojure</a> | 
					
						
							|  |  |  |   </ul> | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <article> | 
					
						
							|  |  |  | <h2>Clojure mode</h2> | 
					
						
							|  |  |  | <form><textarea id="code" name="code"> | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  | (ns game-of-life | 
					
						
							|  |  |  |   "Conway's Game of Life, based on the work of | 
					
						
							|  |  |  |   Christophe Grand (http://clj-me.cgrand.net/2011/08/19/conways-game-of-life) | 
					
						
							|  |  |  |   and Laurent Petit (https://gist.github.com/1200343).") | 
					
						
							| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  | ;;; Core game of life's algorithm functions | 
					
						
							| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  | (defn neighbors | 
					
						
							|  |  |  |   "Given a cell's coordinates `[x y]`, returns the coordinates of its | 
					
						
							|  |  |  |   neighbors." | 
					
						
							| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  |   [[x y]] | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  |   (for [dx [-1 0 1] | 
					
						
							|  |  |  |         dy (if (zero? dx) | 
					
						
							|  |  |  |              [-1 1] | 
					
						
							|  |  |  |              [-1 0 1])] | 
					
						
							| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  |     [(+ dx x) (+ dy y)])) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | (defn step | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  |   "Given a set of living `cells`, computes the new set of living cells." | 
					
						
							| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  |   [cells] | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  |   (set (for [[cell n] (frequencies (mapcat neighbors cells)) | 
					
						
							|  |  |  |              :when (or (= n 3) | 
					
						
							|  |  |  |                        (and (= n 2) | 
					
						
							|  |  |  |                             (cells cell)))] | 
					
						
							| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  |          cell))) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  | ;;; Utility methods for displaying game on a text terminal | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | (defn print-grid | 
					
						
							|  |  |  |   "Prints a `grid` of `w` columns and `h` rows, on *out*, representing a | 
					
						
							|  |  |  |   step in the game." | 
					
						
							|  |  |  |   [grid w h] | 
					
						
							|  |  |  |   (doseq [x (range (inc w)) | 
					
						
							|  |  |  |           y (range (inc h))] | 
					
						
							|  |  |  |     (when (= y 0) (println)) | 
					
						
							|  |  |  |     (print (if (grid [x y]) | 
					
						
							|  |  |  |              "[X]" | 
					
						
							|  |  |  |              " . ")))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | (defn print-grids | 
					
						
							|  |  |  |   "Prints a sequence of `grids` of `w` columns and `h` rows on *out*, | 
					
						
							|  |  |  |   representing several steps." | 
					
						
							| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  |   [grids w h] | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  |   (doseq [grid grids] | 
					
						
							|  |  |  |     (print-grid grid w h) | 
					
						
							|  |  |  |     (println))) | 
					
						
							| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  | ;;; Launches an example grid | 
					
						
							| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  | (def grid | 
					
						
							|  |  |  |   "`grid` represents the initial set of living cells" | 
					
						
							|  |  |  |   #{[2 1] [2 2] [2 3]}) | 
					
						
							| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  | (print-grids (take 3 (iterate step grid)) 5 5)</textarea></form> | 
					
						
							| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  |     <script> | 
					
						
							| 
									
										
										
										
											2018-10-07 12:02:07 +02:00
										 |  |  |       var editor = CodeMirror.fromTextArea(document.getElementById("code"), { | 
					
						
							|  |  |  |           autoCloseBrackets: true, | 
					
						
							|  |  |  |           lineNumbers: true, | 
					
						
							|  |  |  |           matchBrackets: true, | 
					
						
							|  |  |  |           mode: 'text/x-clojure' | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-01-21 10:33:32 -05:00
										 |  |  |     </script> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     <p><strong>MIME types defined:</strong> <code>text/x-clojure</code>.</p> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   </article> |