Fork me on GitHub
#beginners
<
2019-05-27
>
vigilancetech00:05:23

if I do (js/alert "hello") I get a popup in the electron window so it sounds to me like I'm in a cljs repl

Mitch00:05:17

I'm having trouble connecting to my postgresql database (getting a PSQLException: The ResultSet is closed.) and am at a loss for how to debug it.

Tensor00:05:52

I've had this issue before years ago when I tried to use my own JDBC wrapper. The likely problem is that the update is happening but its trying to return the rows in a lazy sequence. Whatever is trying to use that lazy sequence may be doing so after the connection has already closed. You could try using doall to ensure that it reads the data immediately rather than lazily.

Mitch00:05:15

Using clojure.java.jdbc and am not sure how to tell if a connection to the DB is failing or if I have some sort of mal-formed query

seancorfield01:05:37

@mitchell_clojure Feel free to share your code, either here or in #sql if you want a smaller audience focused on SQL stuff.

seancorfield01:05:09

I suspect you're doing something lazily and the connection is closing before you realize the result set sequence, based on that message.

Mitch01:05:30

Hey thanks for reaching out Sean. I think I coincidentally JUST figured out the issue as you wrote

Mitch01:05:18

the postgre driver was migrated from postgresql -> org.postgresql group ID

Mitch01:05:29

and so I was using a much older dependency version

seancorfield01:05:57

Oh, yeah, they had a massive version jump... up to 42.x.y.z I think ?

Mitch01:05:08

lol yeah, good memory

seancorfield01:05:44

Trust me, the quirkiness of several JDBC drivers is indelibly stamped on my poor ol' memory 😞

Mitch01:05:34

It's much appreciated!

felipebarros01:05:52

I was trying https://rigsomelight.com/figwheel-main-template/ out and wondered why the templates generate projects named first.second. I mean, I understand namespacing things inside the project, but why would the project itself default to that style instead of just first?

felipebarros01:05:28

Maybe it's just a small detail I shouldn't think too much about, but if it is a good practice I would like to know.

felipebarros01:05:59

Just to be clear my question is about the command clj -A:new figwheel-main hello-world.core generating a directory called hello-world.core instead of a project called hello-world with a src, hello-world and core.clj or whatever.

seancorfield01:05:17

Single segment namespaces are a bad idea. They map to segment Java packages (which I believe are going to be outlawed?).

seancorfield01:05:04

clj-new requires qualified project names like my-test/project or multi-segment projects like project.main.

seancorfield01:05:34

The top-level folder name isn't important -- you can easily rename that if you want. But the structure inside the project is important.

seancorfield01:05:56

Leiningen tried to "avoid" this problem by slapping .core on the end of every project name that was a single segment...

felipebarros01:05:11

Oh, perfect. I understand the importance inside the project, but was confused about the top-level directory naming.

seancorfield01:05:12

...but that left us with hundreds of projects that had a core.clj file 😞

seancorfield01:05:41

Feel free to open an issue on clj-new's GitHub repo. I've been annoyed by it too sometimes πŸ™‚

felipebarros01:05:02

Haha ok πŸ™‚ Thank you!

seancorfield01:05:02

If you use a qualified name, you get the project name you "expect"

[email protected]:~/clojure$ clj -A:new app something/project
Generating a project called project based on the 'app' template.
[email protected]:~/clojure$ tree project
project
β”œβ”€β”€ CHANGELOG.md
β”œβ”€β”€ LICENSE
β”œβ”€β”€ README.md
β”œβ”€β”€ deps.edn
β”œβ”€β”€ doc
β”‚   └── intro.md
β”œβ”€β”€ resources
β”œβ”€β”€ src
β”‚   └── something
β”‚       └── project.clj
└── test
    └── something
        └── project_test.clj

6 directories, 7 files

felipebarros01:05:29

In that case, shouldn't the top-level directory be something?

seancorfield01:05:13

No, because the assumption is you'd use fbarros/project-a and fbarros/project-b etc

