[rcr] Array#join non string arguments

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

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.

Hi --

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.

David

···

On Sun, 20 Feb 2005, Simon Strandgaard wrote:

--
David A. Black
dblack@wobblini.net

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]

I have posted rcr #294 on rcrchive.

http://rcrchive.net/rcr/show/294

please vote for/against it.

···

--
Simon Strandgaard

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]

···

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".

--
Simon Strandgaard

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.

···

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.

--
Bill Guindon (aka aGorilla)

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

martin

···

David A. Black <dblack@wobblini.net> wrote:

If you're just using one item, it's more like "interspersing" or
something.

[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]

···

On Sat, 19 Feb 2005 17:59:55 +0100, Simon Strandgaard <neoneye@gmail.com> wrote:

--
Simon Strandgaard

It may be just me, but in a programming context I see 'interpolate' as
referring to the interpolation behavior of string literals. I assume
that your name choice is reflecting it's use in graphics?

The actual meaning of interpolate is much more vague; it just means to
insert or interject. I thought interspersing was pretty good, as it
implies (to me) multiple copies at regular intervals.

cheers,
Mark

···

On Sun, 20 Feb 2005 07:09:43 +0900, Martin DeMello <martindemello@yahoo.com> wrote:

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.

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/interpolate.rb

···

--
Jannis Harder

Oops - no, I was thinking of mathematics rather than programming. I'd
forgotten about string interpolation.

martin

···

Mark Hubbart <discordantus@gmail.com> wrote:

It may be just me, but in a programming context I see 'interpolate' as
referring to the interpolation behavior of string literals. I assume
that your name choice is reflecting it's use in graphics?

if i read array_of_numbers.interpolate
i'll think the mathematical version
therefore as it currently stands i'm
opposed to rcr. however it was 'intersperse'
i'd be more in favour. personally though, i
don't see that is to useful as to make its
way into the stdlib... maybe facets?

Alex

···

On Feb 20, 2005, at 12:08 AM, Mark Hubbart wrote:

The actual meaning of interpolate is much more vague; it just means to
insert or interject. I thought interspersing was pretty good, as it
implies (to me) multiple copies at regular intervals.

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.

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

I fixed that.

···

--
Jannis Harder