For Programmers: Free Programming Magazines  


Home > Archive > Ruby > August 2005 > [Q] how to unflatten a flat-array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author [Q] how to unflatten a flat-array
SHIGETOMI, Takuhiko

2005-08-28, 9:57 pm

dear guys,

i am sing a smart way to do ...

[ 1, 2, 3, 4, 5, 6, ... ] => [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], ... ]

especialy, the way hopefully will work safety when the source array's
size is odd or zero.

--
<name species-designation="5618" default-lang="ja_JP">SHIGETOMI,Takuhiko</name>
<contact medium="email">tshiget1@gw.nsw.co.jp</contact>
<location federational-alias="/galaxy/alpha-quadrant/sector-001/earth/">
/void/3d/universe/milkyway-galaxy/orion's-arm/sol-solar-system/3rd-planet/fareast/jp/tky/
</location>
<hail>resistance is futile.</hail>



Hal Fulton

2005-08-28, 9:57 pm

SHIGETOMI, Takuhiko wrote:
> dear guys,
>
> i am sing a smart way to do ...
>
> [ 1, 2, 3, 4, 5, 6, ... ] => [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], ... ]
>
> especialy, the way hopefully will work safety when the source array's
> size is odd or zero.
>


I have used something like:

arr = array.dup
result = []
while ! arr.empty?
result << arr.slice!(0..1)
end

Would that work for you?


Hal




SHIGETOMI, Takuhiko

2005-08-28, 9:57 pm

greetings, Hal. gratitude to you.

> arr = array.dup
> result = []
> while ! arr.empty?
> result << arr.slice!(0..1)
> end


looks like nice to me.
please let me exec it and repot the result to you later.
(sorry, i am now screwed up in my business.)

--
<name species-designation="5618" default-lang="ja_JP">SHIGETOMI,Takuhiko</name>
<contact medium="email">tshiget1@gw.nsw.co.jp</contact>
<location federational-alias="/galaxy/alpha-quadrant/sector-001/earth/">
/void/3d/universe/milkyway-galaxy/orion's-arm/sol-solar-system/3rd-planet/fareast/jp/tky/
</location>
<hail>resistance is futile.</hail>



David A. Black

2005-08-28, 9:57 pm

Hi --

On Mon, 29 Aug 2005, SHIGETOMI, Takuhiko wrote:

> dear guys,
>
> i am sing a smart way to do ...
>
> [ 1, 2, 3, 4, 5, 6, ... ] => [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], ... ]
>
> especialy, the way hopefully will work safety when the source array's
> size is odd or zero.


I've got a method from long ago that might fit:

class Array # or do it as a module, or whatever
def in_slices_of(n)
res = []
0.step(size - 1, n) do |i|
s = slice(i...i + n)
yield s if block_given?
res.push s
end
res
end
end

[1,2,3,4,5,6].in_slices_of(2) # => [[1, 2], [3, 4], [5, 6]]
[1,2,3,4,5,6].in_slices_of(3) # => [[1, 2, 3], [4, 5, 6]]

It handles odd numbers by putting whatever's left over in its own
array:

[1,2,3,4,5,6,7].in_slices_of(2) # => [[1, 2], [3, 4], [5, 6], [7]]

(and note that it yields each slice too).

In 1.9 you can do:

irb(main):015:0> [1,2,3,4,5,6].enum_slice(2).map {|x| x }
=> [[1, 2], [3, 4], [5, 6]]

which handles odd numbers the same way. (I can't seem to get it to
return the desired result without doing the map operation.)


David

--
David A. Black
dblack@wobblini.net


SHIGETOMI, Takuhiko

2005-08-28, 9:57 pm

greetings, David. how nice it is!

> I've got a method from long ago that might fit:
>
> class Array # or do it as a module, or whatever
> def in_slices_of(n)
> res = []
> 0.step(size - 1, n) do |i|
> s = slice(i...i + n)
> yield s if block_given?
> res.push s
> end
> res
> end
> end
>
> [1,2,3,4,5,6].in_slices_of(2) # => [[1, 2], [3, 4], [5, 6]]
> [1,2,3,4,5,6].in_slices_of(3) # => [[1, 2, 3], [4, 5, 6]]
>
> It handles odd numbers by putting whatever's left over in its own
> array:
>
> [1,2,3,4,5,6,7].in_slices_of(2) # => [[1, 2], [3, 4], [5, 6], [7]]