seancorfield01:05:29

I can certainly improve the clj-new documentation...

felipebarros01:05:33

Hmm, that is still a bit confusing to me. Do you have a reading recommendation?

felipebarros01:05:00

On proper namespacing I mean.

felipebarros01:05:33

Wait, so you mean that the name of a projects main file shouldn't be main.clj or core.clj, but instead the name of the project itself?

felipebarros01:05:52

I thought namespacing was just about src/something/core.clj instead of src/core.clj.

seancorfield01:05:50

Having your default project entry point ending in core is an anti-pattern caused by Leiningen.

seancorfield01:05:47

There are two things at play here: the group/artifact ID used for deployment (to Clojars) and the namespace tree. If you're producing a library that other people use, you want your project's namespaces to not clash with anyone else's.

seancorfield01:05:04

You also want the group/artifact to not clash.

seancorfield01:05:24

So <my-unique-id>/<my-project-name> is really what you want every time.

seancorfield01:05:47

Yours would be fbarros/project, mine would be seancorfield/project. For example.

seancorfield01:05:09

Now we both have a project.clj file but the namespaces are different and could be used in the same project.

seancorfield01:05:16

Does that help?

felipebarros01:05:46

It does! It will take me a bit longer to wrap my head around it (coming from front-end development with index.html, index.js, etc) but it makes sense. I just had this misunderstanding because I thought namespacing problems were caused by having src/core.clj instead of src/project/core.clj. I thought the second version was fine. But now I see that you may have many things called project, so the spot for project there is more important and should be my name as a solo developer or the name of the company I'm working for maybe.

felipebarros01:05:32

Have I understood correctly?

seancorfield01:05:52

The namespace can be a.b.c. The main things are to a) avoid single-segment names and b) to create a name that likely won't conflict with other people's projects.

felipebarros01:05:31

Perfect. I get it now. Thank you again! πŸ˜„

seancorfield01:05:38

