Replies: 2 comments 9 replies
-
As a specific example, I wanted to write these macros: (defmacro set-item [var-name item-keys rhs]
"Convenience syntax for `(setv (get v ...))`."
`(setv (get ~var-name ~item-keys) ~rhs))
(defmacro set-attr [var-name attr-names rhs]
"Convenience syntax for `(setv (. v ...))`."
`(setv (. ~var-name ~attr-names) ~rhs))
(defmacro replace-item [var-name item-keys rhs]
"Anaphoric version of set-attr, with the item bound to `it`."
`(let [it (get ~var-name ~@item-keys)]
(setv (get ~var-name ~@item-keys) ~rhs)))
(defmacro replace-attr [var-name item-keys rhs]
"Anaphoric version of set-attr, with the item bound to `it`."
`(let [it (. ~var-name ~@item-keys)]
(setv (. ~var-name ~@item-keys) ~rhs))) Working examples: (set-item foo "x" (bar))
(set-item foo ["x" "y"] (bar))
(set-item foo #("x" "y") (bar))
(set-attr foo x (bar))
(set-attr foo ["x"] (bar))
(replace-item foo ["x"] (it.astype "float"))
(replace-item foo ["x" "a" "b"] (it.astype "float"))
(replace-attr foo [x] (it.astype "float"))
(replace-attr foo [x a b] (it.astype "float")) However, I also wanted to be able to support this syntax, allowing a standalone atom instead of a list literal: (replace-item foo "x" (it.astype "float"))
(replace-attr foo x (it.astype "float")) But this would require me to figure out inside the macro that |
Beta Was this translation helpful? Give feedback.
-
It sounds like the notion of "atom" you're aiming for is simply "non-sequential models", so try Does that fit the bill? |
Beta Was this translation helpful? Give feedback.
-
There seems to be no straightforward way to tell if a Hy form is an "atom". I ran into this issue when writing a macro in which I wanted to change the behavior depending on the kind of form that the user provided as input.
It looks like
hy.models.Symbol
,hy.models.String
, etc. don't have a common base class that indicates their "atomicity", onlyhy.models.Object
which is too general.Similarly, there doesn't seem to be any way to tell if something is a "number". You'd have to write
(isinstance form (hy.models.Integer hy.models.Float, hy.models.Complex))
, which of course one could wrap up into a predicate (maybemodel-number?
), but it seems flaky to write such utilities in user code, particularly with how much of a moving target the Hy language is.Would it be possible to add sufficient class structure, or to add a collection of predicate functions, to distinguish among numbers, string-like forms (
String
,FString
), collection-like forms (List
, etc.), and atoms vs. composite forms?Beta Was this translation helpful? Give feedback.
All reactions