Created
June 6, 2013 14:10
-
-
Save Velrok/5721801 to your computer and use it in GitHub Desktop.
Some example code to answer the question of when to move results into a let.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; using a let to store intermediat results | |
(defn recommendations [scores user] | |
(let [user-row (.viewRow (:matrix scores) | |
(get (:row-mapping scores) | |
user)) | |
sorted-by-score (sort-by #(get % 1) | |
(non-zero user-row))] | |
(map (fn [[item, score]] | |
{:item item, :score score}) | |
sorted-by-score))) | |
;; don't use any let | |
(defn recommendations [scores user] | |
(map (fn [[item, score]] | |
{:item item, :score score}) | |
(sort-by #(get % 1) | |
(non-zero (.viewRow (:matrix scores) | |
(get (:row-mapping scores) | |
user)))))) | |
(defn recommendations [scores user]
(->> user
(get (:row-mapping scores))
(.viewRow (:matrix scores)) ;; User row
non-zero
(sort-by #(get % 1)) ;; Sorted by score
(map (fn [[item, score]]
{:item item, :score score}))))
Thanks for the comment. I will refactor my code, because this version is very nice.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I like it. :)
I still need to get used to the ->> and -> operators.
Often times I find that wen I need to use use directly I end up needing the other one.
For now I can only see them as a refactoring.