quick question. (sorry, i am still screwed up.)
will the yieldee ( = the block ) 's 2nd parameter be nil at the end of
the case of odd number as above ?

--
<name species-designation="5618" default-lang="ja_JP">SHIGETOMI,Takuhiko</name>
<contact medium="email">tshiget1@gw.nsw.co.jp</contact>
<location federational-alias="/galaxy/alpha-quadrant/sector-001/earth/">
/void/3d/universe/milkyway-galaxy/orion's-arm/sol-solar-system/3rd-planet/fareast/jp/tky/
</location>
<hail>resistance is futile.</hail>



David A. Black

2005-08-28, 9:57 pm

Hi --

On Mon, 29 Aug 2005, SHIGETOMI, Takuhiko wrote:

> greetings, David. how nice it is!
>
>
> quick question. (sorry, i am still screwed up.)
> will the yieldee ( = the block ) 's 2nd parameter be nil at the end of
> the case of odd number as above ?


Yes:

[1,2,3,4,5,6,7].in_slices_of(2).map {|x,y| [x,y]}
=> [[1, 2], [3, 4], [5, 6], [7, nil]]

I don't think there's much choice. (You could of course just catch
each inner array in one variable.)


David

--
David A. Black
dblack@wobblini.net


James Edward Gray II

2005-08-29, 3:57 am

On Aug 28, 2005, at 8:50 PM, SHIGETOMI, Takuhiko wrote:

> dear guys,
>
> i am sing a smart way to do ...
>
> [ 1, 2, 3, 4, 5, 6, ... ] => [ [ 1, 2 ], [ 3, 4 ], [ 5,
> 6 ], ... ]
>
> especialy, the way hopefully will work safety when the source array's
> size is odd or zero.


Pretty hackish but:

irb(main):001:0> arr = [ 1, 2, 3, 4, 5, 6]
=> [1, 2, 3, 4, 5, 6]
irb(main):011:0> arr << nil unless arr.size % 2 == 0
=> nil
irb(main):012:0> Hash[*arr].to_a.sort
=> [[1, 2], [3, 4], [5, 6]]

I was just playing around. I like David's in_slices() method much
better.

James Edward Gray II



SHIGETOMI, Takuhiko

2005-08-29, 3:57 am

greeting James. what an intriguing way!

irb(main):001:0> arr = [ 1, 2, 3, 4, 5, 6]
=> [1, 2, 3, 4, 5, 6]
irb(main):011:0> arr << nil unless arr.size % 2 == 0
=> nil
irb(main):012:0> Hash[*arr].to_a.sort
=> [[1, 2], [3, 4], [5, 6]]

for me, as a beginner of ruby,
this is a fascinating black magic.
i will study hard to let it be white. :-)

--
<name species-designation="5618" default-lang="ja_JP">SHIGETOMI,Takuhiko</name>
<contact medium="email">tshiget1@gw.nsw.co.jp</contact>
<location federational-alias="/galaxy/alpha-quadrant/sector-001/earth/">
/void/3d/universe/milkyway-galaxy/orion's-arm/sol-solar-system/3rd-planet/fareast/jp/tky/
</location>
<hail>resistance is futile.</hail>



Robert Klemme

2005-08-29, 3:57 am

2005/8/29, SHIGETOMI, Takuhiko <tshiget1@gw.nsw.co.jp>:
> dear guys,
>=20
> i am sing a smart way to do ...
>=20
> [ 1, 2, 3, 4, 5, 6, ... ] =3D> [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], ... ]
>=20
> especialy, the way hopefully will work safety when the source array's
> size is odd or zero.


This is a pretty much straightforward implementation:
[color=darkred]
=3D> [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]

Kind regards

robert


SHIGETOMI, Takuhiko

2005-08-29, 7:57 am

greetings, Robert. thank you, it does make sense to me.

> => [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]


i know that an yieldee is 100% passive to its yielder, but i often feel
desire to let it fetch to the next/previous yielded object from inside
the yieldee.

don't you know any way to do this in ruby?

