Fork me on GitHub
#beginners
<
2019-12-03
>
leo07:12:15

i've been banging my head against this bizarre error message for a while now, wondering if i can get some help:

(defn directions-to-lines [directions]
  ((reduce reduce-directions {:lines () :position [0 0]} directions)) :lines)
when i evaluate this function (directions-to-lines wire) i get Wrong number of args (0) passed to: clojure.lang.PersistentArrayMap class clojure.lang.ArityException, but when i evaluate the body directly:
((reduce reduce-directions {:lines () :position [0 0]} wire)) :lines)
the expression evaluates to the expected result. any ideas? i figured it out. my parentheses were mis-matched.

facepalm 8
Edward Hughes12:12:08

Happens to the best of us. Are you using an editor with paren-matching?

hiredman07:12:29

Check your parents, those are not the same expressions

👍 4
Emanuele10:12:47

Hello, stupid question: how do I do nested loops? I need a classic

for i = 0; i< 10; i++
for j = 0; j< 10; j++

Emanuele10:12:36

it’s a loop with two recurs?

Emanuele10:12:02

no wait, it’s an if over two recurs??

Emanuele10:12:18

that’s curious…also counterintuitive 😄

ben11:12:13

Do you absolutely need a loop? If you just need the i, j pairs, clojure.math.combinatorics/cartesian-product can get you all combinations of i, and j, which you can map a function (your inner loop body) over. Feels a bit more idiomatic to me, but I am equally beginner

Emanuele11:12:50

I need to basically brute force all the combination until I found what I need.

Emanuele11:12:19

cartesian-product is lazy?

ben11:12:28

I think so, yeah

Emanuele13:12:24

that’s a nice solution than, I will try. Thanks.

Alex Miller (Clojure team)10:12:25

Nested dotines is prob easiest

Alex Miller (Clojure team)10:12:45

But it depends if you are making something (then use for)or doing side effects

skykanin11:12:16

How do I run a single clojure file from the CLI if I have a -main function? The filename is manhattan.clj and ns is manhattan.core but running clj manhattan.clj doesn't seem to be working when I just do a println in -main . It's not printing anything to the console.

borkdude11:12:13

@nicholas.jaunsen main methods can be executed with the -m option, but I think the file has to be on the classpath (typically in the src dir)

skykanin11:12:15

Ah, I just imported clojure.main and passed my entry point to the main function from that namespace

Eamonn Sullivan12:12:22

