From: David Flanagan Date: 2008-08-08T04:07:24+09:00 Subject: [ruby-core:18175] Re: New array methods cycle, choice, shuffle (plus bug in cycle) David A. Black wrote: > Hi -- > > On Thu, 7 Aug 2008, Yukihiro Matsumoto wrote: > >> Hi, >> >> In message "Re: [ruby-core:18164] Re: New array methods cycle, choice, >> shuffle (plus bug in cycle)" >> on Thu, 7 Aug 2008 18:35:28 +0900, gdefty@attglobal.net writes: >> I am against #sample! for the name. If one try to persuade me to add >> a method with the behavior, the name should be anything but #sample! >> >> |The problem with the shuffle-and-shift solutions >> |is that they do not scale well with the size of >> |the array. It is fine for small amounts of data >> |(e.g. the rock-paper-scissors example) but not how >> |I would expect a built-in method to be coded, >> |where the size of the array can not be known in >> |advance. >> >> I believe the calculation complexity of shuffle-once-and-pop is far >> lesser than pick-some-elements-and-remove-them, even with an array of >> thousands of elements. I see your point, Matz. I was thinking about avoiding the cost of the initial shuffle, but hadn't considered the cost of closing up all the holes in the array after sampling elements out. And a element-removing version of sample is only interesting if you're going to call it multiple times, so I agree with your analysis: shuffle and pop is more efficient. I believe I was the first one to request this sample! method, and I now withdraw that request. > I also agree with something I think you said before, which is that > sample! as described here seems to be doing two things. Usually the > "danger" of the bang fits very well with the receiver-changing > behavior of arrays (plus the secondary danger of the nil return). But > it's a stretch, I think, to think of a sampling as a removal > operation. It feels a bit like having first! remove the first element. > > > David I don't really get the objection to using a bang in the name, though. While its not strictly parallel, I can't see it confusing anyone. I'm replying to David's comment about this, though, because when I was trying to put together an argument *for* sample!, I was going to use a hypothetical first! as an example: it seems to me that anyone who was familiar with Ruby method naming conventions would understand that first!, if it existed, worked just like shift. first! and last! would certainly be easier for new programmers who haven't already been introduced to terms like shift and pop. (I'm not asking to add these methods--just saying that they seem perfectly reasonable to me!) David Flanagan