Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove trailing whitespace which is in almost every file. #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions src/examples/atom_snake.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
; Inspired by the snakes the have gone before:
; Abhishek Reddy's snake: http://www.plt1.com/1070/even-smaller-snake/
; Mark Volkmann's snake: http://www.ociweb.com/mark/programming/ClojureSnake.html
; Mark Volkmann's snake: http://www.ociweb.com/mark/programming/ClojureSnake.html

(ns examples.atom-snake
(:import (java.awt Color Dimension)
(javax.swing JPanel JFrame Timer JOptionPane)
(java.awt.event ActionListener KeyListener))
(:use examples.import-static))
(:require [examples.import-static :refer :all])
(:import [java.awt Color Dimension]
[java.awt.event ActionListener KeyListener]
[javax.swing JFrame JOptionPane JPanel Timer]))

(import-static java.awt.event.KeyEvent VK_LEFT VK_RIGHT VK_UP VK_DOWN)

; ----------------------------------------------------------
Expand All @@ -17,34 +18,34 @@
(def point-size 10)
(def turn-millis 75)
(def win-length 5)
(def dirs { VK_LEFT [-1 0]
(def dirs { VK_LEFT [-1 0]
VK_RIGHT [ 1 0]
VK_UP [ 0 -1]
VK_UP [ 0 -1]
VK_DOWN [ 0 1]})

(defn add-points [& pts]
(defn add-points [& pts]
(vec (apply map + pts)))

(defn point-to-screen-rect [pt]
(map #(* point-size %)
(defn point-to-screen-rect [pt]
(map #(* point-size %)
[(pt 0) (pt 1) 1 1]))

(defn create-apple []
(defn create-apple []
{:location [(rand-int width) (rand-int height)]
:color (Color. 210 50 90)
:type :apple})
:type :apple})

(defn create-snake []
{:body (list [1 1])
{:body (list [1 1])
:dir [1 0]
:type :snake
:color (Color. 15 160 70)})

(defn move [{:keys [body dir] :as snake} & grow]
(assoc snake :body (cons (add-points (first body) dir)
(assoc snake :body (cons (add-points (first body) dir)
(if grow body (butlast body)))))

(defn turn [snake newdir]
(defn turn [snake newdir]
(if newdir (assoc snake :dir newdir) snake))

(defn win? [{body :body}]
Expand Down Expand Up @@ -74,28 +75,28 @@
; ----------------------------------------------------------
; gui
; ----------------------------------------------------------
(defn fill-point [g pt color]
(defn fill-point [g pt color]
(let [[x y width height] (point-to-screen-rect pt)]
(.setColor g color)
(.setColor g color)
(.fillRect g x y width height)))

(defmulti paint (fn [g object & _] (:type object)))

(defmethod paint :apple [g {:keys [location color]}]
(defmethod paint :apple [g {:keys [location color]}]
(fill-point g location color))

(defmethod paint :snake [g {:keys [body color]}]
(defmethod paint :snake [g {:keys [body color]}]
(doseq [point body]
(fill-point g point color)))

(defn game-panel [frame game]
(proxy [JPanel ActionListener KeyListener] []
(paintComponent [g]
(paintComponent [g]
(proxy-super paintComponent g)
(paint g (@game :snake))
(paint g (@game :apple)))
; START: swap!
(actionPerformed [e]
(actionPerformed [e]
(swap! game update-positions)
(when (lose? (@game :snake))
(swap! game reset-game)
Expand All @@ -105,26 +106,25 @@
(swap! game reset-game)
(JOptionPane/showMessageDialog frame "You win!"))
(.repaint this))
(keyPressed [e]
(keyPressed [e]
(swap! game update-direction (dirs (.getKeyCode e))))
(getPreferredSize []
(Dimension. (* (inc width) point-size)
(getPreferredSize []
(Dimension. (* (inc width) point-size)
(* (inc height) point-size)))
(keyReleased [e])
(keyTyped [e])))

(defn game []
(defn game []
(let [game (atom (reset-game {}))
frame (JFrame. "Snake")
panel (game-panel frame game)
timer (Timer. turn-millis panel)]
(doto panel
(doto panel
(.setFocusable true)
(.addKeyListener panel))
(doto frame
(doto frame
(.add panel)
(.pack)
(.setVisible true))
(.start timer)
[game, timer]))

(.start timer)
[game, timer]))
4 changes: 2 additions & 2 deletions src/examples/chat.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

; START: messages
(def messages (ref ()))
; END: messages
; END: messages

; START: validate-message-list
(def validate-message-list
Expand All @@ -20,7 +20,7 @@
(defn naive-add-message [msg]
(dosync (ref-set messages (cons msg @messages))))
; END: naive-add-message

; START: add-message
(defn add-message [msg]
(dosync (alter messages conj msg)))
Expand Down
8 changes: 3 additions & 5 deletions src/examples/concurrency.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,22 @@

; START: demo-memoize
(defn demo-memoize []
(time
(time
(dorun
(binding [slow-double (memoize slow-double)]
(calls-slow-double)))))
; END: demo-memoize
; END: demo-memoize

; START: backup-agent
(def backup-agent (agent "output/messages-backup.clj"))
; END: backup-agent

; START: add-message-with-backup
(defn add-message-with-backup [msg]
(dosync
(dosync
(let [snapshot (commute messages conj msg)]
(send-off backup-agent (fn [filename]
(spit filename snapshot)
filename))
snapshot)))
; END: add-message-with-backup


19 changes: 6 additions & 13 deletions src/examples/error_kit.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,34 @@
(next (re-matches #"(\d+-\d+-\d+) (\d+:\d+:\d+) (\w+) (.*)" entry))
(raise malformed-log-entry entry)))

(def bad-log
(def bad-log
["2008-10-05 12:14:00 WARN Some warning message here..."
"<<this is not a log message>>"
"2008-10-05 12:14:00 INFO End of the current log..."])

(def good-log
(def good-log
["2008-10-05 12:14:00 WARN Some warning message here..."
"2008-10-05 12:14:00 INFO End of the current log..."])

; Example 1. Continue calculation with replacement value
(defn parse-or-nil [logseq]
(with-handler
(with-handler
(vec (map parse-log-entry logseq))
(handle malformed-log-entry [msg]
(continue-with nil))))

; Example 2. Continue calculation with logging & replacement value
(defn parse-or-warn [logseq]
(with-handler
(with-handler
(vec (map parse-log-entry logseq))
(handle malformed-log-entry [msg]
(continue-with (println "****warning****: invalid log: " msg)))))

; Example 3. Caller can choose from a fixed set of contiue strategies.
(defn parse-or-continue [logseq]
(let [parse-log-entry
(let [parse-log-entry
(fn [entry]
(with-handler (parse-log-entry entry)
(bind-continue skip [msg] nil)
(bind-continue log [msg] (println "****warning****: invalid log: " msg))))]
(vec (map parse-log-entry logseq))))







12 changes: 6 additions & 6 deletions src/examples/exploring.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

; START:date
(defn date [person-1 person-2 & chaperones]
(println person-1 "and" person-2
(println person-1 "and" person-2
"went out with" (count chaperones) "chaperones."))
; END:date

Expand All @@ -26,7 +26,7 @@
(defn is-small? [number]
(if (< number 100)
"yes"
(do
(do
(println "Saw a big number" number)
"no")))
; END:do
Expand Down Expand Up @@ -54,7 +54,7 @@

; START: index-filter
(defn index-filter [pred coll]
(when pred
(when pred
(for [[idx elt] (indexed coll) :when (pred elt)] idx)))
; END: index-filter
; START:index-of-any
Expand All @@ -63,15 +63,15 @@
; END: index-of-any

; START:greeting
(defn greeting
(defn greeting
"Returns a greeting of the form 'Hello, username.'"
[username]
(str "Hello, " username))
; END:greeting
(def simple-greeting greeting)

; START:greeting-with-default
(defn greeting
(defn greeting
"Returns a greeting of the form 'Hello, username.'
Default username is 'world'."
([] (greeting "world"))
Expand All @@ -84,7 +84,7 @@
(> (count word) 2))
; END:indexable-word

; START:indexable-words
; START:indexable-words
(defn indexable-words [text]
(let [indexable-word? (fn [w] (> (count w) 2))]
(filter indexable-word? (str/split text #"\W+"))))
Expand Down
17 changes: 7 additions & 10 deletions src/examples/functional.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
(fib 0N 1N n))) ; <label id="code.tail-fibo.call"/>
; END: tail-fibo

; START: recur-fibo
; START: recur-fibo
; better but not great
(defn recur-fibo [n]
(letfn [(fib
(letfn [(fib
[current next n]
(if (zero? n)
current
Expand All @@ -32,8 +32,8 @@
; END: recur-fibo

; START: lazy-seq-fibo
(defn lazy-seq-fibo
([]
(defn lazy-seq-fibo
([]
(concat [0 1] (lazy-seq-fibo 0N 1N))) ; <label id="code.lazy-seq-fibo.basis"/>
([a b]
(let [n (+ a b)] ; <label id="code.lazy-seq-fibo.n"/>
Expand Down Expand Up @@ -75,7 +75,7 @@

; START: count-heads-by-pairs
(defn count-heads-pairs [coll]
(count (filter (fn [pair] (every? #(= :h %) pair))
(count (filter (fn [pair] (every? #(= :h %) pair))
(by-pairs coll))))
; END: count-heads-by-pairs
(def count-heads-by-pairs count-heads-pairs)
Expand Down Expand Up @@ -147,20 +147,17 @@
true
(recur next (rest stream))))))))


(declare init more end)

(defn init [stream]
(if (#{'c} (first stream))
(more (rest stream))))

(defn more [stream]
(cond
(cond
(#{'a 'd} (first stream)) (more (rest stream))
(#{'r} (first stream)) (end (rest stream))))

(defn end [stream]
(when-not (seq stream) true))



2 changes: 1 addition & 1 deletion src/examples/import_static.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"Imports the named static fields and/or static methods of the class
as (private) symbols in the current namespace.

Example:
Example:
user=> (import-static java.lang.Math PI sqrt)
nil
user=> PI
Expand Down
18 changes: 8 additions & 10 deletions src/examples/interop.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@

; START:print-element-handler
(def print-element-handler
(proxy [DefaultHandler] []
(startElement
[uri local qname atts]
(proxy [DefaultHandler] []
(startElement
[uri local qname atts]
(println (format "Saw element: %s" qname)))))
; END:print-element-handler

; START:demo-sax-parse
(defn demo-sax-parse [string handler]
(.. SAXParserFactory newInstance newSAXParser
(parse (InputSource. (StringReader. string))
(.. SAXParserFactory newInstance newSAXParser
(parse (InputSource. (StringReader. string))
handler)))
; END:demo-sax-parse

Expand All @@ -83,7 +83,7 @@
(finally
(println "we get to clean up")))
; END:try-finally
)
)

; START:poor-class-available
; not caller-friendly
Expand All @@ -94,9 +94,9 @@

; START:better-class-available
(defn class-available? [class-name]
(try
(try
(Class/forName class-name) true
(catch ClassNotFoundException _ false)))
(catch ClassNotFoundException _ false)))
; END:better-class-available

; START:untyped-describe-class
Expand All @@ -112,5 +112,3 @@
:final (java.lang.reflect.Modifier/isFinal (.getModifiers c))})
; END:typed-describe-class
(def typed-describe-class describe-class)


Loading