[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

! haphazard

bertrandmuscle

3/27/2005 1:03:00 AM

is ! haphazardly implemented for a reason?

yes: array.sort!

no: array.sort_by!
17 Answers

Daniel Amelang

3/27/2005 1:47:00 AM

0

Tell us what you mean by 'haphazardly' ?

I guess I just don't understand your question.

Dan


Lyle Johnson

3/27/2005 3:32:00 AM

0

On Sun, 27 Mar 2005 10:47:15 +0900, Daniel Amelang
<daniel.amelang@gmail.com> wrote:

> Tell us what you mean by 'haphazardly' ?
>
> I guess I just don't understand your question.

The first two times I read it, I thought he was referring to a
"!haphazardly" method, which was one I hadn't heard of yet.

I think what he was trying to ask was: Is there some reason why
certain methods, like Array#sort, have both a "bang" version (i.e.
Array#sort!) and a non-bang version (i.e. Array#sort), while other
very similar methods (like Array#sort_by) only offer one of those
choices? Why the inconsistency?


bertrandmuscle

3/27/2005 3:42:00 AM

0

>>Tell us what you mean by 'haphazardly' ?
>>
>>I guess I just don't understand your question.
>
>
> The first two times I read it, I thought he was referring to a
> "!haphazardly" method, which was one I hadn't heard of yet.
>
> I think what he was trying to ask was: Is there some reason why
> certain methods, like Array#sort, have both a "bang" version (i.e.
> Array#sort!) and a non-bang version (i.e. Array#sort), while other
> very similar methods (like Array#sort_by) only offer one of those
> choices? Why the inconsistency?


That's correct, thank you.

Daniel Amelang

3/27/2005 6:53:00 AM

0

Gotcha. Well, I can tell you firsthand about the controversies of the
'bang' methods (like 'sort!', etc). Since they really aren't a
necessity (you can get along with just the non-bang versions) and ruby
has only relatively recently acquired sort_by, I bet they just didn't
get around to writing the bang version of sort_by.

FYI, bang methods have been found to be only slightly more efficient
than their non-bang versions ('sort' and 'sort!' being pretty much the
same, efficiency-wise) In case it helps, we discussed bang methods
quite extensively last week:

http://ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-t...
http://ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-t...

I've recently fallen back to only using the non-bang version of
methods and the only thing I've missed is the expressiveness of my
code (e.g. c'mon you 'reject!', 'squeeze!' the 'next!' 'sub!', you
'succ!'er)

It's late.

Dan


vruz

3/27/2005 7:40:00 AM

0

> I've recently fallen back to only using the non-bang version of
> methods and the only thing I've missed is the expressiveness of my
> code (e.g. c'mon you 'reject!', 'squeeze!' the 'next!' 'sub!', you
> 'succ!'er)
> It's late.

reminds me of the funny headlines from El Reg when they tease Yahoo
for their promiscuous use of the exclamation symbol
http://www.theregister.co.uk/2005/02/10/yahoo_mozilla_fir...
http://www.theregister.co.uk/2005/03/02/yahoo_rebrands...
http://www.theregister.co.uk/2003/10/31/yahoo_takes_overture_...


bertrandmuscle

3/27/2005 8:07:00 PM

0

Thank you for the information.

It doesn't seem that you can chain them either, which is too bad.

I'm just surprised by this-- that's a pretty glaring inconsistency. It
should be in everything or nothing.



Daniel Amelang wrote:
> Gotcha. Well, I can tell you firsthand about the controversies of the
> 'bang' methods (like 'sort!', etc). Since they really aren't a
> necessity (you can get along with just the non-bang versions) and ruby
> has only relatively recently acquired sort_by, I bet they just didn't
> get around to writing the bang version of sort_by.
>
> FYI, bang methods have been found to be only slightly more efficient
> than their non-bang versions ('sort' and 'sort!' being pretty much the
> same, efficiency-wise) In case it helps, we discussed bang methods
> quite extensively last week:
>
> http://ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-t...
> http://ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-t...
>
> I've recently fallen back to only using the non-bang version of
> methods and the only thing I've missed is the expressiveness of my
> code (e.g. c'mon you 'reject!', 'squeeze!' the 'next!' 'sub!', you
> 'succ!'er)
>
> It's late.
>
> Dan
>
>

Florian Frank

3/27/2005 10:05:00 PM

0

Daniel Amelang wrote:

>Gotcha. Well, I can tell you firsthand about the controversies of the
>'bang' methods (like 'sort!', etc). Since they really aren't a
>necessity (you can get along with just the non-bang versions) and ruby
>has only relatively recently acquired sort_by, I bet they just didn't
>get around to writing the bang version of sort_by.
>
>
sort! sorts an array in place, while sort always makes a copy from the
original array. This distinction wouldn't be sensible for sort_by,
because it's actually a Schwartzian Transform, that always creates
temporary arrays:

class Array
def sort_by
map { |x| [ x, yield(x) ] }.sort { |a,b|
a.last <=> b.last
}.map { |p| p.first }
end
end

The transform makes a lot of sense if the yielded computations is very
expensive, because O(n) < O(n*log(n)). It's good to know about the trade
off between creating temporary arrays and the computation of the sort
keys, if one has to choose between sort(!) and sort_by.

--
Florian Frank



bertrandmuscle

3/27/2005 11:31:00 PM

0

Florian Frank wrote:
> Daniel Amelang wrote:
>
>> Gotcha. Well, I can tell you firsthand about the controversies of the
>> 'bang' methods (like 'sort!', etc). Since they really aren't a
>> necessity (you can get along with just the non-bang versions) and ruby
>> has only relatively recently acquired sort_by, I bet they just didn't
>> get around to writing the bang version of sort_by.
>>
>>
> sort! sorts an array in place, while sort always makes a copy from the
> original array. This distinction wouldn't be sensible for sort_by,
> because it's actually a Schwartzian Transform, that always creates
> temporary arrays:
>
> class Array
> def sort_by
> map { |x| [ x, yield(x) ] }.sort { |a,b|
> a.last <=> b.last
> }.map { |p| p.first }
> end
> end
>
> The transform makes a lot of sense if the yielded computations is very
> expensive, because O(n) < O(n*log(n)). It's good to know about the trade
> off between creating temporary arrays and the computation of the sort
> keys, if one has to choose between sort(!) and sort_by.
>

this is very interesting, thank you.

but for myself and perhaps many other users it comes down to how
intuitive the language is. from this perspective: if sort! works,
sort_by! should also.

one of the things that drew me to ruby recently was this philosophy, but
there have been some off-putting inconsistencies.

Eric Hodel

3/28/2005 1:23:00 AM

0

On 27 Mar 2005, at 15:34, bertrandmuscle@yahoo.com wrote:

> but for myself and perhaps many other users it comes down to how
> intuitive the language is. from this perspective: if sort! works,
> sort_by! should also.
>
> one of the things that drew me to ruby recently was this philosophy,
> but there have been some off-putting inconsistencies.

There are two very important things to remember...

Ruby is perfectly intuitive to Matz.

If you stick around, Matz will program your brain.

(Also,

A foolish consistency is the hobgoblin of little minds.)

--
Eric Hodel - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

dblack

3/28/2005 2:04:00 AM

0