-
Notifications
You must be signed in to change notification settings - Fork 8
finish 4 hw #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
finish 4 hw #27
Conversation
margintop15px
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
мне понравилось решение
лаконично и понятно
можно немножко абстрагировать код вынеся повторяющиеся части в функции, но это больше вкусовщина
хорошо что умеете работать с атомами и рефами, но эти структуры данных чаще используются для координации состояния между потоками.
в более простых задачах обычно хватает функций и коллекций
otus-06/src/otus_06/homework.clj
Outdated
| (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))))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
интересное решение, у меня вопрос а почему бы не вернуть просто данные вызывающему коду? тогда не надо будет использовать атомы для сбора данных
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
это также используется в print-table, тут хотел попробовать вытащить общий процесс и передавать обработчик, больше для практики)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ну тут общий процесс может вполне себе вернуть ленивую коллекция а уже вызывающий код может решить что ему делать дальше
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
хмм, действительно, есть же ленивые коллекции :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
только тогда with-open не будет работать
otus-06/src/otus_06/homework.clj
Outdated
| (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? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
если функция вызывается с двумя аргументами то ее определение тоже должно быть с двумя аргументами иначе будет ArityException
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
вот да, тут не хватало полиморфности относительно арности
There was a problem hiding this comment.
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))There was a problem hiding this comment.
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
otus-06/src/otus_06/homework.clj
Outdated
| (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)))))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
эти две функции прям очень похожи
предлагаю в качестве разминки вынести общий код и параметризировать все отличающиеся части
|
@margintop15px все поправил, если замечаний не будет - можно закрывать! 🫂 |
No description provided.