Fork me on GitHub
#beginners
<
2016-04-14
>
mrwhite10:04:17

Hello! Is it correct way return map with f and sql? (defn- do-with-try-catch [f & sql] (try ( {:f (f) :sql sql} ) ; <<<<<——— this is correct? (catch java.sql.SQLException e (jdbc/print-sql-exception-chain e) (let [^String message (or (->> (.getMessage e) (re-find #"(?s)(^.*)\s+\[[\d-]+\]$") second) (.getMessage e))] (throw (Exception. message)))))) (defn process-structured "Convert QUERY into a korma select form, execute it, and annotate the results." [driver {{:keys [source-table]} :query, database :database, settings :settings, :as outer-query}] (let [timezone (:report-timezone settings) entity ((resolve 'metabase.driver.generic-sql/korma-entity) database source-table) korma-form (build-korma-form driver outer-query entity) f (partial k/exec korma-form) f (fn [] (kdb/with-db (:db entity) (if (seq timezone) (do-with-timezone driver timezone f) (f))))] (log/debug (u/format-color 'blue "\nSQL: 😈\n%s\n" (get-sql korma-form))) ;(log-korma-form korma-form) (do-with-try-catch f, (get-sql korma-form)))) When I try to destructuring , I got null (let [{driver-process-query :f sql :sql} (partial (if (structured-query? query) ; <<- driver-process-query, sql are null driver/process-structured driver/process-native) driver)]

sveri11:04:09

@mrwhite: Hi, "rest" arguments of a function have to be passed in to a vec like this: do-with-try-catch [f & [sql]] Depending on what sql is you will have to call it too in your map: {:f (f) :sql (sql)} . But, tbh, I dont know what the purpose of that map is: {:f (f) :sql sql}. It is not really making sense to me being wrapped into a SQLException, but maybe it is because I am lacking context.

sveri11:04:41

Other than that you can add snippets here by pressing the + plutton on the left of the input field, it adds syntax highlighting to your code simple_smile

mrwhite12:04:07

@sveri: thank you buddy

sveri12:04:14

@mrwhite: np, your welcome

mrwhite16:04:37

Oh, peeps %) Java nothing after Clojure xDDD

akiva18:04:53

@mrwhite: Or you can use a single backtick for inline code and triple backticks for

multiple
lines of 
code.

akiva18:04:31

Also, I wouldn’t force rest arguments to be passed as a vector unless you have good reason for it. I’d rather have a slightly more complex function that handles all comers rather than forcing callers to adhere to an expectation.

cory20:04:50

Hi everyone, I’m blowing my mind trying to understand macros. Could someone explain what is happening here?

(defmacro my-macro
  ([] '())
  ([x & next] (cons x (apply my-macro next))))

(macroexpand '(my-macro a b c))

;; expected => (c b a)
;; actual => (a)

akiva20:04:25

Oh, I see you already have, @bojan.matic!

bojan.matic20:04:06

all i can contribute at this point is that you want your macro to look like this

bojan.matic20:04:25

(defmacro my-macro
  ([& args] (reverse args)))

bronsa20:04:21

@cory: you can't apply macros

bojan.matic20:04:58

that was my first thought as well, but then why doesn’t that blow up?

bronsa20:04:44

it's an unfortunate implementation detail

bronsa20:04:10

from within the macro definition, the macro itself is not yet a macro and still a function

arrdem20:04:52

Why would you want a macro that applies itself rather than just returning a transform of itself.

cory20:04:59

This answers my real question, but now I’m curious why you can’t call apply on a macro if the macro is a function from the point of view of the macro.

cory20:04:09

(It is likely that my understanding of macros is too simplistic)

robincarlo8420:04:43

hi guys, just starting with clojurescript and really need to clarify something.

robincarlo8420:04:54

If I'm making a multiple pages, does that mean I will have also a multiple .cljs? e.g. index.cljs, about.cljs, profile.cljs?

bronsa20:04:01

@cory: well you technically can, if you provide the two implicit args that macros have (&env and &form), so you could technically write

(defmacro my-macro
  ([] '())
  ([x & next] (cons x (apply my-macro nil nil next))))

bronsa20:04:26

but this is wrong in so many different levels and you really don't want to do this

bronsa20:04:09

usually if you feel the need to recur through a macro, just delegate that to a function

bronsa20:04:32

(defmacro my-macro [& args] (apply some-processing-fn-that-can-recur args))

arrdem20:04:04

Implementing macros by delegating to rewrite functions is considered better practice by some anyway.

cory20:04:53

Thanks a ton for humoring me. So in my example, what is actually happening to produce (a)? It is consing a to (apply my-macro ‘(b c)), which is ending up as effectively (my-macro b c) and those are being treated as those implicit arguments?

cory20:04:02

Awesome, thanks again. I will definitely not do this in real life.

bojan.matic21:04:27

this is an interesting gotcha

bojan.matic21:04:06

is it mentioned anywhere in some docs? how discoverable is this?

lsenta21:04:52

Hi there, I'm stuck trying to use multimethods defaults:

(defmulti to-home (fn [page] [(:app page) (:module page)]))

(defmethod to-home [:a :b] [page]
  :first)

(defmethod to-home [:a :default] [page]
  :second)

(defmethod to-home :default [page]
  :default)

(println (to-home {:app :a :module :x})) ; => :default
Any idea what to write to dispatch on [:some-value *] ?

lsenta21:04:52

So that (to-home {:app :a :module :x}) calls the :second implementation

lsenta22:04:34

Got it! I used (derive ::x ::default)