This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-07-14
Channels
- # aleph (16)
- # bangalore-clj (4)
- # beginners (19)
- # boot (27)
- # cider (81)
- # clara (2)
- # cljs-dev (343)
- # cljsrn (97)
- # clojure (224)
- # clojure-hk (1)
- # clojure-italy (25)
- # clojure-russia (5)
- # clojure-serbia (2)
- # clojure-spec (7)
- # clojure-uk (27)
- # clojurescript (97)
- # cursive (8)
- # datomic (48)
- # docker (1)
- # emacs (15)
- # hoplon (39)
- # jobs (4)
- # lumo (13)
- # off-topic (2)
- # om (66)
- # onyx (7)
- # parinfer (5)
- # pedestal (2)
- # play-clj (10)
- # protorepl (2)
- # quil (1)
- # re-frame (38)
- # reagent (33)
- # spacemacs (1)
- # specter (4)
- # sql (19)
- # test-check (31)
- # unrepl (4)
- # untangled (3)
yes (what else would possibly happen? :)
Guys, me again, when do you think that a new and more specific function is necessary? For example, I have a function F with two args (A and B), but I call it about 5 times with A=X in my app, should I create another function Fx that receives just as arg B and call (F X B)?
one simple thing is to use partial
(def g (partial F X))
(g B)
but it really depends on how things are being used, sometimes partial just makes code a lot harder to read by compressing it, rather than improving it
I tend to not think of it in terms of text, i.e “how many times have I typed this”, but in abstactions, i.e “does a partial application of this function represent something sensible in my domain”
+ one less call frame per usage - not usable first class
So, just to clarify, here is an example:
; Funtion for filter a dataset and get just the first element from results
(defn filter-first-by [key value data]
(first (filter (fn [obj] (= (obj (keyword key)) value)) data)))
; Funtion for filter a user from dataset and get just the first element from results
(defn filter-first-by-user [value data]
(filter-first-by "user" value data))
; Another funtion for filter a user from dataset and get just the first element from results
(def filter-first-by-user2
(partial filter-first-by "user"))
What do you think about last two functions? They will be called about five times...Given that you're calling keyword
unconditionally, I'd probably skip that and just pass in the keyword in the first place instead of a string.
I probably wouldn't combine the first
and filter
calls because filtering may be more reusable on its own.
So
(defn filter-by [key value data]
(filter #(= (% key) value) data))
(def filter-by-user (partial filter-by :user))
And then call (first (filter-by-user some-val the-data))
as needed
Take a look at when-first
which might also help you here...
(when-first [match (filter-by-user some-val the-data]
(do-stuff-to match))