Fork me on GitHub
#beginners
<
2015-12-25
>
polymeris01:12:57

So I have this function:

(defn note [note-name]
  (let [groups (->> (name note-name)
                    (re-matcher #"^([a-gA-g])([^\d]*)(\d+)$")
                    re-find
                    rest)
        scale (read-string (last groups))
        base (-> (first groups)
                 clojure.string/lower-case
                 first
                 char
                 int
                 (- 145))
        intonation (case (second groups)
                     ("\uD834\uDD2A" "##") 2
                     ("♯" "#") 1
                     ("" "♮") 0
                     ("♭" "b") -1
                     ("\uD834\uDD2B" "bb") -2)]
    (->Note (+ (* 12 scale)
               base
               intonation))))
Any comments on implementation or style are welcome, but I am mostly wondering about how to handle errors. Should I wrap it all in a try/catch and throw my own IllegalArgumentException explaining the expected format?

polymeris01:12:25

And, while we are at it... Is clojure smart enough to reuse the Matcher generated by (re-matcher #"^([a-gA-g])([^\d]*)(\d+)$") if I call the function multiple times, or should I move it out of the function so it get's called only once?

mudphone01:12:31

You could def it

polymeris01:12:11

yeah, but is it necessary?

polymeris01:12:10

Nevermind, it changes anyways

polymeris01:12:17

Was a dumb question

kopasetik01:12:19

Is it possible to nest one shorthand anonymous function literal e.g. #(println %) in another?

kopasetik01:12:37

Especially concerned about parameters

polymeris01:12:47

you can nest (fn [] ...) inside each other

polymeris01:12:19

as you said, knowing which fn the % parameter refers to would be impossible

meikemertsch12:12:22

@kopasetik: if it's as easy as one param in the end, you can write things link (partial function) . For println you don't need an explicit function...

eggsyntax14:12:11

You can also nest (fn [] …) inside #( … ), which is what I tend to do when I hit a case where I would have liked to nest the #.

not-much-io22:12:52

'[(todos/add {:title ~title})]
I want to pass title as its value but I get (clojure.core.unquote title) Am I doing something silly?

mudphone22:12:19

@not-much-io:

(let [title "hello"] 
  `[(todos/add {:title ~title})])
=> [(todos/add {:title "hello"})]

mudphone22:12:49

note the back quote

mudphone22:12:32

a.k.a. syntax-quote

not-much-io22:12:23

oh, so the wrong type of quote?

not-much-io22:12:47

alright, damn... Thanks! simple_smile

mudphone22:12:54

looks like you’re checking out Om.Next simple_smile

not-much-io22:12:51

Yup, was going well until started making queries and the quote tripped me 😄

mudphone22:12:39

good times, me too… liberal use of om/get-query in the REPL simple_smile

not-much-io22:12:56

Can't figure out how to pass parameters to a mutate query 😕 om/IQueryParams is ignored and can't pass parameters straight because of the tick (')

mudphone22:12:14

you’re sorted now though, right?

not-much-io22:12:15

No not really, because a mutation query has to be used with the tick (') and as I understand it cannot be escaped?

mudphone22:12:48

you can pass params to a mutate

not-much-io22:12:08

with om/IQueryParams or straight?

mudphone22:12:34

for example, here’s one of my mutate methods:

(defmulti mutate om/dispatch)

(defmethod mutate 'page/select
  [{:keys [state] :as env} key {:keys [page] :as params}]
  {:action (swap! state assoc-in [:page/current] page)})

mudphone22:12:12

where I pass the page as {:page blah}

mudphone22:12:49

(om/transact! this `[(page/select {:page ~page}) :page/current])

mudphone22:12:59

does that help? I haven’t used IQueryParams outside of the wiki tutorials yet

not-much-io22:12:56

Damn... Cursive gave me a cannot be resolved on my query when i switched '->` so I assumed it wouldn't work, but it actually works 😅

mudphone22:12:39

Yeah, I have to switch my smartparens off to insert it

not-much-io22:12:55

That's what I get for depending too much on Cursive. But thanks alot, it worked, finally!

mudphone22:12:05

I insert the double back quotes, then switch it off, then erase one, then switch it back on

mudphone22:12:52

My assumption with IQueryParams from the wiki at https://github.com/omcljs/om/wiki/Quick-Start-%28om.next%29 is that you have to dynamically update the query params with a call to om/set-query!

mudphone22:12:29

That’s more of a component-level change to how data is read/filtered, I think

mudphone22:12:38

as opposed to how it is mutated

mudphone22:12:46

I’m just thinking out loud here

not-much-io22:12:27

Looking at set-query! in the docs, it actually makes sense. It was just used in the tutorial for hardcoding parameters it seemed. A sort of local state for the component maybe.

not-much-io22:12:57

also just thinking out loud

mudphone23:12:26

ah yes, interesting