Fork me on GitHub
#nbb
<
2022-07-19
>
Aron05:07:32

I tried to use nbb recently, but I think I am lacking some fundamentals, because I am stuck

Aron05:07:01

all I need is to recursively read files from a directory on windows, filter and parse them 🙂

jmglov06:07:14

If I understand what you mean, you should be able to do something like this:

(require
 '[babashka.fs :as fs]
 '[clojure.string :as str])

(defn my-filter [path]
  (str/starts-with? (fs/file-name %) "t"))

(defn my-parser [path]
  (slurp (fs/file path))

(->> (fs/glob "somedir" "**")
     (remove fs/directory?)
     (filter my-filter)
     (map my-parser)

Aron06:07:21

looks like it, I was trying node interop and running into some Promise weirdness that I didn't understand

jmglov06:07:07

Yeah, I'd use the builtin Babashka and Clojure stuff first and see if I can avoid interop altogether.

jmglov06:07:26

If you know you only want files match a certain pattern, https://babashka.org/fs/codox/babashka.fs.html#var-glob can do a lot more than just return all of the files. You can do (fs/glob "somedir" "**.clj") do get all of the Clojure files, for example.

Aron06:07:41

agreed, for some reason I concluded that those wouldn't work, not sure why, I was too tired probably

Aron06:07:01

trying this require form kills the nbb nrepl server with no value specified for babashka.fs

Aron06:07:16

for key [babashka.fs as fs

Aron06:07:39

Not sure what that even means

Aron07:07:29

Ok, so I wasn't entirely wrong, babashka.fs is only for the JVM, doesn't work on node

jmglov07:07:44

omg, didn't know that. 😭

jmglov07:07:02

Is there any reason you want to use nbb instead of regular bb?

Aron07:07:46

well, node is already installed

jmglov07:07:25

bb doesn't need anything installed. You can just download the static binary and use it.

Aron07:07:28

colleagues are mostly jsers, so they would get a milder heart attack

jmglov07:07:37

You're on Windows, right?

Aron07:07:52

downloading binaries on corporate laptop is "installing" them

Aron07:07:21

That said, probably will try it, because your code looks simpler than anything I could do in node I think

jmglov07:07:14

I'm not a Node person myself, but I'm sure other people in the channel can get you over the current hurdle with nbb if that's preferable. 🙂

Aron07:07:23

we are in the nbb channel 😄

Aron07:07:47

anyway, thanks again, will try it soon

1
jmglov07:07:51

I didn't realise that bb and nbb are so different.

Aron07:07:58

me neither, and the stuff I see should work, I just don't understand why I see a promise come out of another promise, when those should be unwrapped at once. Need more time to debug, but even better if I can avoid the whole mess

Aron07:07:09

I dislike promises with a passion

borkdude07:07:56

You can use node fs readFileSync

Aron07:07:29

finding files recursively is the biggest complication

Aron07:07:05

I switched for a bb repl but this require form still doesn't work, now it says lib names inside prefix lists must not contain periods

borkdude07:07:37

For Node.js you can use any Node library for globbing. E.g.: https://github.com/isaacs/node-glob

borkdude07:07:47

For your bb problem, paste your ns form here, there's probably a syntax error or so

Aron07:07:20

I think I used the node based glob before and didn't have this feature? But will check again

Aron07:07:37

oh, example, can't argue with that

Aron07:07:51

awesome as always @U04V15CAJ

Aron07:07:16

I was using the same code that @U054AT6KT typed in this thread at the very beginning. Could be that I messed up something else by not fully starting from scratch. But going back to nbb out of curiousity now:)

borkdude07:07:53

You probably didn't use this example literally but transformed it into:

(ns foo (:require
 '[babashka.fs :as fs]
 '[clojure.string :as str]))
which is indeed syntactically not valid. You should remove the quotes from those libspecs inside the ns form

Aron07:07:13

thanks, that's what happened exactly

Aron07:07:22

will read up on this 😐

borkdude07:07:33

So it's either:

(require '[foo])
or (ns my-ns (:require [foo]))

borkdude07:07:46

The reason is that ns is a macro and require is a function

❤️ 1
Aron08:07:55

so beautiful when things work

Aron08:07:13

I just wish I wouldn't be so confused all the time:)

❤️ 3
teodorlu10:07:11

And loose out on all the fun and weird stuff? 😂

😅 1