Fork me on GitHub
#babashka
<
2021-02-28
>
borkdude15:02:04

@wilkerlucio What would you need to implement this? reify + ILookup + ...?

wilkerlucio15:02:30

but also IFn, to make (map :key)

borkdude15:02:56

and what about assoc etc?

wilkerlucio15:02:29

yeah, all of those, in the CLJ side I'm currently using Potemkin, but there he also takes care of a lot of Java specific interfaces that I believe are unescessary in Kondo

wilkerlucio15:02:46

for CLJS there also a few different protocols, so I'm not sure the exact list needed for babashka*

borkdude15:02:11

Can you clone babashka locally? And then the branch custom-map.

$ clojure -M:main -e '(def m (reify clojure.lang.ILookup (valAt [this x] (str "->" x)))) (:foo m)'
"->:foo"

borkdude15:02:20

Clone using the --recursive option

wilkerlucio15:02:16

so, my guess here is that I may be able to almost fulfil the complete contract, just not the IFn part?

borkdude15:02:13

$ clojure -M:main -e '(def m (reify clojure.lang.IFn (invoke [this arg] (str "arg->" arg)))) (m :foo)'
"arg->:foo"

borkdude15:02:19

The way it's currently set up it only supports one arity but I think I can change this

wilkerlucio16:02:45

cool, working on other things now, but I'll give a try on that, thanks!

borkdude16:02:52

I fixed the arity problem locally. Any more interfaces you need?

borkdude16:02:55

I'm about to cut a release

wilkerlucio16:02:23

sorry, cant check now

wilkerlucio16:02:35

go ahead with the release 🙂

borkdude17:02:02

I've got ILookup and IFn working now for all arities

wilkerlucio19:03:07

did more playing here, these are some others protocols that are missing:

clojure.lang.Associative
clojure.lang.IPersistentCollection
clojure.lang.Counted
clojure.lang.Seqable
There may be more, according to Potemkin docs (https://github.com/clj-commons/potemkin#def-map-type), these are the nescessary protocols on the JVM: clojure.lang.IPersistentCollection, clojure.lang.IPersistentMap, clojure.lang.Counted, clojure.lang.Seqable, clojure.lang.ILookup, clojure.lang.Associative, clojure.lang.IObj, java.lang.Object, java.util.Map, java.util.concurrent.Callable, java.lang.Runnable, and clojure.lang.IFn, but I'm not sure if we need all of them to get a workable map for Babashka purposes

Luis Santos19:02:40

Hi everyone, I'm currently trying to use records in babashka but they don't seem to work. The documentation says that the implementation is different from clojure but that it should work as expected. In the example bellow the messages are never printed. Am I doing something wrong? Should I open a bug or is this a know limitation? Thanks

(defprotocol Task
  (test [this])
  (test2 [this]))

(defrecord TaskImpl [a b]
  Task
  (test  [this] (println "It should be printed"))
  )

(defmethod test2 TaskImpl []
  (println "It should be printed")
  )

(let [ t (->TaskImpl "a" "b") ]
  (.test t)
  (.test2 t))

borkdude19:02:10

@luis559 Made it a little bit linter proof ;). Note that you can just call the methods by name, you don't have to use .test, although we could probably make that work

(ns foo (:refer-clojure :exclude [test]))

(defprotocol Task
  (test [this])
  (test2 [this]))

(defrecord TaskImpl [a b]
  Task
  (test [_] (println "It should be printed (proto)")))

(defmethod test2 TaskImpl [_]
  (println "It should be printed (mm)"))

(let [ t (->TaskImpl "a" "b") ]
  (test t)
  (test2 t))
It should be printed (proto)
It should be printed (mm)

Luis Santos19:02:13

Thanks. Let me try it again.

Luis Santos20:02:30

@borkdude Thanks. I managed to get it to work. I figured out what my problem was. I was not able to call methods my name like you suggested because I forgot to import them. I guess the biggest difference between the 2 approaches would be weather or not you one needs to import the methods. To make sure my library is compatible with clojure and babashka I have to make sure I don't use the dot syntax.

borkdude20:02:32

@luis559

binary: "/usr/bin/ffprobe"
args:   [:show_format :show_streams ""]
task:   {:ss nil, :ex #error {
 :cause "error=2, No such file or directory"
It's probably best to look on the path for ffprobe?

Luis Santos20:02:39

I will update the docs. But if you remove the binary name it will use whatever is available in the path.

borkdude20:02:20

Ah I didn't know that, thanks.

robertfw20:02:39

Apologies if I missed something obvious in the docs - how can I have a script I am writing return a non-zero exit code? I'm giving babashka a whirl for the first time today, writing a little helper script so I can have one command to properly pick the right command for unpacking a given file. I've googled "tar gz command line options" more than I care to admit, and then today downloaded something that was .tar.xz. Figured it was time to scratch the itch. I've called it bunpack 🐰 🫓

robertfw20:02:42

doh. Never fails, ask a question publicly, find your answer immediately thereafter. I believe System/exit is what I needed

borkdude20:02:17

@robertfrederickwarner That's it and don't worry, feel free to ask anything here

robertfw20:02:33

Thanks @borkdude. Your prolific work astounds! I always loathed writing bash scripts, this is a wonderful addition to the ecosystem

borkdude20:02:52

This looks exciting, thanks for making it babashka compatible @luis559 :)

