Fork me on GitHub
#beginners
<
2018-03-12
>
yanko.vadim00:03:00

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. https://github.com/finomayato/ad-astra/pull/1

mathpunk01:03:43

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?

mathpunk01:03:29

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

mathpunk01:03:10

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

yanko.vadim01:03:14

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

mathpunk01:03:34

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

noisesmith01:03:10

and when-some - which specifically checks for nil

noisesmith01:03:43

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

yanko.vadim01:03:55

Thank you so much, guys!

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

mfikes01:03:56

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

drewverlee01:03:03

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

mfikes01:03:02

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

drewverlee01:03:06

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

mfikes01:03:15

I guess you can also write it as

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

mfikes01:03:08

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

drewverlee01: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

mfikes01:03:01

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

mfikes01:03:50

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

drewverlee01:03:57

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

drewverlee01:03:33

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

mfikes01:03:14

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

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

mfikes01:03:30

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

mfikes01:03:24

As in

(let [x [1 2 3]] `[:a :b [email protected] :c])

mfikes01:03:51

Or, perhaps the simplest:

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

noisesmith01:03:56

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

mathpunk01:03:54

@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

mathpunk01:03:05

this null is really surprising me

duminda04:03:30

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.

noisesmith04:03:11

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

noisesmith04:03:10

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

duminda04:03:04

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

sb09:03:37

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

sundarj09:03:12

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

sb09:03:45

@sundarj thanks, in this case “similar”!

petr.mensik09:03:11

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?

noisesmith15:03:10

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

noisesmith15:03:20

PROD=true java -jar uberjar.jar

petr.mensik19:03:24

Thanks for tip

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

drewverlee15:03:20

Thanks! Yea. I remember most things correctly.

russ76715: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. 😁

drewverlee16: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…

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

drewverlee16: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…

drewverlee16:03:44

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

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

russ76717: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?

drewverlee13: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)

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

matan15:03:40

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

matan15:03:43

can't say it was very encouraging though 😉

matan15:03:33

@joelsanchez thanks for pointing at data-specs!

timok17:03:46

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

timok17:03:22

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

mathpunk20:03:27

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

mfiano21:03:55

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

noisesmith23:03:38

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

noisesmith23:03:59

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