[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: A method that doesn't change its arguments
From:       Ken Bloom <kbloom () gmail ! com>
Date:       2009-11-09 1:08:30
Message-ID: pan.2009.11.09.00.23.21 () gmail ! com
[Download RAW message or body]

On Sun, 08 Nov 2009 19:40:08 +0900, Timo Jeranko wrote:

> At the moment I'm doing something like this:
> 
> def some_method(array1)
>  ...
>   array2 = array1.clone
>  ...
>   array2.delete_at(i)
>  ...
>   return array2
>  ...
> end
> 
> 
> I would like array1 to remain unchanged. It seems a little inefficient
> to clone array1 every time some_method is called.  Is there a more
> elegant solution?

If you're comfortable with the idea of using completely immutable lists, 
and not changing any of the elements of the list, an implementation of 
efficiently catenable lists (such as that described in "Purely Functional 
Data Structures") might be in order.


-- 
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic