├── README.md └── clojure-lt.clj /README.md: -------------------------------------------------------------------------------- 1 | # Clojure 101: Light Table Tutorial 2 | An interactive Clojure tutorial intended for folks who use Light Table. 3 | 4 | Light Table can be found [here](http://lighttable.com/). 5 | Based on teaching materials used during [ClojureBridge tutorials](http://www.clojurebridge.org/). 6 | 7 | 8 | # Brainstorming 9 | - The basics: arithmetic, arrays / lists, vectors, functions, if/else statements, Booleans, strings, etc. 10 | - Pick a number, anagrams, animations? 11 | - Reading files, pulling columnar data from a single file / series of files? 12 | - Sorting algorithms (insertion, merge, quick, selection...) 13 | - Also include how to make a basic Quil app. 14 | 15 | # Out of Scope 16 | - cljs, because [David Nolen is amazing](https://github.com/swannodette/lt-cljs-tutorial). 17 | - integration with (most) API's 18 | -------------------------------------------------------------------------------- /clojure-lt.clj: -------------------------------------------------------------------------------- 1 | ;; arrays / lists 2 | 3 | (def my-list (list 1 2 3 4 5)) 4 | 5 | my-list 6 | 7 | (first my-list) ; returns the first element of the array 8 | (nth my-list 3) ; returns the 0-index element of the array 9 | 10 | (conj my-list 100) ; adds 100 to my-list 11 | 12 | my-list ; but note: my-list does not change 13 | 14 | ;; in order to add 100, you must define a new array 15 | (def my-new-list (conj my-list 100)) 16 | my-new-list 17 | 18 | ;; same story for vectors 19 | (def my-vector [1 2 3 4 5 6]) 20 | (conj my-vector 300) 21 | 22 | ;; guessing a number 23 | 24 | (def target (inc (rand-int 10)) 25 | 26 | (loop [n 0] 27 | (print ln "Guess a number between 1 and 15:") 28 | (let [guess (read)] 29 | (if (= guess target) 30 | (printf "Correct! \n" n) 31 | (do 32 | (println "Nope. Try again!") 33 | (recur (inc n)))))) 34 | 35 | ;; swapping 36 | 37 | ;; define a and b 38 | 39 | (defn swap [pair] (reverse pair)) 40 | (defn swap [[a b]] '(b a)) 41 | (defn swap [[a b]] [b a]) 42 | 43 | ;; anagrams 44 | (require '[clojure.java.io :as io]) 45 | 46 | (def groups 47 | (with-open [r (io/reader wordfile)] 48 | (group-by sort (line-seq r)))) 49 | 50 | (let [wordlists (sort-by (comp - count) (vals groups)) 51 | maxlength (count (first wordlists))] 52 | (doseq [wordlist (take-while #(= (count %) maxlength) wordlists)] 53 | (println wordlist)) 54 | --------------------------------------------------------------------------------