by aviramha on 11/19/22, 6:33 PM with 126 comments
by derriz on 11/19/22, 8:35 PM
This inconsistency infuriated me when I discovered it but the Javadoc for Double.equals explicitly states that this anomaly is there to "allow hash tables to work properly".
by halpmeh on 11/19/22, 7:30 PM
by martisch on 11/19/22, 7:38 PM
For maps with keys that are reflexive with == the Go compiler already optimizes the range loop to a single efficient runtime map clear call: https://go-review.googlesource.com/c/go/+/110055
by inglor on 11/19/22, 7:40 PM
by ch_123 on 11/19/22, 8:07 PM
by _old_dude_ on 11/19/22, 8:32 PM
[1] https://docs.oracle.com/en/java/javase/19/docs/api/java.base...
by karmakaze on 11/19/22, 10:29 PM
by akira2501 on 11/19/22, 8:14 PM
by zmj on 11/19/22, 8:15 PM
by chubot on 11/19/22, 9:06 PM
I don’t think there’s any real use case for it
I’d say clear() is good for clarity, and that’s it
by kazinator on 11/19/22, 10:10 PM
by kazinator on 11/19/22, 10:51 PM
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)))
by yawaramin on 11/19/22, 8:31 PM
by yencabulator on 11/20/22, 11:21 PM
m = make(map[A]B, cap(m))
just like it already recognized for k := range m {
delete(m, k)
}
and many other similar idioms.(cap because len is not quite the same -- note, cap is not currently defined on maps)
EDIT: Likely because that's an assigment on m, not a mutation of it, so it can't be done e.g. in a function that gets m as argument.
by unwind on 11/19/22, 8:49 PM
I had to try it in Python (3.10.4, Windows on x86), it worked fine:
>>> import math
>>> a={math.nan: 1}
>>> a
{nan: 1}
>>> del(a[math.nan])
>>> a
{}
by kangalioo on 11/19/22, 8:27 PM
by anontrot on 11/20/22, 8:16 AM