Fork me on GitHub
#beginners
<
2023-07-27
>
stopa16:07:05

Curious question. Just today my working project started to error out with:

Syntax error (ClassNotFoundException) compiling at (instant/scripts/analytics.clj:1:1).
instant.scripts.analytics
Looking at analytics.clj, I realized I had leftover :gen-class, but no -main function. I removed gen-class , and things are working fine.
(ns instant.scripts.analytics
  (:require
    ...)
  (:import
   (java.time LocalDate Period Instant))
  (:gen-class))
What puzzles me though is, why did it start throwing an error on my machine now? Maybe something quietly updated?

hiredman17:07:17

gen-class without a -main doesn't cause that error

hiredman17:07:47

that is the kind of error you get when you requiring the namespace foo.bar but the file that lives at foo/bar.clj has the namespace foo.baz

hiredman17:07:41

ah, no, not that either, it really looks like a syntax error in your file

hiredman17:07:09

Clojure 1.11.1
user=> instant.scripts.analytics
Syntax error (ClassNotFoundException) compiling at (REPL:0:0).
instant.scripts.analytics
user=> 

hiredman17:07:02

so I would check to make sure the source code you are editing is actually what is being loaded (you aren't working on a different second checkout, don't have another file with the same name on the classpath, etc)

Kevin Hovsäter21:07:22

Yesterday I started learning Clojure and I just wrote my first program solving Advent of Code 2022, day 1. I'd love some feedback on my https://gist.github.com/hovsater/b3c8bb0774a4f2a1c364c635027a631b. What can I improve?

Kevin Hovsäter21:07:30

Full disclosure: I've done programming in other functional languages such as Elm and OCaml.

bzmariano22:07:42

Nice and simple, I like it. I've been learning Clojure slowly for a couple of months now, so I can't provide much feedback, sorry. I remember that I overcomplicated my solution by using 'split-lines,' which gave me empty strings. Then, I had to use 'partition-by blank?' to group the values. So... yeah, I prefer your approach.

Bob B22:07:42

day 1 is often a straightforward enough problem that there isn't a ton of nuance to critique - I happened to be focusing on generalizing in the 2022 AOC, so not necessarily an improvement, but a thing that might be interesting to play with, is to solve both with one function that takes a number of elves

pppaul02:07:58

i guess it would help to have the question and expected answer (test) in the example code. i have no idea what is going on

pppaul02:07:46

(defn parse-input [s]
  (->>
    (str/split s #"\n\n")
    (map #(->> (str/split % #"\n") (map parse-long) (reduce +)))))
put threaded transformations on new lines to improve readability

1
pppaul03:07:55

if my map function is complicated, i like to give it a name (defn). it's easier to tag with a debugger, and it's easier to quickly right some tests against it (when a map -> map fails then you have to write more code to debug/test)

pppaul03:07:53

(defn parse-input [s]
  (->>
    (str/split s #"\n\n")
    (map #(->> (str/split % #"\n")
               (map parse-long)
               (reduce +)))))
i ran you code, and i was surprised that this function didn't just parse the input. it turned all the lines of the input into a single number.

pppaul03:07:54

if parse-input always returns a single number (wrapped in a vector for some reason), why is the code for part-1 and part-2 so complicated?

pppaul03:07:35

when i run your sample data on part-1 and part-2 i get the same answer, is that supposed to happen?

pppaul03:07:12

(defn parse-input [s]
  (->>
    (str/split s #"\n+")
    (map parse-long)
    (reduce +)))

#_(parse-input sample-input)

(defn part-1 [input]
  (parse-input input))

(defn part-2 [input]
  (parse-input input))

(part-1 sample-input)
(part-2 sample-input)
using your sample data, i am able to get the same behaviour with this code. i'm really confused as to what the objective of the code is or how to test it

Kevin Hovsäter06:07:36

@U0LAJQLQ1 your implementations of part1 and part2 above is not correct. The first part should return the biggest number. The second part should return the sum of three biggest numbers. Not sure how you end up with the same number. I’ve tested my code multiple times.

Kevin Hovsäter06:07:01

I do appreciate the feedback regarding breaking out into a separate function to ease debugging. I can also see how reducing within parse-input is weird. I did that to save repeating myself in part-1 and part-2, but it should most likely just be repeated.

tschady09:07:45

checkout #adventofcode for comparative solutions. Each day has a thread pinned, here’s https://clojurians.slack.com/archives/C0GLTDB2T/p1669870809772339

dgb2313:07:28

Your code is nice! Have a look at http://clojuredocs.org for more regex based functions. They typically start with re- I'm half sure you can do more in fewer steps (computations). It's sometimes interesting to do the same thing in different ways.

pppaul15:07:51

@U05JP9PDU75 how am i supposed to know what these function are supposed to do? when posting code for other people to look at it really matters that you do some work to help them understand the goal. i ran these 2 functions with your sample-data and they produced the same output, so i changed my code as if those were the tests that needed to be passed.

Kevin Hovsäter19:07:46

@U0LAJQLQ1 they don’t produce the same output for the sample data either, so I’m not sure what you did wrong. :man-shrugging:

Kevin Hovsäter19:07:56

But I’ll make sure to make the expectations of program more clear the next time around.

pppaul21:07:25

@U05JP9PDU75 you are running different code than the one you posted in the gist, if that is the case

pppaul21:07:14

ok, seems like there is some problem copying and pasting code form the gist

pppaul21:07:27

newlines just disappear

pppaul21:07:36

that's pretty annoying

Kevin Hovsäter05:07:27

Yeah, I was about to say, the new lines are quite important. 😅