[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[rcr] Array#join non string arguments

Simon Strandgaard

2/19/2005 4:43:00 PM

A proposal:

[1, 2, 3, 4, 5, 6].join(0) #-> [1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6]


I would like to interleave an arrray with another element.
This is similar to what the current Array#join does, except that
it converts the elements to strings.


Maybe find a better name, for instance #interleave.

--
Simon Strandgaard


14 Answers

Yukihiro Matsumoto

2/19/2005 4:52:00 PM

0

Hi,

In message "Re: [rcr] Array#join non string arguments"
on Sun, 20 Feb 2005 01:42:34 +0900, Simon Strandgaard <neoneye@gmail.com> writes:

|A proposal:
|
|[1, 2, 3, 4, 5, 6].join(0) #-> [1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6]

I think behavior that changes depends on indirect types (i.e. type of
elements in the receiver this case) is not good idea. A method gives
a string should always gives string (or string-like object).
This particular behavior (interleaving array elements with given
value) might be useful, but should not be implemented by "Array#join".

matz.


Simon Strandgaard

2/19/2005 5:00:00 PM

0

On Sun, 20 Feb 2005 01:52:25 +0900, Yukihiro Matsumoto
<matz@ruby-lang.org> wrote:
> on Sun, 20 Feb 2005 01:42:34 +0900, Simon Strandgaard <neoneye@gmail.com> writes:
>
> |A proposal:
> |
> |[1, 2, 3, 4, 5, 6].join(0) #-> [1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6]
>
> I think behavior that changes depends on indirect types (i.e. type of
> elements in the receiver this case) is not good idea. A method gives
> a string should always gives string (or string-like object).
> This particular behavior (interleaving array elements with given
> value) might be useful, but should not be implemented by "Array#join".

good point. I just needed this behavior and the first thing that came to
mind was #join.

Maybe better to name it #interleave, like this

[1, 2, 3, 4, 5, 6].interleave(0) #-> [1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6]
[1, 2, 3, 4, 5, 6].interleave('hi') #-> [1, 'hi', 2, 'hi', 3, 'hi',
4, 'hi', 5, 'hi', 6]


--
Simon Strandgaard


Simon Strandgaard

2/19/2005 5:10:00 PM

0

On Sat, 19 Feb 2005 17:59:55 +0100, Simon Strandgaard <neoneye@gmail.com> wrote:
[snip]
> Maybe better to name it #interleave, like this
>
> [1, 2, 3, 4, 5, 6].interleave(0) #-> [1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6]
> [1, 2, 3, 4, 5, 6].interleave('hi') #-> [1, 'hi', 2, 'hi', 3, 'hi', 4, 'hi', 5, 'hi', 6]


Jannis Harder suggested to let #interleave take a block, like this:

[8,6,4,2,0].interleave{|a,b|(a+b)/2} #-> [8,7,6,5,4,3,2,1,0]


--
Simon Strandgaard


Jannis Harder

2/19/2005 6:04:00 PM

0

I wrote a pure Ruby implementation of interpolate (was: interleave)
(Simon Strandgaard and I decided interpolate fits better)

1 argument, without block:
[1, 2, 3, 4].interpolate("a") #=> [1, "a", 2, "a", 3, "a", 4]

2+ arguments, without block:
[1, 2, 3, 4, 5].interpolate("a","b","c") #=> [1, "a", 2, "b", 3, "c", 4,
"a", 5]

no arguments, with block:
[0, 10, 100, 1000].interpolate{|a,b,index|"#{(a+b)/2} #{index}"}
#=> [0, "5 0", 10, "55 1", 100, "550 2", 1000]

with arguments and block:
[0, 10, 100, 1000,
10000].interpolate(+1,-1){|sign,a,b,index|"#{(a+b)/2*sign} #{index}"}
#=> [0, "5 0", 10, "-55 1", 100, "550 2", 1000, "-5500 3", 10000]

http://www.harderweb.de/jannis/ruby/inte...

--
Jannis Harder



dblack

2/19/2005 6:05:00 PM

0

Yukihiro Matsumoto

2/19/2005 6:14:00 PM

0

Hi,

In message "Re: [rcr] Array#join non string arguments"
on Sun, 20 Feb 2005 03:04:05 +0900, Jannis Harder <jannis@harderweb.de> writes:

|with arguments and block:
|[0, 10, 100, 1000,
|10000].interpolate(+1,-1){|sign,a,b,index|"#{(a+b)/2*sign} #{index}"}
|#=> [0, "5 0", 10, "-55 1", 100, "550 2", 1000, "-5500 3", 10000]

Shouldn't "sign" be the last parameter? Just because fragile
parameter place is a bad idea in general.

matz.


Bill Guindon

2/19/2005 6:30:00 PM

0

On Sun, 20 Feb 2005 03:04:41 +0900, David A. Black <dblack@wobblini.net> wrote:
> Hi --
>
> On Sun, 20 Feb 2005, Simon Strandgaard wrote:
>
> > A proposal:
> >
> > [1, 2, 3, 4, 5, 6].join(0) #-> [1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6]
> >
> >
> > I would like to interleave an arrray with another element.
> > This is similar to what the current Array#join does, except that
> > it converts the elements to strings.
> >
> >
> > Maybe find a better name, for instance #interleave.
>
> Interleaving, to me, suggests this:
>
> a = [1,2,3]
> b = [4,5,6]
> a.interleave(b) # => [1,4,2,5,3,6]
>
> If you're just using one item, it's more like "interspersing" or
> something.

Dilimiter? Separator?

Not that I'd mind having it, and your version of "interleave" as well.
Does leave me wondering if there would be a need for functions that
reverse those actions.

--
Bill Guindon (aka aGorilla)


Jannis Harder

2/19/2005 7:52:00 PM

0


> Shouldn't "sign" be the last parameter? Just because fragile
> parameter place is a bad idea in general.

I fixed that.

--
Jannis Harder


Martin DeMello

2/19/2005 10:05:00 PM

0

David A. Black <dblack@wobblini.net> wrote:
>
> If you're just using one item, it's more like "interspersing" or
> something.

"Interpolate", I was thinking, particularly with the block version.

martin

William James

2/19/2005 10:39:00 PM

0


Simon Strandgaard wrote:
> A proposal:
>
> [1, 2, 3, 4, 5, 6].join(0) #-> [1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6]
>
>
> I would like to interleave an arrray with another element.

[3,6,9].map{|x|[x,0]}.flatten[0..-2]
[3, 0, 6, 0, 9]