Fork me on GitHub
#babashka
<
2021-08-04
>
dabrazhe12:08:38

is there a way to pass the output of a bb task to another task as input?

borkdude12:08:50

@dennisa you can pass return values from one task to another like this:

{:tasks {a (+ 1 2 3) b {:depends [a] :task (+ a 2)}}}

👍 2
2
dabrazhe12:08:54

aha, so the whole output of a will be passed to b ? If b has command line params as well they can be passed separately?

borkdude12:08:16

command line params are always handled via *command-line-args*

borkdude12:08:08

note that "return value" isn't the same as "output", as in text written to the console

👍 2
dabrazhe12:08:35

is there a shortcut to `--prn, as in bb run --prn b`  ?

borkdude12:08:43

no. but if you want b to always print, you could do that in the task itself

dabrazhe12:08:40

Can I print all outputs in the global :leave section?

borkdude12:08:27

I don't think this is possible right now, since the var named by the task isn't defined by then yet

borkdude12:08:40

else you could have done something like:

:leave (prn (deref (resolve (:name (current-task)))))

borkdude12:08:47

This could potentially be fixed, issue welcome

borkdude12:08:31

It does work for dependencies since they already defined:

{:tasks {:leave (prn (map (comp deref resolve) (:depends (current-task))))
         a 1
         b {:depends [a]
            :task (+ a 1)}}}

borkdude12:08:44

$ bb b
()
(1)

borkdude12:08:34

note that you can define helper functions etc in :init, this also may help

👍 3
dabrazhe12:08:36

This is powerful stuff. I'd update the bb book, because I saw the :depends section but it was not clear if the output is passed as well (vs only task a being called first)

👍 2
mauricio.szabo16:08:26

Hi, I'm trying to use a library on Babashka that wraps some Java objects, and I'm having the following error:

Unable to resolve classname: java.util.function.Function

mauricio.szabo16:08:41

Is Function not enabled on Babashka by default?

borkdude16:08:57

We can add this class, given some useful minimum viable scripting example :)

borkdude16:08:03

Classes are added on demand

borkdude16:08:08

to not make the image bigger than necessary

borkdude16:08:42

but Function seems like a pretty useful thing to have, just curious about the application

mauricio.szabo16:08:47

Ah, right! Interesting! I was trying to make funcool/promesa wok with bb

mauricio.szabo16:08:13

They import these libs:

java.util.function.Function
      java.util.function.BiFunction
      java.util.function.BiConsumer
      java.util.function.Supplier
      java.util.function.Consumer

borkdude16:08:03

I would suggest making a fork of bb and add these classes to babashka.impl.classes and see how far you would get

borkdude16:08:22

you can invoke bb via clojure CLI using clojure -M:babashka/dev if you make some alias for it

borkdude16:08:09

I see promesa also relies on deftype

borkdude16:08:03

bb doesn't support this, it could to a certain degree but not if deftypes implement Java interfaces since GraalVM doesn't support creating new classes at runtime

borkdude16:08:49

but bb already comes with core.async so perhaps that's a good replacement

mauricio.szabo16:08:19

Yeah, it'll probably not be so simple to port then. Ok, thanks 🙂

whatacold16:08:50

Hi, I'm trying to rewrite a little babashka script (https://github.com/whatacold/babashka-tools/blob/master/illustrate.clj) using rewrite-clj to evaluate top-level forms and appends the result in comments for illustration purpose. For example, I want to transform below:

(defn my-function [a]
  (* a 3))

(my-function 7)
to something like:
(defn my-function [a]
  (* a 3))
;; => user/my-function

(my-function 7)
;; => 21
But I'm a little stuck that the last comment didn't show up, here is what I came up:
(require '[rewrite-clj.zip :as z]
         '[rewrite-clj.node :as n]
         '[rewrite-clj.parser :as p])

;; define some test data
(def data-string
  "
(defn my-function [a]
  (* a 3))

(my-function 7)
")

;; parse code to nodes, create a zipper, and navigate to the first non-whitespace node
(def zloc (z/of-string data-string))

(loop [cur zloc
       left cur]
  (println "current string {{" (z/string cur) "}}")
  (if (z/end? cur)
    (println "final string: {{" (z/root-string left) "}}") ; XXX it doesn't work as expected!!!
    (recur (z/right (z/insert-right cur (p/parse-string ";; test\n"))) cur)
    ))
The output is like this:
current string {{ (defn my-function [a]
  (* a 3)) }}
current string {{ (my-function 7) }}
current string {{ nil }}
final string: {{ 
 (defn my-function [a]
  (* a 3)) ; test


(my-function 7)
 }}
What's the problem with my code? Thanks.

borkdude17:08:15

@whatacold if you don't get an answer here, you might also wanna try #rewrite-clj

whatacold17:08:32

Thanks, I didn't know that there is a specific channel for rewrite-clj. I'll post there too.