beautify code

This commit is contained in:
didier amyot committed 2018-05-31 14:35:08 -04:00
1 parent 3ed03675dd
commit 70d4e9f47f
2 files changed
+64 -75

No files matched your search

+51 -62
View File
@@ -25,24 +25,11 @@
(let [cmp #(compare (:price %2) (:price %1))]
(apply sorted-list-by cmp args)))
(def book {:sell (create-sell-book) :buy (create-sell-book)})
(defn buy? [order]
(= :buy (:type order)))
(defn create-trade [order-to-fulfill book-order]
#:book.order{:type (:type book-order)
:quantity (:quantity order-to-fulfill)
:price (min (:price order-to-fulfill) (:price book-order))
:order-id (:book.order/id order-to-fulfill)
:book-id (:book.order/id book-order)})
(defn sell? [order]
(= (:type order) :sell))
@@ -52,24 +39,38 @@
(defn buy-order-fullfillable? [book-order order]
(<= (:price book-order) (:price order)))
(defn create-sell-trade-factory [order-to-fulfill]
(fn [trade book-order]
(let [already-fulfilled (:book.order/already-fulfilled trade 0)
qtn-to-fulfill (min (:quantity book-order) (- (:quantity order-to-fulfill) already-fulfilled))
can-fulfill? (sell-order-fullfillable? book-order order-to-fulfill)]
(when (and can-fulfill? (< 0 qtn-to-fulfill))
#:book.order{:type :buy
:quantity qtn-to-fulfill
:already-fulfilled (+ already-fulfilled qtn-to-fulfill)
:price (max (:price book-order) (:price order-to-fulfill))
:order-id (:book.order/id order-to-fulfill)
:book-id (:book.order/id book-order)}))))
(defn create-buy-trade-factory [order-to-fulfill]
(fn [trade book-order]
(let [already-fulfilled (:book.order/already-fulfilled trade 0)
qtn-to-fulfill (min (:quantity book-order) (- (:quantity order-to-fulfill) already-fulfilled))
can-fulfill? (buy-order-fullfillable? book-order order-to-fulfill)]
(when (and can-fulfill? (< 0 qtn-to-fulfill))
#:book.order{:type :sell
:quantity qtn-to-fulfill
:already-fulfilled (+ already-fulfilled qtn-to-fulfill)
:price (min (:price book-order) (:price order-to-fulfill))
:order-id (:book.order/id order-to-fulfill)
:book-id (:book.order/id book-order)}))))
(defn create-trade-factory [order-to-fulfill]
(let [trade-type (if (sell? order-to-fulfill) :buy :sell)
trade-price (if (sell? order-to-fulfill) max min)
fulfillable? (if (sell? order-to-fulfill) sell-order-fullfillable? buy-order-fullfillable?)]
(fn [trade book-order]
(let [already-fulfilled (:book.order/already-fulfilled trade 0)
qtn-to-fulfill (min (:quantity book-order) (- (:quantity order-to-fulfill) already-fulfilled))
can-fulfill? (fulfillable? book-order order-to-fulfill)]
(when (and can-fulfill? (< 0 qtn-to-fulfill))
#:book.order{:type trade-type
:quantity qtn-to-fulfill
:already-fulfilled (+ already-fulfilled qtn-to-fulfill)
:price (trade-price (:price book-order) (:price order-to-fulfill))
:order-id (:book.order/id order-to-fulfill)
:book-id (:book.order/id book-order)})))))
(if (sell? order-to-fulfill)
(create-sell-trade-factory order-to-fulfill)
(create-buy-trade-factory order-to-fulfill)))
(defn create-sell-trades [order-to-fulfill sell-book]
(defn create-trades [order-to-fulfill sell-book]
(let [create-trade (create-trade-factory order-to-fulfill)
trades (reductions create-trade {} sell-book)
ret (rest (take-while some? trades))]
@@ -78,54 +79,42 @@
(defn partial-trade? [trade order]
(not= (:quantity order) (:book.order/quantity trade)))
(defn compute-partial-order [order trade]
(assoc order :quantity (- (:quantity order) (:book.order/quantity trade 0))))
(defn compute-new-sell-book-after-trades [sell-book trades]
(defn compute-new-book-after-trades [sell-book trades]
(let [new-book (drop (-> trades count dec) sell-book)
possible-partial-trade (last trades)]
(if (partial-trade? possible-partial-trade (first new-book))
(conj (rest new-book) (compute-partial-order (first new-book) possible-partial-trade))
(rest new-book))))
(defn leftover-order [order trades]
(let [traded-quantity (reduce + (map :book.order/quantity trades))
leftover-quantity (- (:quantity order) traded-quantity)]
(when (pos? leftover-quantity)
(assoc order :quantity leftover-quantity))))
(defn match-buy-order-in-sell-book [sell-book order]
(let [trades (create-sell-trades order sell-book)
new-book (compute-new-sell-book-after-trades sell-book trades)
(defn match-order-in-book [sell-book order]
(let [trades (create-trades order sell-book)
new-book (compute-new-book-after-trades sell-book trades)
new-buy-order (leftover-order order trades)]
[trades new-book new-buy-order]))
(defn match-sell-order-in-buy-book [buy-book order]
(let [trades (create-sell-trades order buy-book)
new-book (compute-new-sell-book-after-trades buy-book trades)
new-buy-order (leftover-order order trades)]
[trades new-book new-buy-order]))
(defn place-buy-order [book order]
(match-buy-order-in-sell-book (:sell book) order)
(println :bingo)) ;; should return multiple "trade" and a new book
(defn place-order [book order]
(if (buy? order)
(place-buy-order book order)))
(defn -main
[& args]
(let [order (-> args first edn/read-string)]
(if (not (s/valid? :book.order/order order))
(println "invalid order :( ")
(println (place-order book order)))))
;(defn place-buy-order [book order]
; (match-buy-order-in-sell-book (:sell book) order)
; (println :bingo)) ;; should return multiple "trade" and a new book
;
;(defn place-order [book order]
; (if (buy? order)
; (place-buy-order book order)))
;
;
;(defn -main
; [& args]
; (let [order (-> args first edn/read-string)]
; (if (not (s/valid? :book.order/order order))
; (println "invalid order :( ")
; (println (place-order book order)))))
;
;