[#6363] Re: rescue clause affecting IO loop behavior — ts <decoux@...>

>>>>> "D" == David Alan Black <dblack@candle.superlink.net> writes:

17 messages 2000/11/14
[#6367] Re: rescue clause affecting IO loop behavior — David Alan Black <dblack@...> 2000/11/14

Hello again --

[#6582] best way to interleaf arrays? — David Alan Black <dblack@...>

Hello --

15 messages 2000/11/26

[#6646] RE: Array Intersect (&) question — Aleksi Niemel<aleksi.niemela@...>

Ross asked something about widely known and largely ignored language (on

23 messages 2000/11/29
[#6652] RE: Array Intersect (&) question — rpmohn@... (Ross Mohn) 2000/11/29

aleksi.niemela@cinnober.com (Aleksi Niemel) wrote in

[#6723] Re: Array Intersect (&) question — Mathieu Bouchard <matju@...> 2000/12/01

> >Use a hash. Here's code to do both and more. It assumes that

[#6656] printing/accessing arrays and hashes — raja@... (Raja S.)

I'm coming to Ruby with a Python & Common Lisp background.

24 messages 2000/11/30

[ruby-talk:6588] Re: best way to interleaf arrays?

From: David Alan Black <dblack@...>
Date: 2000-11-27 04:09:05 UTC
List: ruby-talk #6588
On Mon, 27 Nov 2000, Brian F. Feldman wrote:

> The final, fastest version I know of is:
> 
> class Array
> 
>   def interleave(ary2)
>     a1, a2, res = self.reverse, ary2.reverse, []
>     res.push a1.pop, a2.pop until a1.empty? or a2.empty?
>     for n in [a1, a2]
>       res.concat n unless n.empty?
>     end
>     res
>   end
> 
> end

Except... you haven't (un)reversed what's left of a1 and a2.  So, for
example:

a1 = [1, 2, 3, 4]
a2 = %w{ one two }

p a1.interleave(a2) # => [1, "one", 2, "two", 4, 3]

So:

   res.concat n.reverse unless n.empty?

though dropping the whole 'for' construct and replacing the final
'res' with this:

   res + (a1 + a2).reverse

(snagging the '+' instead of concat approach from Dave's iterator
version) runs a little faster.  To save some space (I assume, since +
creates a new array):

   res.concat (a1 + a2).reverse

And other permutations, yet unborn.... :-)


David

-- 
David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web:  http://pirate.shu.edu/~blackdav



In This Thread