refactored files.
This commit is contained in:
1 parent
70d4e9f47f
commit
b1ba21f391
4 files changed
+126
-121
No files matched your search
+3
-118
@@ -1,120 +1,5 @@
|
|||||||
(ns book.core
|
(ns book.core)
|
||||||
(:import [book.PersistentSortedList])
|
|
||||||
(:require [book.order]
|
|
||||||
[clojure.spec.alpha :as s]
|
|
||||||
[clojure.edn :as edn])
|
|
||||||
(:gen-class))
|
|
||||||
|
|
||||||
|
|
||||||
(defn sorted-list [& keys]
|
(defn -main [& args]
|
||||||
(book.PersistentSortedList/create (seq keys)))
|
(println "hello workd"))
|
||||||
|
|
||||||
(defn sorted-list-by [comparator & keys]
|
|
||||||
(book.PersistentSortedList/create comparator (seq keys)))
|
|
||||||
|
|
||||||
|
|
||||||
;; Let say a book is two collections. The first called
|
|
||||||
;; sell-book contains all the sell orders. The second
|
|
||||||
;; called buy-book contains all the buy orders.
|
|
||||||
|
|
||||||
(defn create-sell-book [& args]
|
|
||||||
(let [cmp #(compare (:price %1) (:price %2))]
|
|
||||||
(apply sorted-list-by cmp args)))
|
|
||||||
|
|
||||||
(defn create-buy-book [& args]
|
|
||||||
(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 sell? [order]
|
|
||||||
(= (:type order) :sell))
|
|
||||||
|
|
||||||
(defn sell-order-fullfillable? [book-order order]
|
|
||||||
(>= (:price book-order) (:price order)))
|
|
||||||
|
|
||||||
(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]
|
|
||||||
(if (sell? order-to-fulfill)
|
|
||||||
(create-sell-trade-factory order-to-fulfill)
|
|
||||||
(create-buy-trade-factory order-to-fulfill)))
|
|
||||||
|
|
||||||
(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))]
|
|
||||||
(map #(dissoc % :book.order/already-fulfilled) ret)))
|
|
||||||
|
|
||||||
(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-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-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 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)))))
|
|
||||||
;
|
|
||||||
;
|
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
(ns book.match
|
||||||
|
(:import [book.PersistentSortedList])
|
||||||
|
(:require [book.order]
|
||||||
|
[clojure.spec.alpha :as s]
|
||||||
|
[clojure.edn :as edn])
|
||||||
|
(:gen-class))
|
||||||
|
|
||||||
|
|
||||||
|
(defn sorted-list [& keys]
|
||||||
|
(book.PersistentSortedList/create (seq keys)))
|
||||||
|
|
||||||
|
(defn sorted-list-by [comparator & keys]
|
||||||
|
(book.PersistentSortedList/create comparator (seq keys)))
|
||||||
|
|
||||||
|
|
||||||
|
;; Let say a book is two collections. The first called
|
||||||
|
;; sell-book contains all the sell orders. The second
|
||||||
|
;; called buy-book contains all the buy orders.
|
||||||
|
|
||||||
|
(defn create-sell-book [& args]
|
||||||
|
(let [cmp #(compare (:price %1) (:price %2))]
|
||||||
|
(apply sorted-list-by cmp args)))
|
||||||
|
|
||||||
|
(defn create-buy-book [& args]
|
||||||
|
(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 sell? [order]
|
||||||
|
(= (:type order) :sell))
|
||||||
|
|
||||||
|
(defn sell-order-fullfillable? [book-order order]
|
||||||
|
(>= (:price book-order) (:price order)))
|
||||||
|
|
||||||
|
(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]
|
||||||
|
(if (sell? order-to-fulfill)
|
||||||
|
(create-sell-trade-factory order-to-fulfill)
|
||||||
|
(create-buy-trade-factory order-to-fulfill)))
|
||||||
|
|
||||||
|
(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))]
|
||||||
|
(map #(dissoc % :book.order/already-fulfilled) ret)))
|
||||||
|
|
||||||
|
(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-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-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 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)))))
|
||||||
|
;
|
||||||
|
;
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
(ns book.core-test
|
(ns book.match-test
|
||||||
(:require [clojure.test :refer :all]
|
(:require [clojure.test :refer :all]
|
||||||
[book.core :refer :all]))
|
[book.match :refer :all]))
|
||||||
|
|
||||||
|
|
||||||
(deftest test-match-buy-order-in-sell-book
|
(deftest test-match-buy-order-in-sell-book
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
(ns book.test-PersistentSortedList
|
(ns book.test-PersistentSortedList
|
||||||
(:require [clojure.test :refer :all]
|
(:require [clojure.test :refer :all]
|
||||||
[book.core :refer [sorted-list sorted-list-by]]))
|
[book.match :refer [sorted-list sorted-list-by]]))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user