Fork me on GitHub
#babashka
<
2022-09-06
>
rafalw11:09:38

I want to write simple script which will fetch some data via rest (http get), and I want this script to be runnable from babashka and jvm, which pod/lib should I use ?

borkdude11:09:06

@rafal.wysocki you can use babashka.curl which is built into bb

borkdude12:09:39

You could also use http-kit.client (built-in) or a Java 11 client based library like https://github.com/schmee/java-http-clj

borkdude12:09:36

I really like the java-http-clj one, it's clean and doesn't do a lot

rafalw12:09:34

http-kit.client it is, thanks

👍 1
flowthing15:09:06

Is it feasible to add Babashka support for clojure.core/PrintWriter-on?

flowthing15:09:31

I tried copy-pasting it from clojure.core:

(defn ^java.io.PrintWriter PrintWriter-on
  "implements java.io.PrintWriter given flush-fn, which will be called
  when .flush() is called, with a string built up since the last call to .flush().
  if not nil, close-fn will be called with no arguments when .close is called"
  {:added "1.10"}
  [flush-fn close-fn]
  (let [sb (StringBuilder.)]
    (-> (proxy [java.io.Writer] []
          (flush []
            (when (pos? (.length sb))
              (flush-fn (.toString sb)))
            (.setLength sb 0))
          (close []
            (.flush ^java.io.Writer this)
            (when close-fn (close-fn))
            nil)
          (write [str-cbuf off len]
            (when (pos? len)
              (if (instance? String str-cbuf)
                (.append sb ^String str-cbuf ^int off ^int len)
                (.append sb ^chars str-cbuf ^int off ^int len)))))
      java.io.BufferedWriter.
      java.io.PrintWriter.)))
