Contenu connexe Similaire à Life without Objects (w/ Clojure) (20) Life without Objects (w/ Clojure)1.
( life w/o objects )
( oop → functional design )
( with clojure )
(Osvaldas Grigas | )@ogrigas
18. Or ... just functions
(defn downloadfiles [location]
(...))
(defn extractzip [archive]
(...))
30. Or ... just functions
(defn saveproduct [product]
(...))
(defn findproductbyid [id]
(...))
43. append-child array-map assoc associative? assoc-in bean bigdec bigint biginteger bit-and bit-and-not bit-clear
bit-flip bit-not bit-or bit-set bit-shi -le bit-shi -right bit-test bit-xor blank? branch? butlast byte capitalize cat
char char? char-escape-string char-name-string children coll? compare concat conj cons contains? count
counted? cycle dec dec' decimal? dedupe dense-int-set diff difference disj dissoc distinct distinct? double
down drop drop-last drop-while edit eduction empty empty? end? enumeration-seq escape even? every?
false? ffirst file-seq filter filterv find find-keyword first flatten float float? fnext for format frequencies gensym
get get-in group-by hash-map hash-set identical? inc inc' index insert-child insert-le insert-right int integer?
interleave interpose intersection int-map into into-array int-set iterate iterator-seq join keep keep-indexed key
keys keyword keyword? last lazy-cat lazy-seq le le most le s line-seq list list? list* long lower-case make-
node map map? mapcat map-indexed map-invert mapv max max-key merge merge-with min min-key mod
neg? next nfirst nil? nnext node not not= not-any? not-empty not-every? nth nthnext nthrest num number?
odd? partition partition-all partition-by path peek pmap pop pos? postwalk postwalk-demo postwalk-replace
prev prewalk prewalk-demo prewalk-replace priority-map project quot rand rand-int rand-nth random-sample
range ratio? rational? rationalize reduce reduce-kv reductions re-find re-groups rem re-matcher re-matches
remove rename rename-keys re-pattern repeat repeatedly replace replace-first re-quote-replacement re-seq
rest resultset-seq reverse reversible? right rightmost rights root rseq rsubseq second select select-keys seq
seq? seque sequence sequential? seq-zip set set? short shuffle some some? sort sort-by sorted? sorted-map
sorted-map-by sorted-set sorted-set-by split split-at split-lines split-with str string? subs subseq subset?
subvec superset? symbol symbol? take take-last take-nth take-while to-array-2d transduce tree-seq trim triml
trim-newline trimr true? unchecked-add unchecked-dec unchecked-inc unchecked-multiply unchecked-
negate unchecked-subtract union unsigned-bit-shi -right up update update-in upper-case val vals vec vector
vector? vector-of vector-zip walk when-first xml-seq xml-zip zero? zipmap
53. "Object is a collection of partially-applied functions."
( J.B. Rainsberger )
77. "Class" as a namespace
(def Account
{'deposit (fn [state amount]
(update state :balance + amount))
'withdraw (fn [state amount]
(if (< amount (state :balance))
(update state :balance amount)
state))})
(def state0 {:balance 0})
(def state1 ((Account 'deposit) state0 100))
(def state2 ((Account 'withdraw) state1 30))
(def state3 ((Account 'withdraw) state2 20))
78. Partially apply state with "constructor"
(defn newobject [clazz initialstate]
..........
..........
..........)
(def acc (newobject Account {:balance 0}))
(acc 'deposit 100)
(acc 'withdraw 30)
(acc 'withdraw 20)
79. Partially apply state with "constructor"
(defn newobject [clazz initialstate]
(let [stateref (atom initialstate)]
..........
..........))
(def acc (newobject Account {:balance 0}))
(acc 'deposit 100)
(acc 'withdraw 30)
(acc 'withdraw 20)
80. Partially apply state with "constructor"
(defn newobject [clazz initialstate]
(let [stateref (atom initialstate)]
(fn [method & args]
..........)))
(def acc (newobject Account {:balance 0}))
(acc 'deposit 100)
(acc 'withdraw 30)
(acc 'withdraw 20)
81. Partially apply state with "constructor"
(defn newobject [clazz initialstate]
(let [stateref (atom initialstate)]
(fn [method & args]
(apply swap! stateref (clazz method) args))))
(def acc (newobject Account {:balance 0}))
(acc 'deposit 100)
(acc 'withdraw 30)
(acc 'withdraw 20)