From b1ba21f39146eb693eb8b079307e8a8f6c8bfc9c Mon Sep 17 00:00:00 2001 From: didier amyot Date: Thu, 31 May 2018 14:46:29 -0400 Subject: [PATCH] refactored files. --- src/book/core.clj | 121 +------------------- src/book/match.clj | 120 +++++++++++++++++++ test/book/{core_test.clj => match_test.clj} | 4 +- test/book/test_PersistentSortedList.clj | 2 +- 4 files changed, 126 insertions(+), 121 deletions(-) create mode 100644 src/book/match.clj rename test/book/{core_test.clj => match_test.clj} (99%) diff --git a/src/book/core.clj b/src/book/core.clj index 65bea9d..010c43e 100644 --- a/src/book/core.clj +++ b/src/book/core.clj @@ -1,120 +1,5 @@ -(ns book.core - (:import [book.PersistentSortedList]) - (:require [book.order] - [clojure.spec.alpha :as s] - [clojure.edn :as edn]) - (:gen-class)) +(ns book.core) -(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))))) -; -; +(defn -main [& args] + (println "hello workd")) \ No newline at end of file diff --git a/src/book/match.clj b/src/book/match.clj new file mode 100644 index 0000000..5598b78 --- /dev/null +++ b/src/book/match.clj @@ -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))))) +; +; diff --git a/test/book/core_test.clj b/test/book/match_test.clj similarity index 99% rename from test/book/core_test.clj rename to test/book/match_test.clj index d51b48e..88b9cc9 100644 --- a/test/book/core_test.clj +++ b/test/book/match_test.clj @@ -1,6 +1,6 @@ -(ns book.core-test +(ns book.match-test (:require [clojure.test :refer :all] - [book.core :refer :all])) + [book.match :refer :all])) (deftest test-match-buy-order-in-sell-book diff --git a/test/book/test_PersistentSortedList.clj b/test/book/test_PersistentSortedList.clj index cf30676..08c9031 100644 --- a/test/book/test_PersistentSortedList.clj +++ b/test/book/test_PersistentSortedList.clj @@ -1,6 +1,6 @@ (ns book.test-PersistentSortedList (:require [clojure.test :refer :all] - [book.core :refer [sorted-list sorted-list-by]])) + [book.match :refer [sorted-list sorted-list-by]]))