Skip to content

Instantly share code, notes, and snippets.

View daveyarwood's full-sized avatar

Dave Yarwood daveyarwood

View GitHub Profile
@daveyarwood
daveyarwood / wpgtr-ch3.clj
Created January 27, 2014 00:37
why's (poignant) guide to ruby in clojure - chapter 3
; ex. 1:
(repeat 5 "Odelay!")
; ex. 2:
(when-not (re-find #"aura" "restaurant") (System/exit 0))
; ex. 3:
(map clojure.string/capitalize ["toast" "cheese" "wine"])
; ex. 4:
@daveyarwood
daveyarwood / wpgtr-ch4.clj
Created February 21, 2014 01:35
why's poignant guide to ruby in clojure - chapter 4
; ex. 1:
(def blue-crystal 1)
(def leaf-tender 5)
; ex. 2:
(catch-a-star pipe)
; ex. 3:
(def captive-star (catch-a-star pipe))
; ex. 1:
(defn dr-chams-timeline [year]
(cond
(= year 1894)
"Born."
(<= 1895 year 1913)
"Childhood in Louisville, Winston Co., Mississippi."
(<= 1914 year 1919)
"Worked at a pecan nursery; punched a Quaker."
(<= 1920 year 1928)
; exs. 32-34:
; not applicable to Clojure (Ruby class inheritance)
; although this is somewhat similar to implementing protocols in records
; ex. 35:
(defn mail-them-a-kit [address]
(when-not (instance? address ex.Address)
(throw (IllegalArgumentException. "No Address object found.")))
(print (formatted address)))
(fn best-hand [card-strings]
(let [card-parser (fn [[s r]]
(let [suit ({\S :spade, \H :heart,
\D :diamond, \C :club} s)
rank (if (> (Character/digit r 10) -1)
(- (Character/digit r 10) 2)
({\T 8, \J 9,
\Q 10, \K 11, \A 12} r))]
{:suit suit, :rank rank}))
cards (map card-parser card-strings)
(fn best-hand [card-strings]
(let [card-parser (fn [[s r]]
(let [suit ({\S :spade, \H :heart,
\D :diamond, \C :club} s)
rank (if (> (Character/digit r 10) -1)
(- (Character/digit r 10) 2)
({\T 8, \J 9,
\Q 10, \K 11, \A 12} r))]
{:suit suit, :rank rank}))
cards (map card-parser card-strings)
for x in range(1,101):
if x % 15 == 0:
print "FizzBuzz"
elif x % 3 == 0:
print "Fizz"
elif x % 5 == 0:
print "Buzz"
else:
print x
(defn fizz-buzz [n]
(cond
(zero? (rem n 15)) "FizzBuzz"
(zero? (rem n 3)) "Fizz"
(zero? (rem n 5)) "Buzz"
:else n))
(doseq [x (map fizz-buzz (range 1 101))]
(println x))
(def fizz-buzz
(->> (map vector (range 1 101) (repeat 100 ""))
(map (fn [[i s]] [i (if (zero? (rem i 3)) (str s "Fizz") s)]))
(map (fn [[i s]] [i (if (zero? (rem i 5)) (str s "Buzz") s)]))
(map (fn [[i s]] (if (= "" s) i s)))))
(doseq [x fizz-buzz] (println x))
(defn fizz-buzz [n]
(let [s (str (when (zero? (rem n 3)) "Fizz")
(when (zero? (rem n 5)) "Buzz"))]
(if (empty? s) n s)))
(doseq [x (map fizz-buzz (range 1 101))]
(println x))