[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: thoughts on a more generic Array#partition function

Yukihiro Matsumoto

7/4/2008 6:28:00 AM

Hi,

In message "Re: thoughts on a more generic Array#partition function"
on Fri, 4 Jul 2008 10:14:18 +0900, Pe=F1a, Botp <botp@delmonte-phil.com=
> writes:

|current test:
|
|1.9 not sorted
|1.8.6 not sorted
|1.8.7 sorted
|
|i prefer 1.8.7 default behaviour

Hmm, from my understanding, 1.9 preserves the order, prior version
does not. Could you show us the test you got the above conclusion?

matz.

3 Answers

Peña, Botp

7/4/2008 7:19:00 AM

0

From: Yukihiro Matsumoto [mailto:matz@ruby-lang.org]=20
# <botp@delmonte-phil.com> writes:
# |current test:
# |
# |1.9 not sorted
# |1.8.6 not sorted
# |1.8.7 sorted
# |
# |i prefer 1.8.7 default behaviour
# Hmm, from my understanding, 1.9 preserves the order, prior version
# does not. Could you show us the test you got the above conclusion?

sir Matz, as requested,

> [RUBY_VERSION, RUBY_RELEASE_DATE, RUBY_REVISION]
=3D> ["1.9.0", "2008-06-20", 17482]

> RUBY_PLATFORM
=3D> "i386-mswin32"

> a =3D [ 1, 2, 3, 4, 7, 9, 11, 12, 13, 14 ]
=3D> [1, 2, 3, 4, 7, 9, 11, 12, 13, 14]

> a.group_by { |i| i % 3 }
=3D> {1=3D>[1, 4, 7, 13], 2=3D>[2, 11, 14], 0=3D>[3, 9, 12]}

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at this point, 1.9 and 1.8.7 differs

> h=3Da.group_by { |i| i % 3 }
=3D> {1=3D>[1, 4, 7, 13], 2=3D>[2, 11, 14], 0=3D>[3, 9, 12]} =20

> h.values
=3D> [[1, 4, 7, 13], [2, 11, 14], [3, 9, 12]]

kind regards -botp

Robert Klemme

7/4/2008 7:27:00 AM

0

2008/7/4 Pe=F1a, Botp <botp@delmonte-phil.com>:
> From: Yukihiro Matsumoto [mailto:matz@ruby-lang.org]
> # <botp@delmonte-phil.com> writes:
> # |current test:
> # |
> # |1.9 not sorted
> # |1.8.6 not sorted
> # |1.8.7 sorted
> # |
> # |i prefer 1.8.7 default behaviour
> # Hmm, from my understanding, 1.9 preserves the order, prior version
> # does not. Could you show us the test you got the above conclusion?
>
> sir Matz, as requested,
>
>> [RUBY_VERSION, RUBY_RELEASE_DATE, RUBY_REVISION]
> =3D> ["1.9.0", "2008-06-20", 17482]
>
>> RUBY_PLATFORM
> =3D> "i386-mswin32"
>
>> a =3D [ 1, 2, 3, 4, 7, 9, 11, 12, 13, 14 ]
> =3D> [1, 2, 3, 4, 7, 9, 11, 12, 13, 14]
>
>> a.group_by { |i| i % 3 }
> =3D> {1=3D>[1, 4, 7, 13], 2=3D>[2, 11, 14], 0=3D>[3, 9, 12]}
>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> at this point, 1.9 and 1.8.7 differs

Where exactly? I cannot seem to see it. Above and below look
identical. What am I missing?

>> h=3Da.group_by { |i| i % 3 }
> =3D> {1=3D>[1, 4, 7, 13], 2=3D>[2, 11, 14], 0=3D>[3, 9, 12]}

I get

09:29:30 bas$ ruby --version
ruby 1.8.7 (2008-06-20 patchlevel 22) [i386-cygwin]
09:29:40 bas$ ruby -e 'p [ 1, 2, 3, 4, 7, 9, 11, 12, 13, 14 ].group_by
{ |i| i % 3 }'
{0=3D>[3, 9, 12], 1=3D>[1, 4, 7, 13], 2=3D>[2, 11, 14]}

09:29:43 bas$ ruby19 --version
ruby 1.9.0 (2008-03-01 revision 15664) [i386-cygwin]
09:29:47 bas$ ruby19 -e 'p [ 1, 2, 3, 4, 7, 9, 11, 12, 13, 14
].group_by { |i| i % 3 }'
{1=3D>[1, 4, 7, 13], 2=3D>[2, 11, 14], 0=3D>[3, 9, 12]}

