Motivation: Babashka pods are a mechanism to build functionality in any language, and expose those features in Clojure. You can build a high performance numerics library in Rust, and use it from Clojure. Or wrap around some nice existing library.
I read the section on pods in the babashka book and the babashka pods README, and still didn’t feel like everything made sense. So, why not write the introduction that I would have wanted.
Q: give me a minimal example of pods
Sure. Here are the moving pieces:
babashka/babashka.pods
libraryQ: what is a pod?
An executable program that takes commands on stdin, and prints results to stdout.
Q: what kind of commands on stdin?
There are three commands: describe, invoke and shutdown.
Q: what do they do?
Q: that doesn’t sound too complicated!
I thought it was way more complicated too! But that’s it, really. You write an application in some language, and follow a convention for stdin and stdout.
Q: But … I might want to use stdin and stdout for other things.
Yup! When the babashka pods library starts your executable, it sets
the environment variable BABASHKA_POD
to
true
. So you can default to your normal
behavior otherwise. For a practical example, see rorokimdim’s
stash or justone’s
tabl.
Here are some examples:
ns explore.bencode-examples
(:require [explore.bencode :as bencode]))
(
:foo "bar"})
(bencode/encode {;; => "d3:foo3:bare"
= (bencode/encode {:foo "bar"})
("foo" "bar"}))
(bencode/encode {;; => true
-> {:foo "bar"}
(
bencode/encode
bencode/decode);; => {"foo" "bar"}
"teodor")
(bencode/encode ;; => "6:teodor"
99)
(bencode/encode ;; => "i99e"
1.2)
(bencode/encode ;; => error - integers only!
"i99e")
(bencode/decode ;; => 99
defn examples [f & exs]
(for [e exs]
(e (f e)]))
[
(examples bencode/encode"x" 1}
{"name" "tom"}
{"n" "tom"]
[1 2 3]
[1 2]
[
);; => ([{"x" 1} "d1:xi1ee"]
;; [{"name" "tom"} "d4:name3:tome"]
;; [["n" "tom"] "l1:n3:tome"]
;; [[1 2 3] "li1ei2ei3ee"]
;; [[1 2] "li1ei2ee"])
But there’s a caveat. I’m cheating! I’m pretending that all binary data is UTF-8 serialized text.
But that’s enough for us now. If you want do dig further into bencode, please read the Bencode Wikipedia page and the nrepl/bencode tests.
local preview | view on the web