Fork me on GitHub
Kris C06:10:31

How can I read a text file in batches of n lines? Is there a simple way of doing this?


You can use line-seq to get a lazy sequence of lines. That's probably a good start.

Kris C07:10:25

and then write a recursive function with take n and drop n?


It would depend on how exactly you want to consume/process it. What are you trying to do with those batches of lines?


You can partition-all the line-seq, for example.


So, what problem are you actually trying to solve here?

Kris C07:10:29

I have a file containing a large dataset and want to import that to a db

Kris C07:10:29

so, read n lines -> process transaction -> read next n lines...


OK. So I'd probably structure that as

(run! transact-batch (partition-all n (line-seq source)))


transact-batch would be invoked with a sequence of n lines at a time (except for the last batch which may be smaller).


It could then use next.jdbc/execute-batch! to insert a block of n rows into the database.

Kris C07:10:42

uh, thanks a lot.


If you need each line parsed into data suitable for the DB:

(run! transact-batch (partition-all n (parse-line (line-seq source))))


(it's morning for you but after midnight here so I'm off to bed but feel free to post any follow-up Qs and I can answer when I'm awake again)

Jakub Holý (HolyJak)09:10:36

This is a mystery:

  (def p (java.util.Properties.))
  (def ^:const btst4 {:k p})
  (:k btst4))
; => IllegalArgumentException: No matching ctor found for class java.util.Properties
But if I remove the ^:const it starts working. Any idea what is happening? (Clojure 1.10.1 and 1.10.3)


The exception (for me) is thrown in this line: My interpretation is that it finds multiple constructors with the same arity, but none matches the parameter type?


I’m wondering about what args are assumed there, but I don’t have a setup which can debug into Java Code


last time I checked ^:const isn't documented in, it's simpler and better documented to use direct linking

Jakub Holý (HolyJak)10:10:56

Same here, I couldn't find it there. Worth direct linking you mean ^:inline?


^:const is mentioned in the API cheatsheet at least.

Jakub Holý (HolyJak)11:10:21

Thank you. If direct linking is a compiler flag then it isn't really relevant here. I am trying to define a map with rather static config options, that's why I marked it with const. I guess I will try to debug it as @U0N9SJHCH proposed to see what really is happening and whether or is a Clojure bug


^:const does not prevent the referenced object from changing. It simply tells the compiler that the var itself will not be changed.


So in your case, seems like you don't need it at all.


it's weird because it works with classes like HashMap

(def x (java.util.HashMap.))
(def ^:const bar {:foo x})
(:foo bar)


With Properties you don't even need to call it, hell you can't even wrap it in try-catch. I think it dies somewhere in the bowels of the compiler

(def x (java.util.Properties.))
(def ^:const bar {:foo x})
So far so good, but then
(defn check [] (:foo bar))
dies without ever calling (check)


Also, it works if you wrap it in a (defn…)


And Java 8 returns a different error message than Java 11

Alex Miller (Clojure team)12:10:26

This is not a place where I would expect const to work

Alex Miller (Clojure team)12:10:05

Use const when you have something like a number or a string you want to inline during compilation. Almost anything beyond that is probably a misuse

💯 1
Jakub Holý (HolyJak)12:10:06

Ah, ok, good to now :-) I thought it would make the field static final or something.


The inconsistency bugged me. Seems like const stuff get quoted? Then I assume evaled at some later point? Thing is that even (eval {:k (java.util.Properties.)}) results in the same error. I added a ton of debug statements in the Reflector and for some reason it can't find the no-args constructor of Properties?

ctor public java.util.Properties(java.util.Properties)
params class java.util.Properties
args {}

ctor public java.util.Properties(int)
params int
args {}
I can't seem to replicate this with other java.util classes (and I tried with a few that aren't used in the Clojure source), so no idea why it only breaks with Properties

❤️ 1

Just some debug output from Compiler/analyze and Reflector

Alex Miller (Clojure team)13:10:56

These are two different things, but I think you're misunderstanding both of them.

Alex Miller (Clojure team)13:10:01

With const, you're saying that the value is a conpile-time constant and the compiler can replace the var reference in the bytecode with the literal value

Alex Miller (Clojure team)13:10:23

In the eval case, I'm not sure exactly what you're seeing there, but it feels like the fallback case the compiler uses to round trip arbitrary objects through bytecode by using print/read


Hm that makes it more clear. Any idea why it behaves differently with Properties and for example BitSet?

Alex Miller (Clojure team)13:10:10

no, without spending time I don't really have atm to find out. Properties is one of the worst classes in the JDK and it has a lot of eccentricities.

👍 1
Mateo Difranco12:10:10

Hmmm... what could be wrong here?

(map key t)
=> (:4 :1 :2 :5 :3)
(name :4)
=> "4"
(map (name key) t)
Execution error ...
clojure.core$key cannot be cast to clojure.lang.Named


keywords are functions, strings aren't

Mateo Difranco12:10:00

You're right, that was silly.

