4 ms·
> This for loop is less efficient than clearing a map in one operation That is not obvious; a compiler could have a pattern match for that exact AST pattern, a
by kazinator 4y ago
> This for loop is less efficient than clearing a map in one operation
That is not obvious; a compiler could have a pattern match for that exact AST pattern, and transform it to a delete operation. (Except for that pesky issue where two fail to be equivalent due to NaN keys.)
Quick and dirty, not entirely correct proof-of-concept in TXR Lisp:
1> (macroexpand '(my-dohash (k v some.obj.hash) (print [some.obj.hash k])))
(dohash (k v some.obj.hash
())
(print [some.obj.hash
k]))
2> (macroexpand '(my-dohash (k v some.obj.hash) (del [some.obj.hash k])))
(clearhash hash)
Impl:
(defmacro my-dohash ((kvar vvar hash : result) . body)
(if-match @(require ((del [@hash @kvar]))
(null result))
body
^(clearhash hash)
^(dohash (,kvar ,vvar ,hash ,result) ,*body)))
- coder543 4y agohttps://news.ycombinator.com/item?id=33673792 https://news.ycombinator.com/item?id=33673792