From: "Ara.T.Howard" Date: 2004-08-11T06:11:15+09:00 Subject: Re: empty? and size in Enumerable On Tue, 10 Aug 2004, Martin DeMello wrote: > Robert Klemme wrote: >> "Martin DeMello" schrieb im Newsbeitrag >>> >>> Can empty? be reliably implemented in terms of each for every >>> enumerable? I can't think of an obvious problem with it, but that >>> doesn't mean there isn't one. >> >> Yes, that's possible IMHO. My suggestion would be: >> >> module Enumerable >> # O(1) >> def empty? >> each { return false } >> true >> end > > My concern was if #each created a temporary data structure in O(n) time > (e.g. an ordered hash) - this would make sense for #each, since its > running time is O(n) anyway, but would be a large hit for empty? my concern would be for code that created a temporary data structure to iterate - the designer should re-design! ;-) seriously, the rbtree package from the raa works exactly like an ordered hash and it does not do this - i would say it's a serious flaw if an ordered container cannot iterate itself in order w/o creating temp structures - though your point is well taken and correct. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | A flower falls, even though we love it; | and a weed grows, even though we do not love it. | --Dogen ===============================================================================