(map #(name (key %)) t)

👏 1

oh, and (name key) is applying the name function to the function key


do you mean (map (comp name key) t) ??

☝️ 1
Mateo Difranco12:10:29

That's a nice solution 🙂

Muhammad Hamza Chippa13:10:01

is there any way to lexicographically sort list ("john" "harry" "anne") into ("anne" "harry" "john")

Johan Thorén13:10:37

(sort '("john" "harry" "anne"))
;; => ("anne" "harry" "john"

🙌 1

Be careful when you sort collections of strings:

(sort ["John" "anne" "Harry"])
;; => ("Harry" "John" "anne")
Depending on the desired result you might want to sort-by lower-case

🎯 1
🙌 1
Daniel Ziltener14:10:13

Is there any library to "memoize" a function using Redis as the caching backend? I found the "crache" library, but it's broken


I wouldn't imagine so since there are a few competing redis libraries and no clear winner


but it shouldn't be that hard to write - just decide how you want to serialize your function's arguments and result to store in redis


You could probably write a Redis backend to core.cache -- it's designed to be extensible.

Daniel Ziltener16:10:25

I am currently trying to do so, by trying to fix "crache", but so far I am failing, the last argument passed to CacheProtocol/miss seems to be a function from core.memoize...


core.cache does not depend on core.memoize -- but core.memoize does depend on core.cache.


  :build {:paths ["src"]
          :deps {io.github.clojure/ {:git/tag "v0.6.2" :git/sha "226fb52"}}
          :ns-default build}

(ns build
  (:require [ :as b]
            [shadow.cljs.devtools.api :as shadow]))
(defn uber [_]
  (clean nil)
  (shadow/release :app)
  (b/copy-dir {:src-dirs ["target" "resources"]
               :target-dir class-dir})
  (b/compile-clj {:basis basis
                  :src-dirs ["src"]
                  :class-dir class-dir})
  (b/uber {:class-dir class-dir
           :uber-file uber-file
           :basis basis}))
Hi I have success in combining the cljs compilation part with clj to get a uber jar file. Question: is there a way to default the .jar to a main class instead of
java -cp xyz-1.2.2-standalone.jar clojure.main -m xyz.core
I'd like to use just
java -jar xyz-1.2.2-standalone.jar

Alex Miller (Clojure team)18:10:10

yes, you can use the :main key in b/uber

Alex Miller (Clojure team)18:10:50

(and btw, you might find #tools-build to be good for future questions like this)


(b/uber {:class-dir class-dir
         :uber-file uber-file
         :basis basis
         :main 'xyz.core})
is this correct?


Is there also an option to AOT compile the code? I have not been able to find anything


There's a compile-clj function in


It's shown in @zengxh’s build.clj above (further up).


Oh so compile-clj would do the Ahead of Time compilation


Interesting; I was receiving an error from graalvm when trying to create a native image even though I was using AOT. I might have done something else incorrectly.


Thanks for the clarification


Well, it's "just" compilation, but it is being done before building the uber JAR so it is "AOT" in that respect.


$ java -jar xyz-0.1.4-standalone.jar 
Error: Could not find or load main class xyz.core
Caused by: java.lang.ClassNotFoundException: xyz.core
$ java -cp xyz-0.1.4-standalone.jar clojure.main -m xyz.core


Is there also an option to AOT compile the code? I have not been able to find anything


@zengxh You also need to ensure that your main class is compiled and that it has (:gen-class) in its ns form for that namespace.


(I think a lot of people miss that last part)


compile-clj with that i thought i could get away without gen-class . i never really understand gen-class 😳


:gen-class causes -main to become a static main method in the class compiled from the namespace.

Alex Miller (Clojure team)19:10:08

really, it causes all -foo methods to become static, just most importantly -main :)


Yes, I was being deliberately vaguely/over-simplified here 🙂 :gen-class is a veritable Swiss Army Knive!


(otherwise it's just compiled Clojure code that can be called from Clojure, rather than directly from Java)


perfect. gen-class is what i missed.


thank you!!


I tend to miss it almost every time I write a new "main" namespace. Then I compile and build the uber JAR and go "Oh, rats! I forgot :gen-class again!" 🙂


I often wish it was a special case that defn -main always became a static main method 🙂


maybe there should be a linter for that 🙂


Yeah! @borkdude if clj-kondo sees defn -main in a namespace that doesn't have (:gen-class) in the ns form, that could be a useful warning! 🙂


hmm, makes sense! :)


although you can still invoke this with -M -m foo as well without having to have :gen-class necessarily - any other heuristics that should point at :gen-class ?


I'm refactoring some code that uses a dynamic db connection built using mount, it looks like

(ns service.test-fixtures
  (:require [service.db :refer [*db*]])) ;;the dynamic var

(defn db-tx-rollback-fixture [f]
  (jdbc/with-db-transaction [tx *db*]
    (jdbc/db-set-rollback-only! tx)
    (with-redefs [*db* tx]
this code has been copy pasted in numerous repos and i wanted to pull out a bunch of test fictures we have into a common lib. Refactoring this as simply
(defn db-tx-rollback-fixture [db]
 (fn [f]
   (jdbc/with-db-transaction [tx db]
     (jdbc/db-set-rollback-only! tx)
     (with-redefs [db tx]
and calling with
(ns service.test-fixtures
  (:require [service.db :refer [*db*]]))

(use-fixtures :each (db-tx-rollback-fixture *db*))
obviously doesnt work, I'm trying to wrap my head around how i can write a macro to solve this.


Instead of with-redefs, use binding. *db* is dynamic.

Muhammad Hamza Chippa21:10:09

how to generate uuid from string ?


Are you fairly new to Clojure @U02DQ45FQF9? If so, maybe ask Qs in #beginners where folks have opted in to helping in depth with basic questions like this.

🙌 1

(logged an issue about gen-class + -main here: