Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
inconvergent committed Feb 28, 2024
1 parent ff576a5 commit 355a54d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 66 deletions.
100 changes: 47 additions & 53 deletions bin/ex.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,55 @@
(ql:quickload :lqn :silent t)
(in-package :lqn)

(print (qry "1 x 1 x 7 x 100" (splt _ :x) int!? (*fld 0 (+ _)))) ;; 109
(print (qry "1 x 1 x 7 x 100" (splt _ :x) int!? (*fld 0 +))) ;; 109
(print (qry "1 x 1 x 7 x 100" (splt _ :x) int!? (*fld 0 acc (- _ acc)))) ;; 93
; (print (qry "1 x 1 x 7 x 100" (splt _ :x) int!? (*fld 0 (+ _)))) ;; 109
; (print (qry "1 x 1 x 7 x 100" (splt _ :x) int!? (*fld 0 +))) ;; 109
; (print (qry "1 x 1 x 7 x 100" (splt _ :x) int!? (*fld 0 acc (- _ acc)))) ;; 93

(pretty-json
(qry "abc x def x gehiil x iiii"
(splt _ :x) str!? trim ; split and trim
(new$ :in (par) ; new kv with input
:len (pnum) ; input length
:items #((new$ :s _ :len (inum)))))) ; and substr lengths
;; { "in": "abc x def x gehiil x iiii",
;; "len": 25,
;; "items": [ { "s": "abc", "len": 3 }, { "s": "def", "len": 3 },
;; { "s": "gehiil", "len": 6 }, { "s": "iiii", "len": 4 } ] }
; (pretty-json
; (qry "abc x def x gehiil x iiii"
; (splt _ :x) str!? trim ; split and trim
; (new$ :in (par) ; new kv with input
; :len (pnum) ; input length
; :items #((new$ :s _ :len (inum)))))) ; and substr lengths
; ;; { "in": "abc x def x gehiil x iiii",
; ;; "len": 25,
; ;; "items": [ { "s": "abc", "len": 3 }, { "s": "def", "len": 3 },
; ;; { "s": "gehiil", "len": 6 }, { "s": "iiii", "len": 4 } ] }

(print (ldnout (qry #("1 x 1 x 7 x 100" "3 x 8 x 30")
#((splt _ :x) int!? ; for each row, split and parse as int
(*fld 0 +))))) ; sum each row
;; #(109 41)
; (print (ldnout (qry #("1 x 1 x 7 x 100" "3 x 8 x 30")
; #((splt _ :x) int!? ; for each row, split and parse as int
; (*fld 0 +))))) ; sum each row
; ;; #(109 41)

(print (qry "aaayyy x abc x def x auiuu x aaaaa"
(splt _ :x) trim ; split
#((?xpr :a :-@b sup (str! _ :-miss))))) ; search & replace
;; #("AAAYYY" "abc-miss" "def-miss" "AUIUU" "AAAAA")
; (print (qry "aaayyy x abc x def x auiuu x aaaaa"
; (splt _ :x) trim ; split
; #((?xpr :a :-@b sup (str! _ :-miss))))) ; search & replace
; ;; #("AAAYYY" "abc-miss" "def-miss" "AUIUU" "AAAAA")

(print (qry #((a b xxx) (a b c) (a b (c xxx)))
#((?txpr (msym? _ xxx) ; recursive search & replace
(symb _ :-HIT---))))) ; with new symbol
;; #((A B XXX-HIT---) (A B C) (A B (C XXX-HIT---)))
; (print (qry #((a b xxx) (a b c) (a b (c xxx)))
; #((?txpr (msym? _ xxx) ; recursive search & replace
; (symb _ :-HIT---))))) ; with new symbol
; ;; #((A B XXX-HIT---) (A B C) (A B (C XXX-HIT---)))

(print (qry #((a bbbxxx xxx) (a b c) (a b (c xxx)))
; symbols with "xxx", but not "bbb"
(?txpr (-@msym? _ "bbb") (+@msym? _ "xxx")
(sym! _ :-HIT---))))
;; #((A BBBXXX XXX-HIT---) (A B C) (A B (C XXX-HIT---)))
; (print (qry #((a bbbxxx xxx) (a b c) (a b (c xxx)))
; ; symbols with "xxx", but not "bbb"
; (?txpr (-@msym? _ "bbb") (+@msym? _ "xxx")
; (sym! _ :-HIT---))))
; ;; #((A BBBXXX XXX-HIT---) (A B C) (A B (C XXX-HIT---)))

(pretty-json (jsnqryf (internal-path-string "data/sample.json")
(|| #{:_id (:things #[:name])})))
;; [ { "_id": "65679d23d38d711eaf999e89",
;; "things": [ "Chris" ] },
;; { "_id": "65679d23fe33bc4c240675c0",
;; "things": [ "Winters", "Haii", "Klein" ] },
;; { "_id": "65679d235b4143d2932ea17a",
;; "things": [ "Star", "Ball" ] } ]
; (pretty-json (jsnqryf (internal-path-string "data/sample.json")
; (|| #{:_id (:things #[:name])})))
; ;; [ { "_id": "65679d23d38d711eaf999e89",
; ;; "things": [ "Chris" ] },
; ;; { "_id": "65679d23fe33bc4c240675c0",
; ;; "things": [ "Winters", "Haii", "Klein" ] },
; ;; { "_id": "65679d235b4143d2932ea17a",
; ;; "things": [ "Star", "Ball" ] } ]

(print (lqn:qry #(0 1) (?rec (< (@ -1) 10000)
(cat* _ (apply* + (tail _ 2))))
#((str! "- " (cnt) ": " _))
(join _ #\Newline)))
; (print (lqn:qry #(0 1) (?rec (< (@ -1) 10000)
; (cat* _ (apply* + (tail _ 2))))
; #((str! "- " (cnt) ": " _))
; (join _ #\Newline)))
;; - 0: 0
;; - 1: 1
;; - 2: 1
Expand All @@ -62,23 +62,17 @@
;; - 5: 5
;; ...
(print :--------------------)
; (print (lqn:ldnout (lqn::qrydb
; (lqn:jsnloads "[{\"a\": 1, \"b\": 2}, {\"a\": 11, \"b\": 12}]")
; #{:a (:b (val)) (:cnt (cnt)) (:val (val)) (:key (key)) (:par (par))})))
(print )

; (print (lqn:ldnout (lqn::qrydb
; (lqn:jsnloads "[{\"a\": 1, \"b\": 2}, {\"a\": 11, \"b\": 12}]")
; #[:a (:b (val)) (:cnt (cnt)) (:val (val)) (:key (key)) (:par (par))])))
(print )

; (print (lqn:ldnout (lqn::qrydb
; (lqn:jsnloads "{\"a\": 1, \"b\": 2}")
; {:a (:b (val)) (:cnt (cnt)) (:val (val)) (:key (key)) (:par (par))})))
(print )

(print (lqn:ldnout (lqn::qrydb
(lqn:jsnloads "[1, 2, 3]")
(*fld 0 (+ _ (val) )))))
(*fld 0 (+ _ (progn (print (key)) (val)) )))))

(print (lqn:ldnout (lqn::qrydb
(lqn:jsnloads "[1, 2, 3]")
(*fld 0 (list _)))))
(*fld 0 (list (print _))))))

2 changes: 1 addition & 1 deletion src/init.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
(defvar *opt* '(optimize (speed 3) (safety 1)))
(defvar *fxns* '(:err :wrn :nope :noop :lst :lit :qt :hld :ghv :pnum :inum :cnt
:fmt :out :jsnstr
:fn :fi :ctx :par :itr :key :compct :?? :@@ :@*
:fn :fi :ctx :par :itr :key :val :compct :?? :@@ :@*
:read? :some? :all? :none? :smth? :size?
:new* :new$ :cat* :cat$
:ind* :sel :seq :apply* :grp :uniq
Expand Down
7 changes: 3 additions & 4 deletions src/pre-qry.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
)
,@body)))))

(defmacro ∈ ((&key par cnt itr key val) &body body)
(defmacro ∈ ((&key par cnt itr key val) &body body) ; env
(declare (symbol par cnt itr))
`(labels (,@(when par `((par () ,par)
(pnum () (size? ,par))))
Expand Down Expand Up @@ -98,9 +98,8 @@
(loop for q in (pre/scan-clauses qq '#:pipe) collect
(if (dat? q) (kw q)
(typecase q (cons q) (boolean q)
(keyword `(** ,q))
(symbol `(*map ,q))
(string `(** ,q)) (vector `(*map ,@(coerce q 'list)))
(keyword `(** ,q)) (string `(** ,q))
(symbol `(*map ,q)) (vector `(*map ,@(coerce q 'list)))
(otherwise q)))))

(defun pre/*map (q &optional (mm :+)) (unless q (warn "*map: missing args."))
Expand Down
41 changes: 33 additions & 8 deletions test/test-lqn.lisp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(in-package #:lqn-tests)

(plan 5)
(plan 6)

(subtest "lqn qry preproc"
(is (lqn::pre/$$ '(:ccc :ddd "IIUJ" "%@UU" :?@aa :?@bb ("cc" (progn _)) (:+ "ABC" (print _)) (:% "ABC" _) (:kkk "ABC" _)))
Expand All @@ -11,7 +11,14 @@
(:% (AND (LQN:STR? :_) (LQN:SUB? :_ "UU"))) (:? (WHEN (AA :_) :_)) (:? (WHEN (BB :_) :_)) (:? ("cc" (PROGN _))) (:? (% "ABC" (PRINT _)))
(:% (AND (LQN:STR? :_) (LQN:SUB? :_ "ABC"))))))

(subtest "modifiers"
(subtest "lqn qry identities"
(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-2-fn* _)) (lqn::jsnstr (lqn:jsnqryf *test-data-2-fn* ($$ _)))))

(subtest "top-level modifiers"
(is (lqn:qry #("abc" "def") [(sub? _ :s@a)]) #("abc") :test #'equalp)
(is (lqn:qry "abc x def x hij" nil) nil)
(is (lqn:qry "aa" s@_) "aa")
Expand All @@ -22,12 +29,30 @@
(is (lqn:qry 1 (progn (s@progn _))) "1")
(is (lqn:qry "abc x def x hij" ∅) nil))

(subtest "lqn qry identities"
(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-2-fn* _)) (lqn::jsnstr (lqn:jsnqryf *test-data-2-fn* ($$ _)))))
(subtest "env fxns"
(is (lqn:ldnout
(lqn:qry (lqn:jsnloads "[{\"a\": 1, \"b\": 2}, {\"a\": 11, \"b\": 12}]")
#{:a (:b (val)) (:cnt (cnt)) (:key (key)) (:par (par))}))
#(((:A . 1) (:B . 2) (:CNT . 0) (:KEY . "key")
(:PAR . #(((:A . 1) (:B . 2)) ((:A . 11) (:B . 12)))))
((:A . 11) (:B . 12) (:CNT . 1) (:KEY . "key")
(:PAR . #(((:A . 1) (:B . 2)) ((:A . 11) (:B . 12)))))) :test #'equalp)
(is (lqn:ldnout
(lqn:qry (lqn:jsnloads "[{\"a\": 1, \"b\": 2}, {\"a\": 11, \"b\": 12}]")
#[:a (:b (val)) (:cnt (cnt)) (:key (key)) (:par (par))]))
#(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 (val)) (:cnt (cnt)) (:key (key)) (:par (par))}))
'((:A . 1) (:B . 2) (:CNT . 0) (:KEY . "key")
(:PAR (:A . 1) (:B . 2))) :test #'equalp)
; (is nil :test #'equalp)
; (is nil :test #'equalp)
; (is nil :test #'equalp)
)

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

0 comments on commit 355a54d

Please sign in to comment.