Hi all, I'm just getting started on Clojure/ClojureScript for a small-ish full-stack web app for my colleagues. After randomly stumbling around, I came up with a luminusweb/re-frame/graphql foundation and it seems to work OK (so far). However, I'm blown away by spec (I'm a Scala guy, usually) and I'd like to use that, in hopefully the right way from the start. Can anyone suggest any similar full-stack apps on github or elsewhere that I could study, preferably using spec in a non-trivial way?

herald12:12:41

Not a codebase like you're looking for, but this is a useful reference on the ways you can use spec. https://corfield.org/blog/2019/09/13/using-spec/

👍 8
thanks3 4
Eamonn Sullivan15:12:03

I still have a load of really basic questions, such as where do people generally put the specs? In their own specs/ directory (separate from src/, for example)? Do you put all of the specs in one place, under one ns, or scattered about, together with modules you are spec-ing?

solf15:12:27

That's also a question I also have. From what I read it's in src/, either together in a specific ns or close to the functions they instrument

seancorfield16:12:17

It depends. And that article answers that question as I recall?

Eamonn Sullivan16:12:49

Thanks. Yes, you did mention that you put the specs and predicates under a separate namespace. In src, I assume? I missed that the first time.

seancorfield16:12:03

All source code goes under src so it is on the classpath. Test code goes under test and is added to the classpath for when you run tests.

athomasoriginal03:12:14

An alternative option (which I find helpful) is to co-locate your specs with the ns they pertain to. e.g. if your folder structure was

src/
  app.clj
  route.clj
  todo.clj
I would put todo specific specs into the todo.clj file and if there are specs that don’t fit into any of the namespaces themselves, I move them to app…but this is a guideline and it really depends on your domain and app structure.

athomasoriginal03:12:23

Another thing to note is I do not opt to spec everything, but rather, the edges of the application. For example, maybe only the public API fns would be spec’d or important implementation details.

skykanin16:12:46

(def pos
  {:u inc :d dec
   :r inc :l dec})

(defn path-inc [cord [k v]]
  (let [dir (if (contains? #{:u :d} k) 1 0)
        choose #(update % dir (pos k))]    
    (take v (rest (iterate choose cord)))))

(defn trace-line [start dirs]
  (let [llast #(last (last %))
        reducer (fn [acc dir]
                  (conj acc (path-inc (llast acc) dir)))]
    (->> dirs
         (reduce reducer [[start]])
         flatten
         (partition 2)
         rest
         set)))

skykanin16:12:38

having a weird issue with the trace-line function. For some reason it's returning a list of tuples and not a set of tuples even though I'm calling set in thread last macro

andy.fingerhut16:12:16

That seems difficult to understand given the code that you show. How certain are you that the code you show matches the code you are running? e.g. could you be running code older than what you have in your editor? Tried saving all files, starting a fresh REPL, and loading your code again?

skykanin16:12:31

ok, I quit the REPL and restarted it that fixed the issue. For some reason it wasn't updating the function def even though I reloaded the code several times

andy.fingerhut16:12:39

There are many many ways to reload code, depending upon editor /environment setup, so difficult for me to guess what might have been going wrong there.

eval-on-point16:12:38

In shadow-cljs, is there a way to re-enter the cljs-repl after leaving it with :cljs/quit ?

thheller16:12:52

just start the CLJS repl again

dpsutton16:12:18

@mitchell_clojure how did you start it in the first place?

eval-on-point17:12:57

just by running (shadow/repl :app) from the shadow nREPL server

eval-on-point17:12:23

running :cljs/quit takes me out to the user namespace and I am a little unclear on if the cljs repl persists or is reachable from there

aisamu17:12:24

Just note that when you exit it with :cljs/quit you're left in a namespace that doesn't contain the shadow alias

eval-on-point17:12:32

oh okay, so is it preferable to have two separate repls running?

aisamu17:12:39

IIRC (shadow.cljs.devtools.api/nrepl-select :app) would work there

eval-on-point01:12:12

following up to say this just worked! thanks again

🙂 4
eval-on-point17:12:17

oh awesome, that looks like exactly what I was looking for. will verify later when I get home! thank you very much!

simple_smile 4
Mehdi H.17:12:01

Hi everyone! I can't seem to be able to catch errors thrown using Pedestal. I have a transaction function to Datomic that throws a clojure.lang.ExceptionInfo exception in the datomic on-prem transactor, which, according to Datomic documention, should also be thrown in the peer, so in my case through the database interceptor that tries to transact the faulty data. None of my interceptors have an :error function, but I have used io.pedestal.interceptor.error/error-dispatch to catch the errors at the end of the process. It does not seem to work. • Does an interceptor need its :enter function to be used on the request to have its :error function used on the way out? • If so what is the proper way to user error-dispatch (the resulting interceptor is the first in the route) • any other thoughts as to why I am not catching it (it's a POST request and it returns 201 created even if it didn't happen...) Thanks!

Mehdi H.11:12:07

Nevermind, I found the issue in the code.

acim121:12:08

Is there a convenient way to use cider-load-buffer without having an associated file? I just have a scratch buffer and I jacked into cider and I want to load the buffer into the REPL and play without without having a file on disk, but trying to load the buffer gives "Buffer 'my-buffer' is not associated with a file"

andy.fingerhut22:12:18

I do not know the answer but wanted to point out that there is a #cider channel

👍 4