Fork me on GitHub
#beginners
<
2019-05-26
>
nmkip18:05:46

Why is it that when is a macro and not a function? Can be written both ways?

nmkip18:05:26

idk if this is a valid implementation. Macro implementation is a lot shorter

codonnell18:05:36

It's so that the body isn't evaluated if the predicate check fails.

codonnell18:05:47

Try this: (when false (throw (Exception. "Doesn't happen")))

nmkip18:05:49

Just like if

codonnell18:05:14

Exactly. The implementation of when actually just uses if.

nmkip18:05:43

Yes, I was reading it some minutes ago

nmkip18:05:52

ok, that makes sense

Nate Sutton18:05:17

given I have (.), how do I append the vector [1 2 3] such that I get (. [1 2 3])

Nate Sutton18:05:06

(cons '(.) [1 2 3]) results in (. 1 2 3)

Nate Sutton18:05:21

I'd use (cons '(.) '([1 2 3])) only [1 2 3] is actually a var

codonnell18:05:03

If you want (. [1 2 3]) from (.) and [1 2 3], you could do (concat '(.) (list [1 2 3])). Using cons gives the list (.) as the first element instead of just ..

codonnell18:05:48

(cons '. (list [1 2 3])) also works

Nate Sutton18:05:28

ahhh list is the bit I was missing out on, I think. I tried a bunch of these variations but list lets me reverence the vector at my-list and transform it into a list

Nate Sutton18:05:32

I was trying to figure out dynamic method calls on java objects like this:

(def my-list [1 2 3])
(def my-method 'count)
(eval (concat '(.) (list my-list) (list my-method)))
;; => 3

dangercoder19:05:06

I can't seem to truly grasp how others use namespaced keywords in practise and what the thought behind it is (or well I do, unique keys). Let's say I have a danger.core.login.specs namespace. If I want to construct maps with namespaced keywords I would have to require the danger.core.specs namespace with an alias everywhere I want to use it? e.g (:require [danger.core.login.specs :as entity]) and then ... ::entity/login-form`? Is that idiomatic?

valtteri19:05:07

There’s a shorthand for defining namespaced maps:

#:my-cool-ns.whatever {:name "example" :age 51}

valtteri19:05:21

No need to require. Keywords are just… keywords. 🙂

lilactown19:05:58

You have to require if you want the alias

lilactown19:05:37

I usually don't use the sugar unless I don't want my keyword being used outside of the namespace I'm defining it in

lilactown19:05:21

Sort of like naming something like ____thingThatMostConsumersShouldntNeed

lilactown19:05:16

I usually find it better to manually namespace things. But then they can get real long :slightly_frowning_face:

valtteri19:05:00

True that require is needed for aliasing. It would be weird to require a namespace only for aliasing keywords.

seancorfield19:05:15

require is not needed for aliasing.

seancorfield19:05:01

user=> (alias 'foo (create-ns 'bar.quux))
nil
user=> ::foo/x
:bar.quux/x
user=>   
That's what we use at work to provide aliases without requiring a namespace -- since our keywords don't need to match namespaces.

seancorfield19:05:27

Also, we use qualified keywords for partitioning keywords into "domain groups" so they can't conflict.

valtteri19:05:11

Good call, I had forgotten about alias.

seancorfield19:05:41

e.g., :api/country, :membership/transaction-id

dangercoder20:05:15

ah, cool. Thanks for the insight everyone!

lilactown20:05:19

Doesn't work in cljs tho :slightly_frowning_face:

dangercoder20:05:33

yeah I just tried it out in cljs, got the same issue

lilactown20:05:04

It's because the aliasing is tied to the ns actually existing as a file

dangercoder20:05:42

I'll go with the manual aliasing aka constructing maps like @valtteri mentioned for cljs. I really want my specs to guide me when I look at the code 6 months later 🙂.

vigilancetech20:05:03

why would I be getting "circular dependency detected" when I require/reload in cljs?

lilactown20:05:59

Have you required an ns that also requires the first ns?

kari20:05:29

I promised to give Clojurians some experiences regarding Purely Functional training web site (https://purelyfunctional.tv/). I have watched the first sessions in the "Repl-Driven Development in Clojure" series and it looks really promising. I really like Eric's teaching style. He gives nice background regarding topics he's teaching. And in the latest session I started to watch he explains the way he uses the Clojure REPL as part of his development process. You can clone the repository he is using as an example, and it's really nice to be able to pause the video and try the same expressions yourself in your own REPL session. I'll give you more experiences later. But already I can say that if you are an absolute beginner Eric can give you a quick start in your Clojure study path.

seancorfield21:05:06

I just finished watching the last few lessons last night. It's a great course -- and a subject that I've been very passionate and vocal about: learning a productive and instinctive REPL-based workflow.

johnj22:05:42

@seancorfield Is the db as a first arg to functions a personal preference or is there more to it? https://github.com/seancorfield/usermanager-example/blob/master/src/usermanager/model/user_manager.clj

papachan22:05:14

To run tests from CLJ command line its:

clj -C:test
or
clj -A:test
?

seancorfield22:05:31

@lockdown- I typically put arguments that change the least at the front -- so you could partial them. I try to do that with all my code.

johnj22:05:50

ah partial is a good reason

seancorfield22:05:56

@papachan -C will only bring in classpath-elements. Generally you'll want -R for resources (dependencies) and you may need -M for main options, for actually running tests. So -A:test makes more sense here: -R + -C + -M.

papachan22:05:23

Thanks @seancorfield it worked fine for me !

seancorfield22:05:08

@papachan -C:test would just add :extra-paths, so just the "test" path. In general tho' you're going to have test dependencies, such as test.check etc which would need a -R option for :extra-deps, and you may have a test runner involved that would need a -M option to bring in its :main-opts (hopefully that's clearer than my previous message).

vigilancetech23:05:07

@lilactown yes. Using this project: https://github.com/vigilancetech-com/shadow-hoplon-electron I compiled it (had to do npm install jquery) and connected to it with cider from the main.cljs file, switched over to it with , s N (which sends the ns declaration) then I tried just adding a def to the file (def foo "abc") and did (require 'app.main :reload) and get that circular error. In the repl I find that foo is unknown/null

lilactown23:05:26

There's a lot in that message. Not sure what's going wrong

lilactown23:05:21

Your cljs code by default hot reloads on file change so you shouldn't need to require it like thay

lilactown23:05:10

But also I'm not sure how you're connecting to the REPL, if you've successfully gotten into a CLJS repl or a Clojure repl

vigilancetech23:05:19

I use the cider-connect-cljs command