Fork me on GitHub
#babashka
<
2022-12-08
>
jmglov10:12:21

To make REPL-driven development easier, I'd like to only eval a certain sexp when not in a REPL. Is there an easy way to detect when I'm in a REPL? Apologies if this has been asked and answered; I did a basic search in this channel and didn't find anything.

borkdude10:12:38

The idiom for this in babashka is:

(when (= *file* (System/getProperty "babashka.file"))
  ;; you're not in the REPL
)

awesome 2
borkdude10:12:19

Of course you can also just use comment forms

jmglov14:12:07

Thanks! šŸ™‚

šŸŽ‰ 1
Kevin13:12:33

Hey there. I'm trying out babashka.nrepl but can't seem to figure out how to print on the client. I have a minimal server setup (sci/binding doesn't seem to be necessary)

(sci/binding [sci/out *out*
                sci/in *in*]
    (-> (sci/init {:namespaces {'my {'print (fn [] (println "TESTING"))}}})
        (babashka.nrepl.server/start-server! {:host "127.0.0.1" :port 23456})))
When I connect with Cider and and run (println 123) it get properly printed to the client's REPL. But calling (my/print) prints in the server REPL. Any idea how to fix this?

borkdude13:12:11

You should do it the other way around:

(binding [*out* sci/out] ...)

Kevin13:12:11

doh! That was silly. It also needs to be inside of the :namespaces functions I think?

Kevin13:12:34

Like this

(-> (sci/init
         {:namespaces {'my {'print (fn []
                                     (binding [*out* @sci/out]
                                      (println "TESTING")))}}})
        (babashka.nrepl.server/start-server! {:host "127.0.0.1" :port 23456}))

Kevin13:12:50

Instead of outside the sci/init like I had list time

borkdude13:12:35

I think that would make it more robust yes

Kevin13:12:11

Thanks, it works! šŸ˜„

šŸŽ‰ 1
Dimitar Uzunov13:12:05

how do you do shell stream editing with babashka? i.e. like you do with sed in bash. In example Iā€™m reaching for bash+sed in cases like this: journalctl -u servicename-$1 -f -o cat | sed 's/(.+servicename\[.+\]:)//'

Dimitar Uzunov13:12:24

oh cool! Thanks Borkdude!

šŸ‘ 1
borkdude14:12:52

I improved the example now, click link again

šŸ‘ 1
pesterhazy14:12:16

Love it, can I steal this (or something like it) for How to do things with babashka?

borkdude14:12:35

once again improved, please check again. I emphasized streaming https://github.com/babashka/process#processing-streaming-output

pesterhazy14:12:25

Is there a way to avoid having to add nil at the end to skip the gobbldygook at the end?

borkdude14:12:34

not really, but maybe we could make this an env var or so

borkdude14:12:15

it was kind of a mistake to print the last expression when executing a file, but when I did a poll most people wanted to keep it ;)

pesterhazy14:12:12

huh interesting - I think it's a little flaw (but doesn't matter in the end)

borkdude14:12:51

@U06F82LES reading an entire file into memory is done easier like this:

(:out (shell {:out :string} "cat /etc/hosts"))

borkdude14:12:18

I mean, the output from a process -> string

pesterhazy14:12:00

šŸ‘ updated the 2nd example

pesterhazy14:12:28

Tangentially I like to split the command and args, because otherwise I get anxious - who does the string splitting again? does it know about single quotes? what if I need to interpolate a variable here? I feel like separate args are simpler in the Hickeyan sense

borkdude14:12:49

you can provide separated args yourself too

borkdude14:12:59

(shell "cat" "/etc/hosts")

pesterhazy14:12:16

yeah I know! I did in the snippet

pesterhazy14:12:29

it's all good šŸ™‚

borkdude14:12:22

The nil at the end of a bb program: the "QED" of a bb script ;)

borkdude14:12:39

@U06F82LES I thought about changing the printing of the output like 2 years or so ago, but then the Rich Hickey on my shoulder told me that it could be a breaking change...

ā¤ļø 1
rich2 1
borkdude14:12:38

You can use

clojure``` in markdown to get syntax highlighting :)

šŸ‘ 1
Dimitar Uzunov14:12:05

how do you use the repl when writing code like this? Mine just freezes

Dimitar Uzunov14:12:19

or do you use i.e. -x to run the functions?

borkdude14:12:07

it depends, I sometimes also write little scripts and just execute them

borkdude14:12:25

especially when dealing with infinite output stuff like this ;)

borkdude15:12:14

Please help spread the word on this new babashka article that just appeared on the GraalVM blog :)

āœ… 2
upvote 4
lread16:12:20

> Note that the amount of parens is exactly the same though šŸ™‚. I hope that James Gosling reads that simple_smile.

šŸ˜† 1
lread16:12:04

(for those of you who were not at reClojure, James Gosling was a keynote speaker and he hinted at his struggles with parens)

emilaasa08:12:24

I think he was talking about writing lisp without much editor help when doing his PhD in the eighties šŸ™‚

emilaasa08:12:07

Nice article! Fun to learn more about how bb works