Fork me on GitHub
#beginners
<
2017-07-14
>
diginomad05:07:51

Do ref's alter 'ed inside a (dosync) get committed at the end of transaction?

Alex Miller (Clojure team)13:07:43

yes (what else would possibly happen? :)

pitalig16:07:28

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)?

noisesmith16:07:20

one simple thing is to use partial

(def g (partial F X))
(g B)

noisesmith16:07:12

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

schmee16:07:57

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”

mobileink19:07:10

@pitalig: convenience macro?

mobileink19:07:38

(defmacro f3 [x] (f 3 x))

noisesmith19:07:50

+ one less call frame per usage - not usable first class

mobileink19:07:46

yeah. avoid macros - unless useful.

pitalig20:07:01

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...

seancorfield20:07:44

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.

seancorfield20:07:28

I probably wouldn't combine the first and filter calls because filtering may be more reusable on its own.

seancorfield20:07:32

So

(defn filter-by [key value data]
  (filter #(= (% key) value) data))

(def filter-by-user (partial filter-by :user))

seancorfield20:07:52

And then call (first (filter-by-user some-val the-data)) as needed

pitalig20:07:00

Nice! Thank you!

seancorfield20:07:22

Take a look at when-first which might also help you here...

seancorfield20:07:43

(when-first [match (filter-by-user some-val the-data]
  (do-stuff-to match))