--
<name species-designation="5618" default-lang="ja_JP">SHIGETOMI,Takuhiko</name>
<contact medium="email">tshiget1@gw.nsw.co.jp</contact>
<location federational-alias="/galaxy/alpha-quadrant/sector-001/earth/">
/void/3d/universe/milkyway-galaxy/orion's-arm/sol-solar-system/3rd-planet/fareast/jp/tky/
</location>
<hail>resistance is futile.</hail>



Robert Klemme

2005-08-29, 7:57 am

2005/8/29, SHIGETOMI, Takuhiko <tshiget1@gw.nsw.co.jp>:
> greetings, Robert. thank you, it does make sense to me.
>=20
>=20
> i know that an yieldee is 100% passive to its yielder, but i often feel
> desire to let it fetch to the next/previous yielded object from inside
> the yieldee.
>=20
> don't you know any way to do this in ruby?


No, not generally. With arrays you can use #each_with_index and in
the general case of Enumerable you can use inject to access the last
element, along the lines

enum.inject {|last, current| .... ; current}

Kind regards

robert


Daniel Brockman

2005-08-29, 7:57 am

"SHIGETOMI, Takuhiko" <tshiget1@gw.nsw.co.jp> writes:

> i know that an yieldee is 100% passive to its yielder, but
> i often feel desire to let it fetch to the next/previous
> yielded object from inside the yieldee.
>
> don't you know any way to do this in ruby?


You can't easily go backwards, but skipping to the next is
as easy as saying `next'.

--
Daniel Brockman <daniel@brockman.se>



Linus Sellberg

2005-08-29, 7:57 am

On 8/29/05, SHIGETOMI, Takuhiko <tshiget1@gw.nsw.co.jp> wrote:
> [ 1, 2, 3, 4, 5, 6, ... ] =3D> [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], ... ]


TIMTOWTDI:

def slicer arr, n
arr =3D arr.dup
arr.each_index {|i| arr[i...(i + n)] =3D [arr[i...(i + n)]] }
end

x =3D [1,2,3,4,5,6,7]

slicer x,3
=3D> [[1, 2, 3], [4, 5, 6], [7]]


SHIGETOMI, Takuhiko

2005-08-29, 7:57 am

greetings, Daniel. true. 'next' works.

> You can't easily go backwards, but skipping to the next is
> as easy as saying `next'.


on the other hand, and as your reference 'cannot easyily go backwards',
'next' forgets current object unless any variable on outside of the
yieldee block.

how about writing a special yielder like as below ..

any-enumerable-object.sliding_each do |prev,this,next|
...
end

- could be really useful?
- any other good name?

/void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618



David A. Black

2005-08-29, 7:57 am

Hi --

On Mon, 29 Aug 2005, SHIGETOMI, Takuhiko wrote:

> greetings, Daniel. true. 'next' works.
>
>
> on the other hand, and as your reference 'cannot easyily go backwards',
> 'next' forgets current object unless any variable on outside of the
> yieldee block.
>
> how about writing a special yielder like as below ..
>
> any-enumerable-object.sliding_each do |prev,this,next|
> ...
> end
>
> - could be really useful?
> - any other good name?


This might help you on this:

irb(main):003:0> require 'enumerator'
=> true
irb(main):004:0> [1,2,3,4,5,6].each_cons(2) {|x| p x }
[1, 2]
[2, 3]
[3, 4]
[4, 5]
[5, 6]
=> nil
irb(main):005:0> [1,2,3,4,5,6].each_cons(3) {|x| p x }
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, 6]


David

--
David A. Black
dblack@wobblini.net


Randy Kramer

2005-08-29, 7:57 am

On Monday 29 August 2005 07:05 am, SHIGETOMI, Takuhiko wrote:
> /void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/3rd-planet/fa
>reast/jp/tky/shigetomi.takuhiko.5618


I appreciate you including your fully-qualified URL, but it would be more
convenient (for cut and paste) if you included the protocol as well (http://
or whatever). ;-)

Randy Kramer

