Robert Dober
4/10/2007 4:11:00 PM
On 4/10/07, Brian Adkins <lojicdotcomNOSPAM@gmail.com> wrote:
> On Tue, 10 Apr 2007 16:52:03 +0900, Robert Dober wrote:
>
> > On 4/10/07, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> >> Brian Adkins wrote:
> >> > 3) Minor point, but apparently Python has a built in constant for the set
> >> > of lower case characters "string.lowercase", so I just defined a constant.
> >> ...
> >> > LETTERS = 'abcdefghijklmnopqrstuvwxyz'
> >>
> >> A minor comment: you can construct this string with
> >>
> >> ("a".."z").to_a.join
> >> => "abcdefghijklmnopqrstuvwxyz"
> >
> > Almost nobody, not even the gurus ever use [*a..b] instead of (a..b).to_a.
>
> Interesting. I didn't realize that would work with ranges. Here's what I
> read in "Programming Ruby" p. 348:
>
> "Following these parameters may be a single parameter prefixed with an
> asterisk. If this parameter is an array, Ruby replaces it with zero or
> more parameters corresponding to the elements of the array."
>
> I guess to_a is called on the range implicitly prior to evaluating *.
Hmm I guessed rather not, but I am wrong:
429/5 > cat ast.rb
# vim: sts=2 sw=2 tw=0 expandtab nu:
a = [*1..2]
robert@PC:~/log/ruby/tests/theory 18:09:04
430/6 > ruby -rprofile ast.rb
% cumulative self self total
time seconds seconds calls ms/call ms/call name
0.00 0.00 0.00 1 0.00 0.00 Enumerable.to_a
0.00 0.00 0.00 1 0.00 0.00 Range#each
0.00 0.01 0.00 1 0.00 10.00 #toplevel
Good thinking there Brian !!
Cheers
Robert
>
>
> > The performance is about the same, the former seems faster for short
> > arrays/ranges ~ 500
> > and from about 1k elements the later gets slightly faster.
> > I feel that [*a..b] is the right/better/more logical construct to use
> > if one wants an array, or do I miss something?
Well I did as shown above, but I still rather go for the shorter ;)
> >
> > Cheers
> > Robert
> >>
> >> --
> >> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
> >>
> >>
>
>
>
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw