squint

Takis_ 2024-12-28T11:13:57.090679Z

can i make this to work in squint? the operator-mappings doesn't work i think, i need it for a mongodb dsl query that i made in past

(defn add [& es]
  {"$add" (vec es)})

(def operators-mappings
  '[+_ add])

(defmacro q [args]
  `(let ~operators-mappings
     ~args))

(prn (q (+_ 2 3)))

;;prints {"$add" [2 3]}   in clojure and clojurescript

borkdude 2024-12-28T11:17:00.163579Z

which version of clojurescript did you use before? macros don't just work like that in CLJS unless you use nbb or so

Takis_ 2024-12-28T11:36:53.128889Z

in cljs i put the macros in a .clj file, and in cljs with same name i used require-macros, my version was [org.clojure/clojurescript "1.10.866"]

Takis_ 2024-12-28T11:38:35.436629Z

i am overriding clojure.core symbols thats why want q to be local scope with those, for example this is a mongodb query, and inside it, i use clojure.core/cond for example to refer to the clj one :

(q :people.workers
   (< :salary 1000)
   (> :years 1)
   {:children (filter (fn [:child.] (< :child.age. 15)) :all-children)}
   (>= :children 2)
   {:bonus (reduce (fn [:total. :child.]
                     (cond (< :child.age. 5) (+ :total. 100)
                           (< :child.age. 10) (+ :total. 50)
                           :else (+ :total. 20)))
                   0
                   :children)}
   [:!id :name {:new-salary (+ :salary (if- (> :bonus 200) 200 :bonus))}]
   (sort :!new-salary))

Takis_ 2024-12-28T11:39:17.175779Z

i made mongodb query to be as if we write clojure https://github.com/tkaryadis/squery-mongojs

borkdude 2024-12-28T11:40:11.676149Z

you need to do a similar thing for squint, you need to use :require-macros but currently you can't use the same filename and the file has to be .cljs or .cljc you also need to add a squint.edn where you add :paths ["src"] and put the code into a source directory. so e.g.

(:require-macros [takis.macros ...])
and src/takis/macros.cljc

Takis_ 2024-12-28T11:45:27.238789Z

i read this in documentation, and i tried but i failed, do you know why this code doesn't work?

(defn add [& es]
  {"$add" (vec es)})

(def operators-mappings
  '[+_ add])
its not macro, i think it doesn't work

borkdude 2024-12-28T11:45:57.684649Z

what do you mean with "doesn't work"?

borkdude 2024-12-28T11:46:09.661339Z

aaah I know

borkdude 2024-12-28T11:46:33.769059Z

the quote doesn't work in squint, since symbols don't work in squint, they do not exist. but they work in macro namespaces

borkdude 2024-12-28T11:46:51.707189Z

squint only has JS data structures, no symbols or keywords

borkdude 2024-12-28T11:46:57.650709Z

keywords are translated to strings

borkdude 2024-12-28T11:47:02.661129Z

symbols simply don't exist

borkdude 2024-12-28T11:47:57.286869Z

if you move this to the macro namespace they should work since symbols do exist at compilation time

Takis_ 2024-12-28T11:48:14.088949Z

ok thank you, is there a simple example of using a macro in squint?

Takis_ 2024-12-28T11:48:20.742579Z

how to create one and use it

borkdude 2024-12-28T11:49:14.450719Z

I'll make an example project, after lunch

Takis_ 2024-12-28T11:49:54.933229Z

thank you, anytime you can : )

Takis_ 2024-12-28T12:02:50.643969Z

i made it working ike it worked in clojurescript, with 2 namespaces : macros.cljc

(ns macros)

(defn add [& es]
  "aaa")

(def operators-mappings
  '[+_ add])

(defmacro q [args]
    `(let ~operators-mappings
       ~args))
macros.cljs
(ns macros
  (:require-macros [macros]))
my normal app namespace
(ns App
  (:require [components.ExpenseItem :as ExpenseItem])
  (:require-macros [macros :refer [q]]))

(defn add [& es]
  "aaa")

(str (q (+_ 1 2)))

Takis_ 2024-12-28T12:03:16.777569Z

seems fine 🙂

borkdude 2024-12-28T12:16:17.223489Z

indeed! that's how you do it