(I hope that's perceived as an attempt at humor.)


SHIGETOMI, Takuhiko

2005-08-29, 7:57 am

thank you, David. it helps me a lot.

> irb(main):003:0> require 'enumerator'
> => true
> irb(main):004:0> [1,2,3,4,5,6].each_cons(2) {|x| p x }
> [1, 2]
> [2, 3]
> [3, 4]
> [4, 5]
> [5, 6]
> => nil
> irb(main):005:0> [1,2,3,4,5,6].each_cons(3) {|x| p x }
> [1, 2, 3]
> [2, 3, 4]
> [3, 4, 5]
> [4, 5, 6]


this is just one of what i had been sing in vain for !!

oh .. 'enumerator' .. i should open my eyes wider.
and i found again that it is very valuable to ask before inventing.

/void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618



Robert Klemme

2005-08-29, 7:57 am

SHIGETOMI, Takuhiko wrote:
> greetings, Daniel. true. 'next' works.
>
>
> on the other hand, and as your reference 'cannot easyily go
> backwards', 'next' forgets current object unless any variable on
> outside of the yieldee block.
>
> how about writing a special yielder like as below ..
>
> any-enumerable-object.sliding_each do |prev,this,next|
> ...
> end
>
> - could be really useful?
> - any other good name?


Like this?

module Enumerable
def each_window(size)
wind = []
each do |x|
wind << x
wind.shift if wind.size > size
yield *wind if wind.size == size
end
self
end
end

?> %w{aa bb cc dd ee ff gg hh ii jj kk}.each_window(3) {|*a| p a}
["aa", "bb", "cc"]
["bb", "cc", "dd"]
["cc", "dd", "ee"]
["dd", "ee", "ff"]
["ee", "ff", "gg"]
["ff", "gg", "hh"]
["gg", "hh", "ii"]
["hh", "ii", "jj"]
["ii", "jj", "kk"]
=> ["aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "kk"]
[color=darkred]
print pre, ">", here, "<", post, "\n"}
aa>bb<cc
bb>cc<dd
cc>dd<ee
dd>ee<ff
ee>ff<gg
ff>gg<hh
gg>hh<ii
hh>ii<jj
ii>jj<kk
=> ["aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "kk"]

Kind regards

robert

Robert Klemme

2005-08-29, 7:57 am

Uh, forget that. Enumerator is already there. +1 for having to look
closer at it.

robert


SHIGETOMI, Takuhiko

2005-08-29, 7:57 am

greetings, Randy. i much appreciate you for pointing it out.
and sorry to my fully qualified signature, since i am in a quite
complicated world. i guess you too.

is this good to you? qssp stands for quantum slipstream protocol :-)

qssp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618



SHIGETOMI, Takuhiko

2005-08-30, 3:57 am

correction to my reply.

> this is just one of what i had been sing in vain for !!


while i was in my regeneration cycle, i found that this is not the ideal
behavior for me.
[color=darkred]

what i am expecting is as below.

[1,2,3.4.5.6].sliding_each(2) {|x| p x}
[nil,1]
[1,2]
[2,3]
[3,4]
[4,5]
[5,6]
[6,nil]

on the other hand, it is easy by like this.

[ nil, enumeratee, nil ].flatten.each_cons(2) { |x| p x }

q2hdp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618



2005-08-30, 3:57 am

--- Robert Klemme <bob.news@gmx.net> wrote:
> ?> %w{aa bb cc dd ee ff gg hh ii jj
> kk}.each_window(3) {|*a| p a}
> ["aa", "bb", "cc"]
> ["bb", "cc", "dd"]
> ["cc", "dd", "ee"]
> ["dd", "ee", "ff"]
> ["ee", "ff", "gg"]
> ["ff", "gg", "hh"]
> ["gg", "hh", "ii"]
> ["hh", "ii", "jj"]
> ["ii", "jj", "kk"]
> =3D> ["aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh",
> "ii", "jj", "kk"]


Am I the only one who thinks this seems a little
peculiar?

If I were writing something using this sort of=20
functionality, I think I'd need and expect=20
output something like:

[nil, "aa", "bb"]
["aa", "bb", "cc"]
["bb", "cc", "dd"]
["cc", "dd", "ee"]
["dd", "ee", "ff"]
["ee", "ff", "gg"]
["ff", "gg", "hh"]
["gg", "hh", "ii"]
["hh", "ii", "jj"]
["ii", "jj", "kk"]
["jj", "kk", nil]

Admittedly, it'd be hard to that reasonably with a
variable-size window of even length. But for a
previous-current-next arrangement, it seems to make
more sense to say "there is no previous" with a nil
than to basically skip the first item.

-Morgan


=09
________________________________________
____________
Start your day with Yahoo! - make it your home page=20
http://www.yahoo.com/r/hs=20
=20


SHIGETOMI, Takuhiko

2005-08-30, 3:57 am

greetings, Morgan. i concur with your point.

> If I were writing something using this sort of
> functionality, I think I'd need and expect
> output something like:
>
> [nil, "aa", "bb"]
> ["aa", "bb", "cc"]
> ["bb", "cc", "dd"]
> ["cc", "dd", "ee"]
> ["dd", "ee", "ff"]
> ["ee", "ff", "gg"]
> ["ff", "gg", "hh"]
> ["gg", "hh", "ii"]
> ["hh", "ii", "jj"]
> ["ii", "jj", "kk"]
> ["jj", "kk", nil]
>
> Admittedly, it'd be hard to that reasonably with a
> variable-size window of even length. But for a
> previous-current-next arrangement, it seems to make
> more sense to say "there is no previous" with a nil
> than to basically skip the first item.


true.
what i am focusing on is 'current'. 'previous' and/or 'next' is not the
primary interest but often help 'current' to be dealt with
correctly/easily.

bxwp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618



Robert Klemme

2005-08-30, 7:58 am

agemoagemo@yahoo.com wrote:
> --- Robert Klemme <bob.news@gmx.net> wrote:
>
> Am I the only one who thinks this seems a little
> peculiar?
>
> If I were writing something using this sort of
> functionality, I think I'd need and expect
> output something like:
>
> [nil, "aa", "bb"]
> ["aa", "bb", "cc"]
> ["bb", "cc", "dd"]
> ["cc", "dd", "ee"]
> ["dd", "ee", "ff"]
> ["ee", "ff", "gg"]
> ["ff", "gg", "hh"]
> ["gg", "hh", "ii"]
> ["hh", "ii", "jj"]
> ["ii", "jj", "kk"]
> ["jj", "kk", nil]


Well, it depends. If, for example, you would want to make a plot that
used averaged values (i.e. to smooth the curve) you would not want to see
those lines containing nil values IMHO.

> Admittedly, it'd be hard to that reasonably with a
> variable-size window of even length. But for a
> previous-current-next arrangement, it seems to make
> more sense to say "there is no previous" with a nil
> than to basically skip the first item.


It's not that hard to do.

Kind regards

robert



Randy Kramer

2005-08-30, 7:58 am

On Tuesday 30 August 2005 12:48 am, SHIGETOMI, Takuhiko wrote:
> bxwp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
> 3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618


;-)

