What’s the equivalent in the (vastly superior) language Ruby? How does one
unflatten?
How about ???
ruby flat.rb
[[1, 1, 1], [2, 2], [3, 3, 3, 3]]
cat flat.rb
class Array
def shape(*n)
data = self
res =
n.each do |count|
res << data.slice!(0, count)
end
res
end
end
p [1, 1, 1, 2, 2, 3, 3, 3, 3].shape(3, 2, 4)
But… …now I need help copying it from a KNode post running inside a
CygWin XFree86 fvwm2 X server, into WinXP’s clipboard so I can then paste
it into Scite. No lie.
What’s the equivalent in the (vastly superior) language Ruby? How does one
unflatten?
How about ???
ruby flat.rb
[[1, 1, 1], [2, 2], [3, 3, 3, 3]]
cat flat.rb
class Array
def shape(*n)
data = self
res =
n.each do |count|
res << data.slice!(0, count)
end
res
end
end
p [1, 1, 1, 2, 2, 3, 3, 3, 3].shape(3, 2, 4)
That modifies the original array, though, which probably isn’t what’s
wanted.
Less impact-ful way:
class Array
def shape(*n)
i = 0
n.map {|e| s=i; i+=e; slice(s,e)}
end
end