[#3986] Re: Principle of least effort -- another Ruby virtue. — Andrew Hunt <andy@...>

> Principle of Least Effort.

14 messages 2000/07/14

[#4043] What are you using Ruby for? — Dave Thomas <Dave@...>

16 messages 2000/07/16

[#4139] Facilitating Ruby self-propagation with the rig-it autopolymorph application. — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/07/20

[ruby-talk:03771] Re: Array.uniq! returning nil

From: Aleksi Niemel<aleksi.niemela@...>
Date: 2000-07-03 17:29:27 UTC
List: ruby-talk #3771
> |Why Array.uniq! returns nil if it doesn't do anything (every element 
> |is unique). Same for Array.compact! Next example requires two times 
> |the memory what uniq! -version would. Could be a problem 
> with large texts.
> 
> Ruby 1.5.x do the magic for you.  Non bang methods don't copy memory
> regions unless copying is really needed, using the state-of-the-art
> copy-on-write technology. ;-)

That's good. *Waiting eagerly* :). But even then I guess

  uniqChars = text.split(//).uniq

needs a copy if there're any characters to be removed from the array. And if
we can't rely on that fact there are always some chars to be removed, it
seems we can't use Array.uniq!. It's all ok, I can always write

  chars = text.split(//)
  chars.uniq!
  uniqChars = chars

What bothers me is that it takes one temporary variable (or not, if you
allow misleading variable names :), two extra lines (or one) thus harder to
read, and most of all, I have to remember uniq! could return nil, and code
accordingly. And that uniq is not equivalent for dup.uniq!.

	- Aleksi

In This Thread

Prev Next