Ok, so what's bxwp://?

(In any case, the link seems to be down. I wonder what traceroute would tell
me--local probem (sol) or something more major? ;-)

Randy Kramer


SHIGETOMI, Takuhiko

2005-08-30, 7:58 am

sorry, Randy.

> (In any case, the link seems to be down. I wonder what traceroute would tell
> me--local probem (sol) or something more major? ;-)


it's not a technology of ours. and it is not 3-dimensional.

> Ok, so what's bxwp://?


i support several ftl(faster-than-light) protocols...

qssp: quantum slipstream protocol
q2htp: quantum-2 hyper-drive protocol
bxwp: borg trans-warp protocol
cigp: caretaker's inter-galactic protocol
xnfp: xeelee night-fighter protocol
...
some of the others are under implementation :-)

xnfp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618



Randy Kramer

2005-08-30, 7:02 pm

On Tuesday 30 August 2005 08:34 am, SHIGETOMI, Takuhiko wrote:
> i support several ftl(faster-than-light) protocols...
>
> qssp: quantum slipstream protocol
> q2htp: quantum-2 hyper-drive protocol
> bxwp: borg trans-warp protocol
> cigp: caretaker's inter-galactic protocol
> xnfp: xeelee night-fighter protocol


I'm speechless, I had no idea this technology had been implemented
already. ;-)

Randy Kramer


SHIGETOMI, Takuhiko

2005-08-30, 7:02 pm