So you generally want something globally unique (so your projects won't clash with other people's) followed by something locally unique (so that project won't conflict with your other projects).

felipebarros01:05:14

Makes perfect sense.

felipebarros01:05:51

I dedicate an unhealthy amount of cognitive time to naming stuff so this will surely help me.

seancorfield01:05:01

Naming is hard.

seancorfield01:05:01

Some projects take liberties -- but unless you're pretty sure you're going to have something globally unique, you shouldn't.

seancorfield01:05:46

For example, expectations. It's a single segment namespace ("bad") with the same group/artifact ID (also a bit "bad").

seancorfield01:05:52

date-clj ("bad"). clojure.java-time (that's it's group/artifact name, which is doubly-bad for including clojure in it) and it's namespace is just java-time which is terrible.

seancorfield01:05:13

I just released seancorfield/next.jdbc which has a namespace structure next.jdbc.* which also breaks the rules (the group/artifact ID is good, but the namespace is less good). But it's a follow-on from org.clojure/java.jdbc and clojure.java.jdbc.* so I feel I can get away with it a bit πŸ™‚

felipebarros01:05:23

I have no knowledge of Java but I can see how distinguishing things named like that could be difficult.

seancorfield01:05:06

Java's heritage is why we get things like com.stuartsierra.component πŸ™‚

felipebarros01:05:07

What would be the ideal name if it wasn't a follow-on?

seancorfield02:05:16

Hard to say really. Only Clojure itself and Contrib projects should really have clojure in their namespace. But next.java.jdbc.* would probably have been a bit "safer"... but also a bit redundant so it's a trade off.

seancorfield02:05:54

I just hated the idea of using seancorfield.java.jdbc as the namespace structure. πŸ™‚ I just don't want my name all over other people's code like that.

felipebarros02:05:19

Your name is in our hearts and minds already haha but yeah, I get it. What about that com. or org.? I see that a lot.

seancorfield02:05:48

That always seems a bit pointless to me. In reality there are very few conflicts where the domain is the same but the TLD is different.

seancorfield02:05:40

Although is a firm of Australian lawyers, last I looked (I'm ).

seancorfield02:05:15

If you're an organization that has both .org and .com domains for open source and commercial stuff then, yeah, I can understand putting the TLD in as well... but even so... REBL has a namespace of cognitect.rebl.* so... πŸ™‚

πŸ˜… 1
felipebarros01:05:10

The official documentation on Namespaces is very terse.

seancorfield01:05:22

Which "official documentation"?

seancorfield01:05:10

Yeah, that first paragraph is... accurate but dense and not entirely helpful for beginners πŸ™‚

bartuka12:05:07

hi ppl, I have one function in my application that uses (all-ns) but I noticed that when I start the application, clojure does not load all namespaces by default (is that true?), because the (count (all-ns)) returns 379 and them when I use cider-load-all-project-ns the count does up to 439.

bartuka12:05:20

is that something that I'm missing on πŸ˜•

bartuka13:05:59

just found this library https://github.com/Raynes/bultitude and it worked great πŸ˜ƒ

JanisOlex19:05:59

Hi. I have a general question about idiomatic Clojure. I read somewhere (can't remember where) that having your whole application state to hold in one atom, and have all the functions to operate on that atom, is bad practice. But how then else you can operate... many atoms?

zlrth20:05:13

where did you see that @olekss.janis? in clojure, you’d most likely keep your application state in a database. when running clojurescript in the browser, i think it’s fine to have your state in an atom. that’s what re-frame does.

Roger Amorin Vieira20:05:51

Hi, my name is Roger, I'm new here and in clojure. I'm having troubles with a simple thing can someone help me? I have 2 maps element - {:id 1 :info "information"} key - {:key_name "id"} And i'm trying to get the id using the key name like that: (get element (get key :key_name)) And i'm get nil

Roger Amorin Vieira20:05:00

I'm also try in repl get with string like: (get element "id") but isnt work, I know the correct is with ':' but I cannot use it how can i fix this?

Roger Amorin Vieira20:05:11

I figure out, using function keyword

orestis06:05:52

That’s the one. πŸ‘ You might want to consider getting the data in the key map to be already a keyword.

πŸ‘ 1
Eric Ervin20:05:46

@olekss.janis It'll depend on the kind of application. I feel like I learned the idiom of putting the universe in a map in an atom from the Clojure community.

bartuka20:05:44

yeah, the problem with namespaces continues. The function all-ns only returns previouesly loaded namespaces. In my application I want to dynamically search for a specific metadata on functions definitions, but for that I would need to get access to all namespaces in the project. One option is to use the library https://github.com/Raynes/bultitude and perform a require on each symbol returned by (b/namespaces-on-classpath).. but I find this slow... looks like a hack, is it?

seancorfield21:05:52

@iagwanderson If you want to access metadata on symbols that have not yet been loaded by your application, your only real choice is to issue a require for any corresponding namespaces that have not yet been loaded.

seancorfield21:05:38

Is the metadata you want specific to your application or could you find it on any function in any library that your app might reference?

bartuka22:05:02

@seancorfield yes, the metadata is specific to my application.

seancorfield22:05:08

@iagwanderson So you'd only need to require those namespaces that are "inside" your application and that have not already been loaded -- that's a much smaller set of files, I suspect, and shouldn't take long to do -- since "most" namespaces would already be loaded. You could probably use clojure.tools.namespace to get that list fairly easily... I'm trying to remember what we did for source-based analysis...

bartuka22:05:05

I endup using the library bultitude cited above, and used a simple (map require my-namespaces)

bartuka22:05:46

and it worked, but I was thinking if there aren't a better way to do so

seancorfield22:05:41

Given that you want require for side-effects only, you want run! not map -- and as long as my-namespaces is just that small set of namespaces you haven't already loaded, that's probably fine.

bartuka22:05:59

I changed my (doall (map ..)) to use run! thanks