Would it negatively impact performance to do a type check? If the input is a MapEntry, use the current key/val, and then use nth for other cases.
On Wednesday, 4 May 2016 10:52:35 UTC-7, Alex Miller wrote: > > Yes, there were some changes relating to tuples in the 1.8 alphas that > made 2-element vectors valid map entries. However, this was done by having > IPersistentVector extend IMapEntry which meant that (from a type > perspective) all vectors were valid map entries. In practice, this led to > some tricky conditional code both inside and outside Clojure itself to > account for the separate cases of generic vectors and 2-element vectors > acting as map entries (this comes up when doing generic tree walk > traversals on heterogeneous data). This caused some pretty subtle breakage. > In the end, nearly all of that was backed out prior to release of 1.8. It > may be considered again in the future - as we found, there are tradeoffs. > > Re performance, I think you'll find that key and val are many orders of > magnitude faster than first and second as they will take a sequence view of > entry. > > By no means rigorous, but a quick test (just reporting last timing): > > (def me (first {:a 1})) > > (dotimes [_ 20] (time (dotimes [_ 1000000] (key me)))) > "Elapsed time: 0.86 msecs" > > (dotimes [_ 20] (time (dotimes [_ 1000000] (val me)))) > "Elapsed time: 0.959 msecs" > > (dotimes [_ 20] (time (dotimes [_ 1000000] (first me)))) > "Elapsed time: 52.712 msecs" > > (dotimes [_ 20] (time (dotimes [_ 1000000] (second me)))) > "Elapsed time: 56.786 msecs" > > If you want something portable between map entries and vectors and also > fast, I would prefer nth: > > (dotimes [_ 20] (time (dotimes [_ 1000000] (nth me 0)))) > "Elapsed time: 0.573 msecs" > > (dotimes [_ 20] (time (dotimes [_ 1000000] (nth me 1)))) > "Elapsed time: 0.573 msecs" > > (def v [:a 1]) > (dotimes [_ 20] (time (dotimes [_ 1000000] (first v)))) > "Elapsed time: 48.3 msecs" > > (dotimes [_ 20] (time (dotimes [_ 1000000] (second v)))) > "Elapsed time: 54.236 msecs" > > (dotimes [_ 20] (time (dotimes [_ 1000000] (nth v 0)))) > "Elapsed time: 2.011 msecs" > > (dotimes [_ 20] (time (dotimes [_ 1000000] (nth v 1)))) > "Elapsed time: 2.093 msecs" > > > On Wednesday, May 4, 2016 at 11:52:40 AM UTC-5, adrian...@mail.yu.edu > <javascript:> wrote: >> >> I may be misremembering here, but I think this was briefly implemented >> for an alpha release of 1.8.0. I think the entire feature in question is >> being reworked for the future, but I'm uncertain of the details. Perhaps >> others can chime in with the background motivating those changes and >> reversions? >> >> On Wednesday, May 4, 2016 at 12:44:19 PM UTC-4, JvJ wrote: >>> >>> >>> I've noticed that, in Clojure, there is a MapEntry type that supports >>> the operations "key" and "val", but in Clojurescript, MapEntry is replaced >>> by simple 2-element vectors. >>> >>> Furthermore, 2-element vectors can be used in almost all other cases >>> where a MapEntry is expected, and MapEntries support all vector >>> operations,but "key" and "val" are >>> not supported on these vectors. >>> >>> Is there a particular design reason for this? It doesn't seem like much >>> more than an inconvenience >>> >> -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.