Skip to content

Instantly share code, notes, and snippets.

@kindlychung
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kindlychung/f7f3cec02b449dfccd6e to your computer and use it in GitHub Desktop.
Save kindlychung/f7f3cec02b449dfccd6e to your computer and use it in GitHub Desktop.
clojure for the true and brave
(ns clojure-noob.core
(:gen-class))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "Hello, World!"))
(println "hi there")
(if true
"blahblah"
"lablab")
(if true
(do
(println "Success!"))
(do
(println "Ahoh...")))
(when true
(println "AAA")
(println "BBB"))
(def some-names ["Larry", "Potter"])
(println some-names)
(def severity :mild)
(def severity :notmild)
(def error-msg "oh god!")
(if (= severity :mild)
(do
(def error-msg (str error-msg " mildly bad."))
(println error-msg)
)
(do
(def error-msg (str error-msg " very bad."))
(println error-msg)
)
)
(nil? 1)
(nil? true)
(nil? nil)
(= 1 1)
(= nil 1)
(= nil nil)
(def mydict
{
:a {:b {:c 15}}
:b "boring"
:c []
"plus" +
})
(get mydict :b)
(get mydict :a)
(get mydict :c)
(get mydict "plus")
(get-in mydict [:a :b :c])
(conj [1 2 3], 4)
(nth '(1 2 3 4) 3)
(conj '(1 2 3) 4)
(conj #{:a :b} :b)
(inc 1)
(map inc [0 1 2])
(defn enth
"Return a cheer"
[name]
(str "Oh, my god." name " You are awesome!"))
(enth "Albert")
(doc enth)
(defn oload
([x y z]
(str x y z)
)
(
[x y]
(str x y)
)
(
[x]
(str x)
))
(oload "what " "is")
(defn codger-communication
[x]
(str "Get off my laun, " x "!!!"))
(defn codger
[& xs]
(map codger-communication xs))
(codger "Venneta" "Wouter")
(defn favor
[name & things]
(str "Hi, " name ", here are my favorite things: " (clojure.string/join ", " things)))
(favor "Doreen", "gum" "shoes" "karate")
(defn my-first
[[x]]
x)
(my-first [1 2 3])
(defn first1
[xs]
(first xs))
(first1 [1 2 3])
(defn chooser
[[a1 a2 & unimportant]]
(println (str "Your first choice is: " a1))
(println (str "Your second choice is: " a2))
(println (str "We are ignoring the rest: "
(clojure.string/join unimportant)
))
)
(chooser ["a", "b", "ccc", "ddd"])
(defn trloc
[{lat :lat lng :lng}]
(println (str "Treasure lat: " lat))
(println (str "Treasure lat: " lng))
)
(trloc {:lat 23 :lng 83})
(defn trloc
[{:keys [lat lng]}]
(println (str "Treasure lat: " lat))
(println (str "Treasure lat: " lng))
)
(trloc {:lat 23 :lng 83})
(map (fn [name] (str "Hi, " name))
["Vader", "Moeder"]
)
((fn [x] (* x 3)) 8)
(#(* % 3) 8)
(map #(str "Hi, " %) ["Emily" "Jane"])
(#(str %1 " and " %2) "corn bread" "butter beans")
(#(identity %&) 1 "blah" :a)
(def addhi )
((fn [xs] (map (fn [a] (str "hi, " a)) xs)) "newton" "plato")
((fn [xs] (map (fn [x] (str "Hello, " xs)) xs)) "newton" "plato")
;; Two ways to write a partially applied functions in clojure (PAF)
((fn [x] (str "Hi, " x)) "there.")
((partial str "Hi, ") "there.")
;; Define these PAFs
(def addhi1 (fn [x] (str "Hi, " x)))
(def addhi2 (partial str "Hi, "))
;; Map these two PAFs to a list, results are the same
(map addhi1 ["Newon", "Einstein"])
(map addhi2 ["Newon", "Einstein"])
;; Passing multiple args to a vector
(defn addhi-map1 [& xs] (map addhi1 xs))
(defn addhi-map2 [& xs] (map addhi2 xs))
(addhi-map1 "Newton" "Einstein")
(addhi-map2 "Newton" "Einstein")
(map (fn [x] (str "Hello, " x)) ["a" "b"])
((fn [xs] (map (fn [x] (str "Hello, " x)) xs)) ["newton" "plato"])
((fn [& xs] (map (partial str "Hello, ") xs)) "Newton", "Plato")
(defn inc-maker
"Create a custom incrementor"
[inc-by]
(fn [x] (+ inc-by x))
)
((inc-maker 3) 5)
(def asym-hobbit-body-parts [{:name "head" :size 3}
{:name "left-eye" :size 1}
{:name "left-ear" :size 1}
{:name "mouth" :size 1}
{:name "nose" :size 1}
{:name "neck" :size 2}
{:name "left-shoulder" :size 3}
{:name "left-upper-arm" :size 3}
{:name "chest" :size 10}
{:name "back" :size 10}
{:name "left-forearm" :size 3}
{:name "abdomen" :size 6}
{:name "left-kidney" :size 1}
{:name "left-hand" :size 2}
{:name "left-knee" :size 2}
{:name "left-thigh" :size 4}
{:name "left-lower-leg" :size 3}
{:name "left-achilles" :size 1}
{:name "left-foot" :size 2}])
(defn needs-matching-part?
[part]
(re-find #"^left-" (:name part))
)
(defn make-matching-part
[part]
{:name (clojure.string/replace (:name part) #"^left-" "right-")
:size (:size part)}
)
(defn symmetrize-body-parts
"Expects a seq of maps which have a :name and :size"
[asym-body-parts]
(loop [remaining-asym-parts asym-body-parts final-body-parts []]
(if (empty? remaining-asym-parts)
final-body-parts
(let [[part & remaining] remaining-asym-parts final-body-parts (conj final-body-parts part)]
(if (needs-matching-part? part)
(recur remaining (conj final-body-parts (make-matching-part part)))
(recur remaining final-body-parts)
)
)
)
)
)
(symmetrize-body-parts asym-hobbit-body-parts)
(let [x 3])
(println x)
(def dalmatian-list
["Pongo" "Perdita" "Puppy 1" "Puppy 2"]) ; and 97 more...
(let [dalmatians (take 3 dalmatian-list)]
dalmatians)
;; Take the 1st and assign to pongo, all the rest assigned to dalmatians
(let [[pongo & dalmatians] dalmatian-list]
[pongo dalmatian-list]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment