[rcr] Array#join non string arguments

S

Simon Strandgaard

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

Yukihiro Matsumoto

Hi,

In message "Re: [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 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.
 
S

Simon Strandgaard

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

Simon Strandgaard

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

Jannis Harder

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
 
D

David A. Black

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
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: [rcr] Array#join non string arguments"

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

Bill Guindon

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.

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

Martin DeMello

David A. Black said:
If you're just using one item, it's more like "interspersing" or
something.

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

martin
 
W

William James

Simon said:
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]
 
M

Mark Hubbart

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

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
 
M

Martin DeMello

Mark Hubbart said:
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?

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

martin
 
A

Alexander Kellett

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top