this is a midnight talk from far east.
maybe i should go back to ruby.
let me explain the background of my original question.
i am going to write a bytesteream/textstream scanner.
i have uptaken many useful ideas and codes from many good-hearted guys
including you, but i am still sing an efficient way for
scanning/traversing a large size of textstream.
while i am thinking whether i should write this part in c-language or
not, i will do write a pilot version in ruby.

> I'm speechless, I had no idea this technology had been implemented
> already. ;-)


'why do you think so 3-dimensional?' the queen said to locutus. :-)

this time, i am on: vaadwaur subspace corridor protocol.
the followings are not implemented so far.

fsbmp: fluidic space bio-mechanical protocol
qcfcp: q-coninuum finger-clicking protocol

vscp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618



Randy Kramer

2005-08-30, 7:02 pm

On Tuesday 30 August 2005 02:55 pm, SHIGETOMI, Takuhiko wrote:
> this is a midnight talk from far east.
> maybe i should go back to ruby.
> let me explain the background of my original question.
> i am going to write a bytesteream/textstream scanner.
> i have uptaken many useful ideas and codes from many good-hearted guys
> including you, but i am still sing an efficient way for
> scanning/traversing a large size of textstream.
> while i am thinking whether i should write this part in c-language or
> not, i will do write a pilot version in ruby.


I've been told that StringScanner is a good approach. Here are some links.

* [[http://www.ruby-doc.org/core/classes/StringScanner.html][Module:
StringScanner]]

* [[http://www1.u-netsurf.ne.jp/~brew/m...scan/usage.html][Usage]]

* [[http://www1.u-netsurf.ne.jp/~brew/m.../reference.html]
[StringScanner Reference Manual]]

* [[http://blade.nagaokaut.ac.jp/cgi-bi...uby-talk/128575]
[I've recently written a JSON parser based on Ruby's StringScanner class]]

BTW, I don't think I can keep up with your faster-than-light protocols, so I'm
going to bow out of that discussion. ;-)

Randy Kramer


2005-08-31, 7:00 pm

--- Robert Klemme <bob.news@gmx.net> wrote:
>=20
> Well, it depends. If, for example, you would want
> to make a plot that
> used averaged values (i.e. to smooth the curve) you
> would not want to see
> those lines containing nil values IMHO.


It seems like it depends on the application then. Most
of the ones I can visualize involve processing each
item in order, with the previous and next values used
for something if they're available, but items still
need to be processed if there isn't a previous or
next.

> make
> nil
>=20
> It's not that hard to do.


Hmmm, maybe I should say "It's hard to decide what the
output should look like.

As in, for someone who wants those nils like I do,
should the first set of arguments from each_window(4)
be something like

[nil, "aa", "bb", "cc"]

or

[nil, nil, "aa", "bb"]

?

It's obvious with odd length windows, the current item
should always be the middle one. And with the kind
that
doesn't use nils, defining one item as the current one
probably isn't meaningful anyway, so it's not an
issue.

-Morgan

________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around=20
http://mail.yahoo.com=20


Robert Klemme

2005-08-31, 7:00 pm

agemoagemo@yahoo.com wrote:
> --- Robert Klemme <bob.news@gmx.net> wrote:
>
> It seems like it depends on the application then.


That's what I meant.

>
> Hmmm, maybe I should say "It's hard to decide what the
> output should look like.
>
> As in, for someone who wants those nils like I do,
> should the first set of arguments from each_window(4)
> be something like
>
> [nil, "aa", "bb", "cc"]
>
> or
>
> [nil, nil, "aa", "bb"]


I'd prefer

[nil, nil, nil, "aa"]

same on the other end: the last window is the one with the last element on
pos 0 and the rest filled with nils.

> It's obvious with odd length windows, the current item
> should always be the middle one. And with the kind
> that
> doesn't use nils, defining one item as the current one
> probably isn't meaningful anyway, so it's not an
> issue.


I think there's a subtle difference: I think of this as a window slid over
the sequence and you focus more on the current with previous and next. IMHO
both are valid but they obviously differ in what they output. Plus, with
your notion (or, what I believe to have recognized as your notion) even
number of elements does not make sense. So we probably have

#each_window(size)
#each_context(elements_on_each_side) # the number indicates how many
elements before and after we see

Is any of this worthwhile to be put into Enumerable?

Kind regards

robert

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com