Home > Archive > Ruby > August 2005 > Re: 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 |
Re: how to unflatten a flat-array
|
|
| William James 2005-08-29, 3:57 am |
|
SHIGETOMI, Takuhiko wrote:
> dear guys,
>
> i am s ing 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.
ary = [1,2,3,4,5,6,7,8]
f=nil
t=ary.partition{f=!f}
p t[0].zip(t[1])
| |
| SHIGETOMI, Takuhiko 2005-08-29, 3:57 am |
| greetings, William. i'm sorry, your way is beyond my light-weight brain.
> ary = [1,2,3,4,5,6,7,8]
> f=nil
> t=ary.partition{f=!f}
> p t[0].zip(t[1])
but it works...
$ irb
irb(main):001:0> ary = [1,2,3,4,5,6,7,8]
=> [1, 2, 3, 4, 5, 6, 7, 8]
irb(main):002:0> f=nil
=> nil
irb(main):003:0> t=ary.partition{f=!f}
=> [[1, 3, 5, 7], [2, 4, 6, 8]]
irb(main):004:0> p t[0].zip(t[1])
[[1, 2], [3, 4], [5, 6], [7, 8]]
=> nil
i'd like you use a terrestrial language, or beam me your universal
translator. :-)
--
<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>
| |
| Simon Kröger 2005-08-31, 7:57 am |
| SHIGETOMI, Takuhiko wrote:
> greetings, William. i'm sorry, your way is beyond my light-weight brain.
>
>
>
>
> but it works...
>
> $ irb
> irb(main):001:0> ary = [1,2,3,4,5,6,7,8]
> => [1, 2, 3, 4, 5, 6, 7, 8]
> irb(main):002:0> f=nil
> => nil
> irb(main):003:0> t=ary.partition{f=!f}
> => [[1, 3, 5, 7], [2, 4, 6, 8]]
> irb(main):004:0> p t[0].zip(t[1])
> [[1, 2], [3, 4], [5, 6], [7, 8]]
> => nil
>
> i'd like you use a terrestrial language, or beam me your universal
> translator. :-)
>
Muhahaha...
p ary.zip((0...ary.size).to_a).partition{|o|(o[1]%2).zero?}.
map{|a|a.transpose[0]}
erm, sorry, could not resist :)
But transpose make the example above shorter (and perhaps even
more readable):
f=nil
p ary.partition{f=!f}.transpose
cheers
Simon
|
|
|
|
|