🆒 9
Luis Santos20:02:29

Thank for helping and sorry misspelling babashka. ;)

jaide21:02:50

Used Babashka over cgi to create a really basic line art svg generator https://cgi.eccentric-j.com/lineart/lineart.svg?bg=000000&amp;fg=ffffff

🆒 6
jaide21:02:39

https://github.com/eccentric-j/clj-lineart src if anyone’s interested in how it works

jaide21:02:51

It did bring about some rough edges I’m trying to come up with better solutions for. The first one is the shebang for the entry point script. #!/home1/<username>/bin/bb makes it less portable. If I could set the PATH variable then I could use #!/user/bin/end bb but unfortunately cgi is pretty locked down so most environment variables are stripped away if set with Apache (according to my research and tests). I’m wondering if I could write a script that behaves like env that will set the path, point to bb, and perhaps supply the src dir relative to the entrypoint script.

borkdude22:02:28

@U8WFYMFRU Maybe you can str/replace the shebang once you deploy the script to bluehost?

borkdude22:02:58

Is it possible to set the interpreter in the cgi config (for a certain file extension)? (sorry, no idea, I haven't used this myself)

jaide22:02:13

As far as I found I can only modify a .htaccess to specify:

AddHandler cgi-script .clj
So that apache knows what to do with requests for that file.

jaide22:02:46

There’s also some runtime config that would be nice to automate so I think a wrapper script would be a reasonable approach

borkdude22:02:48

Yeah, so maybe making a before-deploy hook which rewrites your script just in time, will work

borkdude22:02:02

yeah, you could write a little bash wrapper

jaide22:02:43

Deploys are pretty primitive right now, sometimes SFTP, sometimes rsync, sometimes scp

jaide22:02:56

Is there a way to load code from a local file not on the classpath? Maybe load-file?

borkdude22:02:37

Yep, that's the one

jaide23:02:32

#!/bin/bash

export PATH="/home1/<username>/bin/:$PATH"
export CLJ_BB_LIB_PATH="/home1/<username>/lib/"

exec env bb -cp src "$@"
Making that my bbenv wrapper which I could reference from the parent dir or create a sibling symlink to. Should do the trick to keep host specific things out of the repos.

adam-james13:03:39

@U8WFYMFRU this is cool! If you plan to expand on the idea with more SVG elements, my library https://github.com/adam-james-v/svg-clj might be interesting to you. I'm working hard to get it polished, but some of the functionality might be useful to you as is.

jaide20:03:35

That’s really cool! I foresee making more generative art experiments in the future. Thanks!

❤️ 3
jaide21:02:39

Then at least it would be like #!../../bin/devenv bb which is a bit easier to replicate in a local docker for development or move to another server.

robertfw23:02:25

Any suggested exemplars for a published babashka script intended for use on the command line? I pushed my little bunpack script up onto github, if anyone has use for it or any suggestions for how to package it in a nicely reusable form. https://github.com/robertfw/bunpack

🎉 6
borkdude23:02:37

You could use a shebang so you don't need the bash wrapper:

#!/usr/bin/env bb

borkdude23:02:48

And then just place the .clj script on the path

robertfw23:02:19

I started with that but my emacs setup was throwing a hissy fit when trying to auto format the buffer

borkdude23:02:55

@robertfrederickwarner If you are in clojure mode it should deal well with the shebang

robertfw23:02:40

I may be making some basic mistake in how I'm interacting with the code. Currently, I'm running cider-jack-in-clj to connect my repl, and then cider-format-buffer . That results in an Invalid symbol: !/usr/bin/env. error

borkdude23:02:08

Have you put the hash sign in front of this?

robertfw23:02:15

Ah. I wonder if this is just starting up a regular clojure REPL. I'm guessing I'd want this to fire up a babashka repl instead?

borkdude23:02:17

Then that could be a bug in cider-format-buffer. I'm always just using this function:

(defun iwb ()
  "Indent whole buffer."
  (interactive)
  (delete-trailing-whitespace)
  (indent-region (point-min) (point-max) nil)
  (untabify (point-min) (point-max)))
Might be worth filing this bug to CIDER

borkdude23:02:57

Yeah, that will start a regular clojure JVM

borkdude23:02:19

I've got to go now, later 💤

robertfw23:02:44

Thanks for the tips! Good night

robertfw23:02:47

I meant to be learning CLJS today. I started installing shadow-js and got to the "install node" step, had to google "extract tar.xz" file.. and now here we are. I haven't learned CLJS but at least I got my feet wet with bb 😋

babashka 6
wilkerlucio19:03:07

did more playing here, these are some others protocols that are missing:

clojure.lang.Associative
clojure.lang.IPersistentCollection
clojure.lang.Counted
clojure.lang.Seqable
There may be more, according to Potemkin docs (https://github.com/clj-commons/potemkin#def-map-type), these are the nescessary protocols on the JVM: clojure.lang.IPersistentCollection, clojure.lang.IPersistentMap, clojure.lang.Counted, clojure.lang.Seqable, clojure.lang.ILookup, clojure.lang.Associative, clojure.lang.IObj, java.lang.Object, java.util.Map, java.util.concurrent.Callable, java.lang.Runnable, and clojure.lang.IFn, but I'm not sure if we need all of them to get a workable map for Babashka purposes