And there *is* a difference.

Kind regards

robert

--=20
use.inject do |as, often| as.you_can - without end

Rick DeNatale

7/4/2008 3:31:00 PM

0

On Fri, Jul 4, 2008 at 3:27 AM, Robert Klemme <shortcutter@googlemail.com>
wrote:

> 2008/7/4 Pe=F1a, Botp <botp@delmonte-phil.com>:
> > From: Yukihiro Matsumoto [mailto:matz@ruby-lang.org]
> > # <botp@delmonte-phil.com> writes:
> > # |current test:
> > # |
> > # |1.9 not sorted
> > # |1.8.6 not sorted
> > # |1.8.7 sorted
> > # |
> > # |i prefer 1.8.7 default behaviour
> > # Hmm, from my understanding, 1.9 preserves the order, prior version
> > # does not. Could you show us the test you got the above conclusion?
>
> >> [RUBY_VERSION, RUBY_RELEASE_DATE, RUBY_REVISION]
> > =3D> ["1.9.0", "2008-06-20", 17482]
> >
> >> RUBY_PLATFORM
> > =3D> "i386-mswin32"
> >
> >> a =3D [ 1, 2, 3, 4, 7, 9, 11, 12, 13, 14 ]
> > =3D> [1, 2, 3, 4, 7, 9, 11, 12, 13, 14]
> >
> >> a.group_by { |i| i % 3 }
> > =3D> {1=3D>[1, 4, 7, 13], 2=3D>[2, 11, 14], 0=3D>[3, 9, 12]}
> >
> > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > at this point, 1.9 and 1.8.7 differs
>
> Where exactly? I cannot seem to see it. Above and below look
> identical. What am I missing?
>
> >> h=3Da.group_by { |i| i % 3 }
> > =3D> {1=3D>[1, 4, 7, 13], 2=3D>[2, 11, 14], 0=3D>[3, 9, 12]}
>
> I get
>
> 09:29:30 bas$ ruby --version
> ruby 1.8.7 (2008-06-20 patchlevel 22) [i386-cygwin]
> 09:29:40 bas$ ruby -e 'p [ 1, 2, 3, 4, 7, 9, 11, 12, 13, 14 ].group_by
> { |i| i % 3 }'
> {0=3D>[3, 9, 12], 1=3D>[1, 4, 7, 13], 2=3D>[2, 11, 14]}
>
> 09:29:43 bas$ ruby19 --version
> ruby 1.9.0 (2008-03-01 revision 15664) [i386-cygwin]
> 09:29:47 bas$ ruby19 -e 'p [ 1, 2, 3, 4, 7, 9, 11, 12, 13, 14
> ].group_by { |i| i % 3 }'
> {1=3D>[1, 4, 7, 13], 2=3D>[2, 11, 14], 0=3D>[3, 9, 12]}
>
> And there *is* a difference.
>

Yes there is. And I suspect that some following this thread are confused b=
y
the difference between sorted and ordered.

Ruby 1.8 (at least before 1.8.7) gives no specification of the order in
which a hash will yield it's keys, values or key-value pairs for methods in
the each family.

Ruby 1.9 keeps track of the INSERTION order so what we are seeing in the 1.=
9
output is a result of the fact that group_by inserts a key value pair each
time it encounters a NEW value from the block., so with the array [1, 2, 3,
...] the block (which returns element % 3) will first return 1, then 2, the=
n
0, after which all of the possible return values have been exhausted. So
the hash is ordered by key as 1 =3D> ... , 2=3D> ..., 3=3D>...

In the case of Ruby 1.8, the enumeration order is an accident of the way th=
e
key values hash, since the hash value of a fixnum n in MRI Ruby 1.8 is
n*2+1, in this case the enumeration order is somewhat predictable.

But in general hashes are not enumerated in key-sort order in any version
of Ruby as far as I know.

--=20
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...