Skip to content

Commit

Permalink
rename *map to ?map
Browse files Browse the repository at this point in the history
  • Loading branch information
inconvergent committed Feb 28, 2024
1 parent cf15940 commit 409d5a6
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 45 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,16 @@ surrounds all terminal queries by default.
For convenience, particularly in the terminal, pipe has the following default
translations:
- `fx`: to `(*map (fx _))`: map `fx` across all items.
- `fx`: to `(?map (fx _))`: map `fx` across all items.
- `:word`: to `[(isub? _ "word")]` to filter by `"word"`.
- `"Word"`: to `[(sub? _ "Word")]` to filter all items by this `string` with
case.
- `(..)`: to itself. That is, expressions are not translated.
### Map Operator - `#()`/`*map`
### Map Operator - `#()`/`?map`
Map operations over `vector`:
- `#(fx)` or `(*map fx)`: map `(fx _)` across all items.
- `#(expr ..)` or `(*map expr ..)`: evaluate these expressions
- `#(fx)` or `(?map fx)`: map `(fx _)` across all items.
- `#(expr ..)` or `(?map expr ..)`: evaluate these expressions
sequentially on all items in `sequence`.
### Fold Operator - `*fld`
Expand Down
2 changes: 1 addition & 1 deletion bin/ex.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@
{\"a\": 11, \"b\": 123},
{\"a\": 11, \"b\": 123}
]")
(?grp (@ :a) (str! (key) (@ :b))))))
(*fld 0 (+ (@ :a)) ))))
4 changes: 2 additions & 2 deletions lqn.asd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(asdf:defsystem #:lqn
:description "Lisp Query Notation"
:version "1.16.0"
:version "1.17.0"
:author "anders hoff / @inconvergent / [email protected]"
:in-order-to ((asdf:test-op (asdf:test-op #:lqn/tests)))
:licence "MIT" :pathname "src/" :serial nil
Expand All @@ -19,7 +19,7 @@

(asdf:defsystem #:lqn/tests
:depends-on (#:lqn #:prove #:uiop #:asdf)
:version "1.16.0"
:version "1.17.0"
:perform (asdf:test-op (o s) (uiop:symbol-call ':lqn-tests '#:run-tests))
:pathname "test/" :serial t
:components ((:file "run")))
2 changes: 1 addition & 1 deletion src/init.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(defvar *qmodes* '(:+ :? :- :%))
(defvar *operators*
`(:*map :@ :|| ?rec :*$ :$$ :$* :** :*fld :?* :?xpr :?txpr :?mxpr :?srch :?grp))
`(:?map :@ :|| ?rec :*$ :$$ :$* :** :*fld :?* :?xpr :?txpr :?mxpr :?srch :?grp))
(defvar *opt* '(optimize (speed 3) (safety 1)))
(defvar *fxns* '(:err :wrn :nope :noop :lst :lit :qt :hld :ghv :pnum :inum :cnt
:fmt :out :jsnstr
Expand Down
8 changes: 4 additions & 4 deletions src/pre-qry.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@
(if (dat? q) (kw q)
(typecase q (cons q) (boolean q)
(keyword `(** ,q)) (string `(** ,q))
(symbol `(*map ,q)) (vector `(*map ,@(coerce q 'list)))
(symbol `(?map ,q)) (vector `(?map ,@(coerce q 'list)))
(otherwise q)))))

(defun pre/*map (q &optional (mm :+)) (unless q (warn "*map: missing args."))
(defun pre/?map (q &optional (mm :+)) (unless q (warn "?map: missing args."))
(labels ((unpack- (o) ; NOTE: can we use modes here?
(dsb (m sk) (unpack-mode o mm)
(unless (eq m :+) (error "*map: expected mode :+, got: ~a." m))
(unless (eq m :+) (error "?map: expected mode :+, got: ~a." m))
(etypecase sk (sequence sk) (keyword sk) (symbol `(,sk :_))))))
(let* ((q* (remove-if #'dat? (pre/scan-clauses q '#:*map)))
(let* ((q* (remove-if #'dat? (pre/scan-clauses q '#:?map)))
(res (mapcar #'unpack- q*))
(allres (if (= (length q) (length q*)) res (cons `(lit :_) res))))
(if (< (length allres) 2) allres `((|| ,@allres))))))
Expand Down
30 changes: 11 additions & 19 deletions src/qry.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
(3 `(@@ ,@(funcall rec conf d)))
(otherwise (error "@: expected 0-3 arguments. got: ~a." d))))

(defun compile/*map (rec conf d) ; (*map ...)
(when (zerop (length d)) (error "*map: missing args."))
(defun compile/?map (rec conf d) ; (?map ...)
(when (zerop (length d)) (error "?map: missing args."))
(awg (k i kres ires itr par)
(labels ((err () `(error "*map/rt: bad type. expected hash-table or vector:~%got: ~a." ,par))
(labels ((err () `(error "?map/rt: bad type. expected hash-table or vector:~%got: ~a." ,par))
(do-ht (expr) `(loop with ,kres = (new$)
for ,i from 0
for ,itr being the hash-values of ,par
Expand All @@ -39,7 +39,7 @@
(otherwise ,(err))))))
(let ((cd (car d)))
(typecase cd (list (do-map cd)) (vector (do-map cd))
(otherwise (error "*map: expected vector or cons. got: ~a." cd)))))))
(otherwise (error "?map: expected vector or cons. got: ~a." cd)))))))

(defun compile/*fld (rec conf d) ; (*fld ...)
(awg (i res itr par) ; 0 + ; 0 acc (+ acc _)
Expand All @@ -60,7 +60,6 @@
(3 (let ((d3 (third d))) (do-fld (car d) (second d) itr d3)))
(otherwise (error "*fld: bad args: ~a." d))))))

; pnum, inum?
(defun compile/$$ (rec conf d) ; {...} ; sel
(awg (kres par dat)
`(let* ((,par ,(gk conf :dat))
Expand Down Expand Up @@ -124,16 +123,12 @@
(defun get-modes (cd &rest mm)
(loop for (m expr) in (strip-all cd) if (member m mm :test #'eq) collect expr))

; TODO: env
(defun compile/** (rec conf d) ; [...] ; filter
(defun compile/** (rec conf d) ; [...] ; sel, filter
(awg (i ires itr par)
`(loop with ,ires of-type vector = (mav)
with ,par of-type vector = (vec! ,(gk conf :dat))
for ,itr across ,par for ,i from 0
do ,(compile/?xpr rec `((:par . ,par)
(:dat . ,itr)
(:i . ,i)
(:itr . ,itr))
do ,(compile/?xpr rec `((:par . ,par) (:dat . ,itr) (:cnt . ,i) (:itr . ,itr))
`(,@d (vex ,ires ,@(or (get-modes d :%) `(,itr))) nil))
finally (return ,ires))))

Expand All @@ -145,9 +140,8 @@
,@(when nots `((not (or ,@nots)))))))))))
(defun compile/?xpr (rec conf d) ; (xpr sel .. hit miss)
(labels ((do-last (d n) (mapcar (λ (d) (funcall rec conf (pre/or-all d))) (last d n))))
`(∈ (:par ,(gk conf :par t)
:cnt ,(gk conf :i t)
:itr ,(gk conf :itr t))
`(∈ (:par ,(gk conf :par t) :cnt ,(gk conf :cnt t)
:itr ,(gk conf :itr t) :key ,(gk conf :itr t))
,(case (length d) ((0 (error "xpr: missing args.")))
(1 (compile/?xpr/bool rec conf (pre/** d)))
(2 `(if ,(compile/?xpr/bool rec conf (pre/** (butlast d 1))) ,@(do-last d 1)))
Expand Down Expand Up @@ -190,11 +184,9 @@
(third d)) ,i (+ ,i 1))))
(values ,∇- ,i)))))

; TODO: rename *map->?map ; ??

; TODO: operate on hts too
; TODO: operate on hts too?
; TODO: smarter selectors? key, index as grpfx; aggfx
; TODO: check other ops for order of ops
(defun compile/?grp (rec conf d)
(unless (< 0 (length d) 3) (error "?grp: expected 1 or 2 args. got: ~a." d))
(awg (i kvres key itr par dat acc)
Expand All @@ -216,14 +208,14 @@
(cond
((dat? d) (gk conf :dat))
((stringp d) d) ; remember that this order is important
((vectorp d) (rec conf `(*map ,@(coerce d 'list))))
((vectorp d) (rec conf `(?map ,@(coerce d 'list))))
((atom d) d)
((qop? :|| d) (compile/|| #'rec conf (pre/|| (cdr d))))
((qop? :** d) (compile/** #'rec conf (pre/** (cdr d))))
((qop? :$* d) (compile/$* #'rec conf (pre/$$ (cdr d))))
((qop? :*$ d) (compile/*$ #'rec conf (pre/$$ (cdr d))))
((qop? :$$ d) (compile/$$ #'rec conf (pre/$$ (cdr d))))
((qop? :*map d) (compile/*map #'rec conf (pre/*map (cdr d))))
((qop? :?map d) (compile/?map #'rec conf (pre/?map (cdr d))))
((qop? :?xpr d) (compile/?xpr #'rec conf (cdr d)))
((qop? :@ d) (compile/@ #'rec conf (cdr d)))
((qop? :*? d) (compile/*? #'rec conf (cdr d)))
Expand Down
12 changes: 6 additions & 6 deletions test/test-lqn-2.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,22 @@
'((:A . "a01") (:B . "b12") (:C . "c23")) :test #'equalp)

(is (lqn:qry "aaayyy x abc x def x uuu x sss x auiuu x aaaaa"
(splt _ :x) (*map (?xpr :a :-@b sup sdwn)))
(splt _ :x) (?map (?xpr :a :-@b sup sdwn)))
#("AAAYYY" "abc" "def" "uuu" "sss" "AUIUU" "AAAAA") :test #'equalp)
(is (lqn:qry "aaayyy x abc x def x uuu x sss x auiuu x aaaaa"
(splt _ :x) (*map _ (?xpr :a :-@b sup sdwn)))
(splt _ :x) (?map _ (?xpr :a :-@b sup sdwn)))
#("AAAYYY" "abc" "def" "uuu" "sss" "AUIUU" "AAAAA") :test #'equalp)
(is (lqn:qry "aaayyy x abc x def x uuu x sss x auiuu x aaaaa"
(splt _ :x) (*map (?xpr "a" "-@b" sup sdwn)))
(splt _ :x) (?map (?xpr "a" "-@b" sup sdwn)))
#("AAAYYY" "abc" "def" "uuu" "sss" "AUIUU" "AAAAA") :test #'equalp)
(is (lqn:qry "aaayyy x abc x def x uuu x sss x auiuu x aaaaa"
(splt _ :x) (?txpr :a :-@b sup))
#("AAAYYY" "abc" "def" "uuu" "sss" "AUIUU" "AAAAA") :test #'equalp)
(is (lqn:qry "aaayyy x abc x def x uuu x sss x auiuu x aaaaa"
(splt _ :x) (*map (?xpr :a :-@b sup nil)))
(splt _ :x) (?map (?xpr :a :-@b sup nil)))
#("AAAYYY" NIL NIL NIL NIL "AUIUU" "AAAAA") :test #'equalp)
(is (lqn:qry "aaayyy x abc x def x uuu x sss x auiuu x aaaaa"
(splt _ :x) (*map (hld :k _) (?xpr :a :-@b (str! (sup _) (ghv :k)) sdwn)))
(splt _ :x) (?map (hld :k _) (?xpr :a :-@b (str! (sup _) (ghv :k)) sdwn)))
#("AAAYYYaaayyy" "abc" "def" "uuu" "sss" "AUIUUauiuu" "AAAAAaaaaa") :test #'equalp)

(is (lqn:qry "a b c x def x 27" (splt _ :x) :-@de) #("a b c" "27") :test #'equalp)
Expand Down Expand Up @@ -157,7 +157,7 @@
"[[\"extra99\"],[\"extra1\",\"extra2\"]]")

(is (lqn:qry "1 x 1 x 7 x 100" (splt _ :x) int!? (*fld 0 +)) 109)
(is (lqn:ldnout (lqn:qry "1 x 1 x 7 x 100" (splt _ :x) int!? (*map (new$ :v _ :n (cnt)))))
(is (lqn:ldnout (lqn:qry "1 x 1 x 7 x 100" (splt _ :x) int!? (?map (new$ :v _ :n (cnt)))))
#(((:V . 1) (:N . 0)) ((:V . 1) (:N . 1)) ((:V . 7) (:N . 2)) ((:V . 100) (:N . 3))) :test #'equalp)
(is (lqn:qry "1 x 1 x 7 x 100" (splt _ :x) int!? (*fld 0 (+ _))) 109)
(is (lqn:qry "1 x 1 x 7 x 100" (splt _ :x) int!? (*fld 1000 +)) 1109)
Expand Down
10 changes: 2 additions & 8 deletions test/test-lqn.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
(is (lqn::jsnstr (lqn:jsnqryf *test-data-fn* _)) (lqn::jsnstr (lqn:jsnqryf *test-data-fn* ($* _))))
(is (lqn::jsnstr (lqn:jsnqryf *test-data-fn* _)) (lqn::jsnstr (lqn:jsnqryf *test-data-fn* (*$ _))))
(is (lqn::jsnstr (lqn:jsnqryf *test-data-fn* _)) (lqn::jsnstr (lqn:jsnqryf *test-data-fn* (** _))))
(is (lqn::jsnstr (lqn:jsnqryf *test-data-fn* _)) (lqn::jsnstr (lqn:jsnqryf *test-data-fn* (*map _))))
(is (lqn::jsnstr (lqn:jsnqryf *test-data-fn* _)) (lqn::jsnstr (lqn:jsnqryf *test-data-fn* (?map _))))
(is (lqn::jsnstr (lqn:jsnqryf *test-data-2-fn* _)) (lqn::jsnstr (lqn:jsnqryf *test-data-2-fn* ($$ _)))))

(subtest "top-level modifiers"
Expand Down Expand Up @@ -43,7 +43,6 @@
#(1 2 0 "key"
#(((:A . 1) (:B . 2)) ((:A . 11) (:B . 12))) 11 12 1 "key"
#(((:A . 1) (:B . 2)) ((:A . 11) (:B . 12)))) :test #'equalp)

(is (lqn:ldnout
(lqn:qry (lqn:jsnloads "{\"a\": 1, \"b\": 2}")
{:a (:b _) (:cnt (cnt)) (:key (key)) (:par (par))}))
Expand All @@ -52,12 +51,7 @@
(is (lqn:ldnout
(lqn:qry (lqn:jsnloads "[{\"a\": 1, \"b\": 23}, {\"a\": 11, \"b\": 123}, {\"a\": 11, \"b\": 123} ]")
(?grp (@ :a) (str! (key) (@ :b)))))
'((1 . #("123")) (11 . #("11123" "11123"))) :test #'equalp)

; (is nil :test #'equalp)
; (is nil :test #'equalp)
; (is nil :test #'equalp)
)
'((1 . #("123")) (11 . #("11123" "11123"))) :test #'equalp))

(subtest "lqn qry 1"
(is (lqn:ldnout (lqn:jsnqryf *test-data-fn* (|| #{:_id (:things #[:name :?@extra])})))
Expand Down

0 comments on commit 409d5a6

Please sign in to comment.