Hy言語でS式をそのまま表示する方法
Hy言語でS式をJSONから生成して表示しようしていたのだが、Pythonコードに直せられてしまったものが表示され困った。
(print '(+ 1 1)) ;; Output: ;; hy.models.Expression([ ;; hy.models.Symbol('+'), ;; hy.models.Integer(1), ;; hy.models.Integer(1)])
そのままS式として表示して欲しかったのでやり方を調べた。
hy.repr
を使用するとそのまま表示されるようだ。
(print (hy.repr '(+ 1 1))) ;; Output: ;; '(+ 1 1)
Hy言語で文字列をシンボルに変換する方法
この方法もなかなか見つけられなかったのでここに記しておく。
(import hy.models [Symbol]) (let [ text "hoge" sym (Symbol text)] (print (type text) text) (print (type sym) sym) (print (type 'hoge) 'hoge)) ;; Output: <class 'str'> hoge <class 'hy.models.Symbol'> hoge <class 'hy.models.Symbol'> hoge
Hy言語でタイプヒントを付ける方法
(require hyrule [of]) (import dataclasses [dataclass]) ;; n: int = 0と同じ (setv #^ int n 0) ; もしくは、(setv (annotate n int) 0) ;; @dataclass ;; class Hoge: ;; name: str ;; comment: Optional[str] = None ;; attrs: set[str] ;; と同じ (defclass [dataclass] Hoge [] #^ str name ; (annotate name str) (setv #^ (of Optional str) comment None) ; (setv (annotate comment (of Optional str)) None) #^ (of set str) attrs ; (annotate attrs (of set atr)) )
余談
Hyruleってハイラルって読むらしい。