Skip to content

Commit

Permalink
Refactor using Reduce in count-user-pr function
Browse files Browse the repository at this point in the history
  • Loading branch information
lordaniel committed Nov 15, 2016
1 parent 057061a commit cb3f525
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/hacktoboy/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@
([org-name token]
"Get Url Events for all Member for org-ame. Requires token and be member"
(map (partial get-value "events_url") (parse->clojure (str base-url "/orgs/" org-name "/members?access_token=" token)))))

(defn count-user-pr [arr]
(loop [[{type "type" date "created_at" {name "name"} "repo" {login "login"} "actor"} & t] arr prs '()]
(if (empty? type)
(when (> (count prs) 0)
{:user (:actor (first prs)) :score (count prs) :last (:date (last prs))})
(recur t (if (and (= type "PullRequestEvent") (.startsWith date "2016-10"))
(conj prs {:actor login :repo name :date date})
prs)))))

(defn count-user-pr [user-events]
"Count User Pull Request Events"
(if-let [user-score
(reduce (fn [final-prs pr]
(let [{type "type" date "created_at" {name "name"} "repo" {login "login"} "actor"} pr]
(if (and (= type "PullRequestEvent") (.startsWith date "2016-10"))
(conj final-prs {:actor login :repo name :date date})
final-prs)))

This comment has been minimized.

Copy link
@edwaraco

edwaraco Nov 15, 2016

I believe it's not necesary this return.
I believe this block maybe:

(when (and (= type "PullRequestEvent") (.startsWith date "2016-10"))
  (conj final-prs {:actor login :repo name :date date}))

@sarcilav What do you think about?

This comment has been minimized.

Copy link
@edwaraco

edwaraco Nov 15, 2016

I'm sorry I just check that it is necesary :D.

This comment has been minimized.

Copy link
@sarcilav

sarcilav Nov 15, 2016

Agree @earayo plus the let it is not require, you can do the same destructuring on the fn definition

'()
user-events)]
{:user (:actor (first user-score)) :score (count user-score) :last (:date (last user-score))}))

;;Get Members Score
(defn -main [& args]
(println "User - Score")
(apply println (map (fn [m] (str "\n" (:user m) " - " (:score m)))
(sort-by :score > (filter (fn [x] (> (count x) 0))
(sort-by :score > (filter (fn [x] (> (:score x) 0))
(map count-user-pr
(map parse->clojure (apply members-url-events args))))))))


0 comments on commit cb3f525

Please sign in to comment.