Fork me on GitHub

hi there! I am not sure that this is a right place for asking that. But, I’ve started to learn clojure a week ago and want to ask you guys to review my PR. Nothing complicated there. I’d like to hear any issues with style or more elegant ways to do something. Will appreciate your help.


I have a hunch there is a built-in macro you could use instead of (if (nil? commands) nil (do.... Maybe take a look at when-let or if-let?


Also, there's a lein plugin called kibit. You can run it on a codebase, and it will analyze your code to try to suggest idiomatic changes


@U9N3MM3H8 ^ in general, the when forms are for doing a do form but only under certain conditions, or after making a binding


@mathpunk didn’t get the last one. What do you mean by “when forms”?


oh like, when, when-let, when-not (if there is one)


and when-some - which specifically checks for nil


(if (nil? x) nil ...) directly translates to (when-some x ...) - they will compile to the same byte code


Thank you so much, guys!

Drew Verlee01:03:02

Is there a way to pass a function to another function (or macro) that contains a macro? Maybe my specific example will make this more clear. I’m trying to make this work: (.... foo [f] (meta #'f)) but i’m a bit rusty on macros and i’m having trouble understanding my options.


(defmacro foo [f] `(meta (var ~f)))

Drew Verlee01:03:03

I’m guessing because the meta macro happens at compile time i can’t do this


Sorry, got it right now... slack markdown problems.

Drew Verlee01:03:06

This works! Thanks. Huh, i must not understand something about how macros work.


I guess you can also write it as

(defmacro foo [f] `(meta #'~f))


Well, if you macroexpand it you can see it turns into what you want. But is there some aspect that is not clear?

Drew Verlee01:03:50

1) right so defmacro stops evaluation of the passed arg 2) ` means dont eval anything inside this until returned 3) ~ means execpt eval this? argh. i need to re-read how everything works it seems


(By the way, foo can't be written as a function; if so f would be a function object, associated with 0, 1 or many vars, and you can't find out inside the function)


Yes, I have read Colin Jones's book once a year at least for the past several years.

Drew Verlee01:03:57

thanks again. No need to try to type out an explanation i can find stuff :0

Drew Verlee01:03:33

kk. I have all the books, so i’ll just refresh my memory.


@drewverlee The syntax quote stuff prevents the need for writing harsh stuff like

(defmacro foo [f] (list 'meta (list 'var f)))


Perhaps understanding of syntax quote also involves realizing that it works outside of macros. Hrm.


As in

(let [x [1 2 3]] `[:a :b ~@x :c])


Or, perhaps the simplest:

which gets turned into user/x if you are in the user namespace.


@mathpunk the ring wrap-params middleware will put the request body into a key on the request - but even without that you can find the body in that request map. Recall that the req is a hash map, you can look at what's stored under the keys, and you can make middleware that puts things under new keys.


@noisesmith hmmm see that's what I thought, and so I expected, if I just put req into my response I'd see a map with my test data inside, and infer the structure from it


this null is really surprising me


I’m doing this inside some cljs code:

(set! (-> @chart .-data .-datasets (aget 0) .-data) (clj->js (reverse temps)))
I’m getting errors sometimes about array access now because sometimes there’s no elements in datasets Can I convert this using cljs-oops like this?
(oset! @chart "!data.!datasets.!0.!data" (clj->js (reverse temps))
I didn’t see anything relating to array access in cljs-oops.


@duminda one option is to use some-> which short circuits if any value is nil


or do you want to create index 0 in .-datasets if it isn't there?


@noisesmith: yes I want to add element if it’s not already there.


(.. (OctetSequenceKey$Builder. k)
         (keyID kid)
         (algorithm (or alg alg-a128kw))
         (keyUse (KeyUse/ENCRYPTION))
         (toSecretKey "AES")))
is that similar like at Clojurescript interop? or exactly what means the two dot?


@sb .. is the interop equivalent of ->: (.. o foo bar) expands to (. (. o foo) bar)


@sundarj thanks, in this case “similar”!


How to correctly distinguish prod and dev environment when running web application via uberjar? I am using Environ for setting the dev and prod property and I have a profiles like this (no vars are being set)

:profiles {:uberjar {:aot :all}
                  :env (:prod "true}
             :test {:dependencies [[midje "1.9.0-alpha5"]]
                    :plugins [[lein-midje "3.2.1"]]}
             :dev {:env {:dev "true"}}}}
Howeover then I realized that project has probably no way how to distinguish lein run vs java -jar uberjar.jar when running the project, so what is the correct way of setting those?


environ picks up environment variables, you can set them on the command line, no lein logic should execute on your server, it's a build tool


PROD=true java -jar uberjar.jar


Thanks for tip

Russ Olsen14:03:42

@drewverlee Macros are certainly a slippery concept. I have to remind myself over and over: Macros run as the code is being compiled. When you pass an argument to a macro the macro sees the argument more or less as you wrote it in the source (that's all it has at compile time). The macro takes its arguments and does its thing and returns the code that should be compiled. The syntax quoting thing ` is a separate feature, but one that is really handy for writing macros. Syntax quoting is a lot like HTML templating in a we application.

Drew Verlee15:03:20

Thanks! Yea. I remember most things correctly.

Russ Olsen15:03:00

I suppose there is one other thing about macros: Most of the time that you think you need a macro you don't. 😁

Drew Verlee16:03:17

In retrospect, i need a macro to do what i’m doing. But what i’m doing might have questionable value in what it achieves…

Drew Verlee16:03:49

I was trying to write a lib that given a set of functions, make them available at the command line. I’m starting to question why anyone would ever need a command line program if they have a REPL though.

Drew Verlee16:03:29

I suppose, the user might not now clojure, but imo knowing basic clojure syntax is actually much easier then some of the syntax i see for cli programs…

Drew Verlee16:03:44

I suppose if your chaining together programs at the cli? But maybe you should stop doing that.

Russ Olsen17:03:15

So I'm kind of a fan of command line programs that don't require me know know anything about the underlying programming language, but maybe that's just me.

Russ Olsen17:03:13

But I'm not sure you would need a macro to do that - couldn't you just deal with the functions in question as functional values along with a description of their arguments?

Drew Verlee13:03:14

My reasoning was that i need to call meta to get information about the function (e.g name) and it’s a macro itself so if i don’t stop it from evaling at compile time it will fail as the function var hasn’t been resolved defn expose [f] (meta f)

Drew Verlee13:03:21

>So I’m kind of a fan of command line programs that don’t require me know know anything about the underlying programming language, but maybe that’s just me. In general i agree, but I haven’t used a sufficiently complicated cli program yet that wasn’t more complex then clojures syntax. For me, the reason it doesn’t have enough value is because its dependent on clojure and you have to use clojure syntax, so why wouldn’t you just use the repl for something simple.


@noisesmith @lee.justin.m @joelsanchez thanks for the resonating yesterday, and also the practical tips!


can't say it was very encouraging though 😉


@joelsanchez thanks for pointing at data-specs!


oh no embarrassing...I think I found my mistake


Fixed it


so the print shows up when reentering the map from another page but it is not updating


I thought I'd just start examining what req looked like as I learned how compojure works, but I'm stumped

Michael Fiano21:03:55

Hello. I'm a Clojure newbie and was wondering if anyone here has used both Clojure and Common Lisp extensively?


depends on your definition of "extensively" in my case (years of clojure at work, vs. I made a few toy things to learn common lisp) - but if you have specific questions there's likely someone who can answer


I did learn common lisp (and scheme) before clojure though