shorter notation for functions and regexes

This commit is contained in:
Michiel Borkent 2019-08-09 18:17:28 +02:00
parent 026f911be2
commit d1a02ca7e3
3 changed files with 12 additions and 4 deletions

View file

@ -64,14 +64,18 @@ $ echo '"babashka"' | bb '(re-find (re-pattern "b.b.*") *in*)'
"babashka"
```
Functions are written using the reader tag `#bb/fn`. Currently up to three
Functions are written using the reader tag `#f`. Currently up to three
arguments are supported.
``` shellsession
$ echo '3' | bb '(#bb/fn (+ %1 %2 %3) 1 2 *in*)'
$ echo '3' | bb '(#f (+ %1 %2 %3) 1 2 *in*)'
6
```
$ ls | bb --raw '*in*' | bb '(filterv #bb/fn (re-find (re-pattern "reflection") %) *in*)'
Regexes are written using the reader tag `#r`.
``` shellsession
$ ls | bb --raw '*in*' | bb '(filterv #f (re-find #r "reflection" %) *in*)'
["reflection.json"]
```

View file

@ -77,6 +77,9 @@
%3 z
(interpret elt x))) form)))
(defn read-regex [form]
(re-pattern form))
(defn apply-fn [f in args]
(let [args (map #(interpret % in) args)]
(apply f args)))

View file

@ -1 +1,2 @@
{bb/fn babashka.interpreter/read-fn}
{f babashka.interpreter/read-fn
r babashka.interpreter/read-regex}