This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-12-25
Channels
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?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?
Is it possible to nest one shorthand anonymous function literal e.g. #(println %)
in another?
@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...
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 #
.
'[(todos/add {:title ~title})]
I want to pass title as its value but I get (clojure.core.unquote title)
Am I doing something silly?(let [title "hello"]
`[(todos/add {:title ~title})])
=> [(todos/add {:title "hello"})]
see “syntax-quote” in: http://clojure.org/reader
oh, so the wrong type of quote?
alright, damn... Thanks!
Yup, was going well until started making queries and the quote tripped me 😄
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 (')
No not really, because a mutation query has to be used with the tick (') and as I understand it cannot be escaped?
with om/IQueryParams or straight?
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)})
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 😅
That's what I get for depending too much on Cursive. But thanks alot, it worked, finally!
I insert the double back quotes, then switch it off, then erase one, then switch it back on
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!
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.
also just thinking out loud