But when I try using it:
(PrintWriter-on println nil)
I get:
java.lang.IllegalArgumentException: No matching clause: ["java.io.Writer" #{}] [at <repl>:27:1]
And I don’t really understand why.

borkdude15:09:57

I think we could add it as a built-in

👍 1
borkdude15:09:47

PR welcome in bb

borkdude15:09:55

or just an issue is also fine

flowthing15:09:40

Cool, I have no idea how bb is made, but I’ll see if I can come up with a PR. 🙂

flowthing15:09:56

While I have you, do you know why this fails on Babashka? (java.util.concurrent.atomic.AtomicInteger.)

flowthing15:09:31

It says No matching ctor found for class java.util.concurrent.atomic.AtomicInteger, but that definitely is a valid constructor.

borkdude15:09:55

For the PR, you can just add it in src/babashka/impl/clojure/core.clj

flowthing15:09:24

OK, thanks, I’ll look into it. :thumbsup:

borkdude15:09:34

About java.util.concurrent.atomic.AtomicInteger - currently it has limited interop support to save space, but we could expand that

borkdude15:09:59

Can you tell me more about your use case? We can make a PR which supports your program

flowthing15:09:19

Nice. Maybe I can look into that as well.

borkdude15:09:43

The relevant change for that is in src/babashka/impl/classes.clj

👍 1
borkdude15:09:59

Currently that class is in :instance-checks which makes only the class available but no methods etc

flowthing15:09:05

I’m working on editor integration with Babashka.

👍 1
borkdude15:09:07

We could just move that to the list of classes that have full support

borkdude15:09:26

but I always check the difference in binary size to see how it affects that

flowthing15:09:45

AtomicInteger is more of a nice-to-have thing, but I can’t do much without PrintWriter-on.

borkdude15:09:07

yeah, PrintWriter-on is a no brainer, I'll merge that immediately

flowthing15:09:33

What about the java.lang.IllegalArgumentException: No matching clause: [".Writer" #{}] [at <repl>:27:1] thing, though?

borkdude15:09:06

That error has to do with the support in reify : the support for that is hardcoded and Writer has not been added to that list

flowthing15:09:19

I see. :thumbsup:

borkdude15:09:42

error message could be better

flowthing15:09:43

Thanks for all the help. :thumbsup: I’ll dive into this once I’m on the other side of this flu.

❤️ 1
🍵 1
borkdude17:09:32

I just pushed PrintWriter-on to master

🎉 1
flowthing17:09:25

Wow, thank you!

borkdude17:09:42

Also moved AtomicLong/Integer to the full interop

borkdude17:09:12

When current master build finishes you can download from: https://github.com/babashka/babashka-dev-builds

flowthing05:09:37

Thank you so much! I really appreciate it. :thumbsup: I gave it a try and this is beginning to look doable. 🙂 https://tutkain.flowthing.me/bb.mov

flowthing05:09:37

I went ahead and made a quick list of all the things Tutkain uses that Babashka doesn’t currently support: • clojure.core.server/stop-server (https://github.com/babashka/babashka/blob/2400fc7be077ea54b95d94c91cbbb2ebf0ff47de/src/babashka/impl/server.clj#L20-L24) • clojure.core/*source-path* (https://github.com/babashka/sci/blob/133a7565749ac6cd5a8308182f0b1c7fc47e8a3d/src/sci/impl/namespaces.cljc#L942) • clojure.core/ns-unalias (https://github.com/babashka/sci/blob/133a7565749ac6cd5a8308182f0b1c7fc47e8a3d/src/sci/impl/namespaces.cljc#L1324) • clojure.main/with-bindings (https://github.com/babashka/babashka/blob/ddf03c50ae14c942c69c85e73e96caed00767e37/src/babashka/impl/clojure/main.clj#L28-L51) (not sure why this doesn’t work — it seems to exist?) • clojure.main/ex-triage, clojure.main/ex-str, clojure.main/repl-exception, clojure.main/repl-caught, clojure.main/skip-whitespace (https://github.com/babashka/babashka/blob/2400fc7be077ea54b95d94c91cbbb2ebf0ff47de/src/babashka/impl/clojure/main.clj) • clojure.lang.Compiler/load (probably not possible? maybe clojure.core/load-reader instead?) • java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy I can definitely live without almost all of these, but stop-server would be very nice to have. Luckily, it seems to me to be the most straightforward to add…? I’m willing to do the legwork on all of this, of course, but I’m wondering whether you have any quick thoughts on the feasibility of adding these things? I certainly understand wanting to keep the binary size small, and I’m not expecting you to add these things just for Tutkain’s sake. 🙂

borkdude09:09:13

This is very nice!

borkdude09:09:38

of course, we can add stop-server, etc

borkdude09:09:47

ns-unalias also

borkdude09:09:32

not sure what the other things entail, but we could discuss/think about that in separate issues

flowthing10:09:28

Definitely, that works for me. :thumbsup: I’ll get back to you via issues/PRs later.

grzm21:09:25

What’s the equivalent to, say, (System/getProperty "java.class.path") in babashka? I’d like to see everything it thinks it has access to.

borkdude21:09:51

@grzm

(babashka.classpath/get-classpath)

thanks2 1
grzm21:09:36

I just have to keep reminding myself that you’re just so so awesome to keep myself from feeling so incredibly incompetent. Thanks, @U04V15CAJ 🙂

borkdude21:09:19

you're welcome ❤️

mschmele22:09:49

A few quick google searches didn't turn up anything - is there an established way to import csv files into a sqlite database?

lukasz22:09:03

Something like this:

sqlite> .mode csv
sqlite> .import ./test_events.csv events
sqlite> .import ./test_users.csv users

lukasz22:09:19

now you have two tables you can query, join etc

mschmele22:09:54

Oh is it seriously that easy?

lukasz22:09:09

if I remember correctly, you can also then do:

sqlite> .output somefile.csv
sqlite> select * from users u join events e on e.user_id = u.id

lukasz22:09:13

yep, that easy

mschmele22:09:30

I was reading babashka and honeysql documentation thinking that it would be some kind of tedious thing lol

lukasz22:09:46

this is from the sqlite repl, I think it also supports cmd line switches

mschmele22:09:57

big ol' facepalm on my part - thank you!

lukasz22:09:01

np :thumbsup:

mschmele22:09:17

I see plenty of examples using a .db file