Skip to content

Conversation

@bondiano
Copy link
Contributor

No description provided.

Copy link
Collaborator

@margintop15px margintop15px left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

мне понравилось решение
лаконично и понятно
можно немножко абстрагировать код вынеся повторяющиеся части в функции, но это больше вкусовщина
хорошо что умеете работать с атомами и рефами, но эти структуры данных чаще используются для координации состояния между потоками.
в более простых задачах обычно хватает функций и коллекций

Comment on lines 119 to 123
(defn for-each-line [file cb]
(with-open [rdr (io/reader file)]
(doseq [[index line] (map-indexed vector (line-seq rdr))]
(let [line (parse-line line)]
(cb line index)))))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

интересное решение, у меня вопрос а почему бы не вернуть просто данные вызывающему коду? тогда не надо будет использовать атомы для сбора данных

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это также используется в print-table, тут хотел попробовать вытащить общий процесс и передавать обработчик, больше для практики)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ну тут общий процесс может вполне себе вернуть ленивую коллекция а уже вызывающий код может решить что ему делать дальше

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

хмм, действительно, есть же ленивые коллекции :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

только тогда with-open не будет работать

(let [{:keys [file] :as schema} (table table-schema)
all-ids (atom [])
by-id (atom {})]
(for-each-line file (fn [line _] ;; do we have a way to pass callback with only one argument?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

если функция вызывается с двумя аргументами то ее определение тоже должно быть с двумя аргументами иначе будет ArityException

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вот да, тут не хватало полиморфности относительно арности

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вообще говоря можно сделать функцию принимающую разное количество аргументов

(defn my-func
  ([one-arg] function body)
  ([one-arg second-arg] function body))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(let [line (parse-line line)]
          (cb line index))

там же вот такой вызов но в этом случае мне достаточно одного аргумента в колбеке, но все равно приходится передавать функцию с арностью 2

Comment on lines 197 to 215
(defn on-display-total-sales-for-customer []
(let [db (load-tables)]
(println "Enter customer name:")
(let [customer-name (read-line)
{customer-id :id customer :data} (find-by-name db :cust customer-name)]
(if customer
(let [total (calc-total-sales-for-customer db customer-id)]
(println (format "Total Sales for Customer %s: $%.2f" customer-name total)))
(println (format "Customer %s not found" customer-name))))))

(defn on-display-total-count-for-product []
(let [db (load-tables)]
(println "Enter product name:")
(let [product-name (read-line)
{product-id :id product :data} (find-by-name db :prod product-name)]
(if product
(let [count (calc-total-count-for-product db product-id)]
(println (format "Total Count of Product %s: %d" product-name count)))
(println (format "Product %s not found" product-name))))))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

эти две функции прям очень похожи
предлагаю в качестве разминки вынести общий код и параметризировать все отличающиеся части

@bondiano bondiano requested a review from margintop15px June 2, 2023 15:52
@bondiano
Copy link
Contributor Author

bondiano commented Jun 2, 2023

@margintop15px все поправил, если замечаний не будет - можно закрывать! 🫂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants