From: Yukihiro Matsumoto Date: 2007-10-30T16:12:53+09:00 Subject: Re: new Enumerable.butfirst method Hi, In message "Re: new Enumerable.butfirst method" on Tue, 30 Oct 2007 13:41:39 +0900, David Flanagan writes: |You wrote in your recent changelog entry: | | * enum.c (enum_butfirst): add a new method to iterates over | elements but first n. RDoc need to be updated. | |I'd be happy to update the rdoc comment, but it looks okay as it is to |me. Did you just want an example added? Or something else? I was not confident about my English description. If it's OK, it's OK. Thanks. |I should say, however, that I don't care for the name 'butfirst'. It's a traditional name (but slightly different behavior) inherited from Logo. In Logo, butfirst is cdr in Lisp, which returns given list with its first element removed. |I've got three alternatives to propose: | |1) How about 'skip' (or 'discard') instead, which would nicely support |its use as an enumerator like this: | | c.skip(n).map {|x|...} # map all but the first n | |If you like this, I'd also propose 'only' (or 'limit'): | | c.only(n).map { |x| ...} # iterates no more than n elements from c We already have #first, that works in reverse of #butfirst. I am not sure skip/only pair is better, especially when #only would work same as #first. |2) Another idea would be to be more verbose than 'butfirst': |each_but_first, which is much clearer and is similar to the existing |each_with_index. Then the opposite method would be each_but_only to |iterate only the first n? It's redundant, when we want to call #butfirst on Enumerator, e.g. (1..1000).map{|x| x*2}.butfirst(2).with_index{|x,i| p [x,i]} |3) Is there some way to integrate your butfirst with the earlier added |take and drop methods? If you removed the form of drop and take that |accept a block, and modified them to be iterators instead of returning |an array, then you'd end up with this butfirst, with a much more natural |(in my opinion) name. Hmm, drop (which is new in 1.9) to work as butfirst may be a better idea than just adding butfirst. Let me consider. matz.