In Janet, it can be very verbose to call functions that have a large number of keyword-based arguments. In JS this is somewhat simplified by the ability to write objects like so: { foo, bar, baz: 5 } which gets expanded to { foo: foo, bar: bar, baz: 5 }
It would be awesome if we had this for tables and structs in Janet too so that we could write { x y z } instead of { :x x :y y :z z }
In the meantime I'm living with this $: macro:
(defmacro $: [& args]
(var pairs @[])
(var i 0)
(while (< i (length args))
(let [x (args i)]
(if (keyword? x)
# Explicit key/value: :baz bazz
(do
(when (>= (+ i 1) (length args))
(error (string/format "$: missing value for keyword %j" x)))
(array/push pairs x)
(array/push pairs (args (+ i 1)))
(set i (+ i 2)))
# Shorthand symbol: foo => :foo foo
(do
(when (not (symbol? x))
(error (string/format "$: expected symbol or keyword, got %j" x)))
(array/push pairs (keyword x))
(array/push pairs x)
(set i (+ i 1))))))
~(table ,;pairs))
In Janet, it can be very verbose to call functions that have a large number of keyword-based arguments. In JS this is somewhat simplified by the ability to write objects like so:
{ foo, bar, baz: 5 }which gets expanded to{ foo: foo, bar: bar, baz: 5 }It would be awesome if we had this for tables and structs in Janet too so that we could write
{ x y z }instead of{ :x x :y y :z z }In the meantime I'm living with this
$:macro: