Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Array resizing

Reply
Thread Tools

Array resizing

 
 
Victor Shepelev
Guest
Posts: n/a
 
      03-28-2006
Maybe dumb question, but...

I have not found anything like Array.resize, i.e.

a = [1,2]

a.resize(5) #=> [1,2,nil,nil,nil]
a.resize(5, 3) #=> [1,2,3,3,3]

I can use Array.fill, but

a.fill(3, a.size, a.size+2)

looks a bit ugly, not?

Moreover, if I want to resize to size lesser then array is, I need slice.

Why not to have all-purpose resize?

Victor.



 
Reply With Quote
 
 
 
 
Pierre Barbier de Reuille
Guest
Posts: n/a
 
      03-28-2006
Victor Shepelev a =E9crit :
> Maybe dumb question, but...
>
> I have not found anything like Array.resize, i.e.
>
> a =3D [1,2]
>
> a.resize(5) #=3D> [1,2,nil,nil,nil] a.resize(5, 3) #=3D> [1,2,3,3=

,3]
>
>
> I can use Array.fill, but
>
> a.fill(3, a.size, a.size+2)
>
> looks a bit ugly, not?
>
> Moreover, if I want to resize to size lesser then array is, I need
> slice.
>
> Why not to have all-purpose resize?
>
> Victor.
>

You may try :

a =3D [1,2]
a.fill(3, -1, 3) #=3D> [1,2,3,3,3]

So you may code yourself your resize :

class Array
def resize(new_size, value=3Dnil)
if new_size < length
slice!(new_size, length)
else
fill(value, -1, new_size-length)
end
self
end
end

Pierre




 
Reply With Quote
 
 
 
 
Marcin Mielżyński
Guest
Posts: n/a
 
      03-28-2006
Victor Shepelev wrote:
> Maybe dumb question, but...
>
> I have not found anything like Array.resize, i.e.
>
> a = [1,2]
>
> a.resize(5) #=> [1,2,nil,nil,nil]
> a.resize(5, 3) #=> [1,2,3,3,3]
>
> I can use Array.fill, but
>
> a.fill(3, a.size, a.size+2)
>
> looks a bit ugly, not?
>


a = [1,2,3]
a += [nil] * 3

> Moreover, if I want to resize to size lesser then array is, I need slice.
>


a = [1,2,3,4,5]
a[0..-3]

Ruby 1.9 supports Array#pop taking an integer

lopex
 
Reply With Quote
 
Marcin Mielżyński
Guest
Posts: n/a
 
      03-28-2006
Pierre Barbier de Reuille wrote:

>
> a = [1,2]
> a.fill(3, -1, 3) #=> [1,2,3,3,3]
>


nope:

a = [1,2]
a.fill(3, -1, 3) #=> [1,3,3,3]


since -1 points to the last value in an array


lopex
 
Reply With Quote
 
Victor Shepelev
Guest
Posts: n/a
 
      03-28-2006
Pierre wrote

> So you may code yourself your resize :
>
> class Array
> def resize(new_size, value=nil)
> if new_size < length
> slice!(new_size, length)
> else
> fill(value, -1, new_size-length)
> end
> self
> end
> end


Yes, I can (in fact, I already did).

The question is more ideological than technical. Why not to have
Array#resize in standard library? It seems not very rational.

> Pierre


Victor.



 
Reply With Quote
 
dblack@wobblini.net
Guest
Posts: n/a
 
      03-28-2006
Hi --

On Tue, 28 Mar 2006, Victor Shepelev wrote:

> Pierre wrote
>
>> So you may code yourself your resize :
>>
>> class Array
>> def resize(new_size, value=nil)
>> if new_size < length
>> slice!(new_size, length)
>> else
>> fill(value, -1, new_size-length)
>> end
>> self
>> end
>> end

>
> Yes, I can (in fact, I already did).
>
> The question is more ideological than technical. Why not to have
> Array#resize in standard library? It seems not very rational.


I think it might be because the size of an array isn't important if
the array just contains nils, because uninitialized values default to
nil anyway. And if you're adding non-nil elements to the array, then
the fact that the size changes is not really the main point; it's just
a side-effect of the operation, so referring to it as a "resize"
operation doesn't fit very well.


David

--
David A. Black ()
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black


 
Reply With Quote
 
Victor Shepelev
Guest
Posts: n/a
 
      03-28-2006
David wrote:

> > The question is more ideological than technical. Why not to have
> > Array#resize in standard library? It seems not very rational.

>
> I think it might be because the size of an array isn't important if
> the array just contains nils, because uninitialized values default to
> nil anyway. And if you're adding non-nil elements to the array, then
> the fact that the size changes is not really the main point; it's just
> a side-effect of the operation, so referring to it as a "resize"
> operation doesn't fit very well.


Sounds reasonable.
I can tell when I stumbled upon need for __resizing__ aray: when tried to
use Array#transpose - it can't process
[
[1,2,3],
[1],
[1,2,3,4]
]

I think, it isn't sole case, when we can want having array of arbitrary
size.

> David


Victor.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Resizing a div by resizing its borders Pil (Trustworthy from Experience) Javascript 9 04-21-2009 07:35 AM
Resizing a div by resizing its borders Proper Javascript 0 04-18-2009 08:02 PM
confused about resizing array in Python Ruan Python 10 02-05-2007 11:46 PM
resizing an array, is there a better way? someone else C Programming 29 08-09-2004 07:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57