Fork me on GitHub
#beginners
<
2022-11-10
>
fenton03:11:03

How do you organize your projects? src, test, dev, resources, dev-resources as top-level dirs? When you run your unit tests do you allow them to see code in dev? How should one use dev? Currently I’m working with some guys who are liberally including dev and dev-resources files in tests. I was under the impression what happened in dev was more of a playground rather than a place for test utilities (code to help run tests) to live. Thoughts?

dpsutton05:11:01

discover your own conventions. see if you regret it later. better to just think of classpath roots. your test alias will include whatever classpath roots it needs for its purposes. does stand to reason why is dev-resources and test separate. but no harm in any of this stuff if you just realize that aliases just conditionally add folders to the classpath. then the names are basically irrelevant

Patrick06:11:28

Hi I have what may be a noob kit-clj question. I'm trying to implement the guestbook with hiccup/htmx instead of Selmer and I keep getting an error that I'm just not sure about. In my system.edn I have

:reitit.routes/ui {:base-path "",
                    :query-fn #ig/ref :db.sql/query-fn
                    :env #ig/ref :system/env}
(which is properly configured afaik) and my web/routes/ui.clj file has these lines so I thought it would work.
(derive :reitit.routes/ui :reitit/routes)

(defmethod ig/init-key :reitit.routes/ui
  [_ {:keys [base-path]
      :or   {base-path ""}
      :as   opts}]
  [base-path (route-data opts) (ui-routes opts)])
I keep getting "Unable to resolve symbol query-fn in this context" when I call query-fn in we/routes/ui.clj. Any help is appreciated!

lispyclouds09:11:32

maybe asking in #C02T4GSBSJ1 helps more too? 🙂

Apple13:11:46

can you share more code?

Patrick15:11:29

I've gone ahead and placed the project into a public repo here https://github.com/pkingstonxyz/pkingstonxyz-clj

Apple17:11:18

oh no you didnt have to share the whole thing. see https://kit-clj.github.io/docs/guestbook.html for correct usage of query-fn

Apple17:11:57

so you just need to add one let block or something to the same effect

Patrick18:11:59

@UP82LQR9N Thank you so much! It works now :)

littleli10:11:15

Hello teachers of Clojure. I have a question as someone who already wrote some code even though I don't feel compelled to call myself proficient dev in Clojure. The question is what's a recommended way to learn writing macros in Clojure? I feel like resources on how to start writing macros, understand them and avoid pitfalls are pretty sparse. That said, macros is an unconquered bastion currently at my Clojure journey and I want to overcome this.

👍 1
1
Lennart Buit10:11:27

As a high level overview

walterl10:11:58

Also see https://clojurians.slack.com/archives/C053AK3F9/p1668019928420969 informative thread from yesterday about best practices, particularly when (not) to use macros.

👍 3
practicalli-johnny12:11:34

First rule of Macro Club.... Don't write macro's. (especially if you haven't spent days/weeks of hammock time considering this choice) I have been working commercially with Clojure since 2017 and have yet to feel the need to write a macro. If I end my career never putting a Clojure Macro I have written into production I will be very pleased. I feel writing macros really should be one of last things to learn (unless there is an overwhelmingly value in using a macro - it's almost always better to write a function).

👍 2
Fredrik12:11:55

When learning macros in Lisp I got a great deal out of On Lisp by Peter Seibel, which has a https://gigamonkeys.com/book/macros-defining-your-own.html which could be helpful to you. While macros are often used as utility (`with-open`,`doto` etc) and control flow, I think one reason they're not used to extend syntax as often as they do in Common Lisp, is that Clojure has data literals, so instead of writing macro manipulating syntax one writes functions manipulating data. Compare Hiccup with https://github.com/ruricolist/spinneret, for example.

👍 1
littleli12:11:23

@U05254DQM I agree with the spirit of this wholeheartedly. However it's also fair to say that some usage of macros can actually capture, and enforce, certain aspects mostly control flow pretty well.

practicalli-johnny13:11:00

@UBLU3FQRZ I would be very interested to see examples of macros that provide value (outside of Clojure core of course), especially if they are accompanied by reasons why they cant be done as effectively with functions.

Noah13:11:44

Hi! I am using Calva and VSC for coding in Clojure and my language server seems to be unable to run. As you can see, the icon for the clojure language server is perpetually spinning. I tried reproducing the error with a different project, but I get this issue only for one specific repository. I tried deleting and recloning the repo but the issue persist. Any ideas on what could be the reason for this problem? Thanks in advance!

pez14:11:01

Very interesting... Do you have a clue, @UKFSJSM38?

ericdallo14:11:23

That's intended! clojure-lsp won't start if it cannot get a valid classpath, from the error message it's not finding clojure , probably not available on your VSCode 's PATH

Noah14:11:36

How can I make it find the class path?

ericdallo14:11:14

good question 😅 don't know that much of vscode

pez14:11:17

Why would it work with other projects, but not this one? Anyway, if you have clojure in the PATH when using a terminal, you can start code from there and it will make VS Code have the same PATH.

ericdallo14:11:33

other may be lein projects and lein vscode is finding for example

ericdallo14:11:45

but yeah, odd if other are clojure projects as well

pez14:11:08

We'll find out, I hope. 😃

Noah15:11:02

Weirdest thing happened.. It just suddenly started working

pez15:11:50

That's cool, but also means we might not find out. 😃

Noah15:11:28

OK no I restarted and Its not working again xD

pez15:11:30

Ar you on a Mac?

Noah15:11:48

Monterey 12.6.1

pez15:11:49

Tried starting code from a terminal?

pez15:11:52

You might need to run the command Install command line code or whatever it is named in VS Code first.

pez15:11:08

But you want that. It is a power command!

ericdallo15:11:16

Are you sure you didn't press Ignore button on that classpath warning? if so, you may find some features not working

Noah15:11:57

Tried starting code from terminal, but the error persits.

Noah15:11:15

I pressed on retry

👍 1
ericdallo15:11:38

Yeah, at least on emacs starting from terminal helps too, weird

pez15:11:24

That doesn't look right. Is this a deps.edn or a Leiningen project?

pez15:11:55

Do you have clojure when you try in the terminal?

Noah15:11:59

deps.edn

ericdallo15:11:28

so you should run clojure -A:dev:test -Spath which is what clojure-lsp is trying to run

ericdallo15:11:41

so you don't have clojure installed at all, you need to install it

pez15:11:04

Ah, yes. For clojure-lsp to work you need that. The other features in Calva can cope without it, though.

Noah15:11:03

You guys were absolutely right

Noah15:11:34

So I am new to Clojure and taking a university course in functional programming and I did some math problems in Project Euler in order to practice

Noah15:11:44

Turns out those were lein projects

Noah15:11:52

and this university project is deps.edn

pez15:11:55

I should update the http://calva.io docs to early instruct to install clojure.

Noah15:11:05

so they are two different things?

Noah15:11:13

deps and lein

pez15:11:44

They are two different ways to start and manage your Clojure app, yes.

ericdallo15:11:15

it's like maven vs gradle for Java

pez15:11:49

Some projects actually use both, but anyway. 😃

Noah15:11:15

I think the auto formatting is kind off weak, or is it just me?

pez15:11:46

Depends on what you expect. Please start a convo about that in #CBE668G4R if you like.

Noah15:11:46

Yes, ofc! Sorry.

pez15:11:57

No worries. I mainly thought it could interest the #CBE668G4R community to see what you expect as a beginner.

Jim Strieter15:11:50

Is the following O(1):

(into [] (rest (conj some-vector 42)))
If not, what is an O(1) way to do the same thing?

ghadi15:11:32

pouring N elements into a vector is always O(N)

Jim Strieter13:11:59

That's what I thought. Thanks!

Jim Strieter13:11:48

Yes. @U03L0CR870T’s suggested is exactly what I need. I needed an O(1) way to make a FIFO buffer.

ghadi15:11:54

if you want to replace the first item in a vec: (assoc v 0 item)

Brennan C.15:11:44

You may be looking for https://clojuredocs.org/clojure.core/subvec which will let you do (conj (subvec some-vector 1) 42)

🙌 1
Alex Miller (Clojure team)15:11:59

what are you actually trying to do? make a queue?

🙌 1
cddr15:11:49

Is there a way to pass args to a java class as if by apply e.g. equiv of the following (which doesn’t work as I hoped) (apply Foo. [foo-arg1 foo-arg2])

delaguardo16:11:55

the best solution imho is to define “constructor” function and call apply with it.

(defn newFoo [arg1 arg2] (Foo. arg1 arg2))
(apply newFoo [foo-arg1 foo-arg2])

👍 1
Benjamin17:11:24

(map #(assoc % :bar (get-bar %)) foos)
is this not sort of a common pattern? Is this already the most succinct way to express this? I have a seq of maps and want to augment each with a :bar

Apple17:11:15

this wont compile

Benjamin17:11:15

there was a % to little yes

ghadi17:11:37

in general doing a thing to all items in a collection is map , that's the common pattern. in specific, deriving info is assoc + some function of the item

ghadi17:11:21

once I wrote a helper (label f :attr) that returned a function, but I thought it was less clear than the ordinary assoc+f

Benjamin17:11:53

yea now that you say the alternative I start seeing how assoc just works

Benjamin17:11:07

it's literally 2-3 characters more