From d1a02ca7e3578786b89aad0c02cf82806bf16f3d Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 9 Aug 2019 18:17:28 +0200 Subject: [PATCH] shorter notation for functions and regexes --- README.md | 10 +++++++--- src/babashka/interpreter.clj | 3 +++ src/data_readers.clj | 3 ++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e649143c..3c67d7bb 100644 --- a/README.md +++ b/README.md @@ -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"] ``` diff --git a/src/babashka/interpreter.clj b/src/babashka/interpreter.clj index aa3a18de..15be74b7 100644 --- a/src/babashka/interpreter.clj +++ b/src/babashka/interpreter.clj @@ -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))) diff --git a/src/data_readers.clj b/src/data_readers.clj index 65dead1b..587e1b48 100644 --- a/src/data_readers.clj +++ b/src/data_readers.clj @@ -1 +1,2 @@ -{bb/fn babashka.interpreter/read-fn} +{f babashka.interpreter/read-fn + r babashka.interpreter/read-regex}