[#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:6584] Re: best way to interleaf arrays?

From: David Alan Black <dblack@...>
Date: 2000-11-27 00:09:35 UTC
List: ruby-talk #6584
Hello --

On Mon, 27 Nov 2000, Brian F. Feldman wrote:

> David Alan Black <dblack@candle.superlink.net> wrote:
> > Hello --
> > 
> > Is there a nice Ruby idiom for interleafing arrays?  It's not hard to
> > write a method that will do it, but I'd be interested in knowing how
> > it's been done in the past.
> 
> What do you mean by interleave?  Normally I'd take this to mean that you'd 
> want the even number of elements from the first array and the odd numbered 
> elements to come from the second array, but it seems you're implying you 
> just want the logical union of the two.  e.g.

Hmmmm.... I'm not sure what I said that implied that.  Maybe throwing
in the hash thing was confusing.  Anyway, except for my misspelling
the word (an old mental glitch), my reference was indeed to
interleaving.

My current best shot at an interleave method is:

class Array

  def interleave(ary2)
    a1, a2, res = self.dup, ary2.dup, []
    res.push a1.shift, a2.shift until a1.empty? or a2.empty?
    for n in [a1, a2]
      res.concat n unless n.empty?
    end
    res
  end

end

which is about as fast as I can get it (non-destructively :-)  

On the hash matter:

> You can always do it as:
> 
> h = Hash.new(false)
> (array1 + array2).each {|x| h[x] = true}

Yes, I think that's where the union confusion was.  My hash topic was
actually separate.  I was working on a whole cluster of problems, the
connections among which were probably less clear than I'd thought.
The problem in the middle, so to speak, was populating a hash from two
arrays, in this sense:

  h = Hash[*a1.interleave(a2)]

which set me on the road to interleaving, and also got me wondering
about what else could be done between those square brackets (e.g.,
setting values to true for an array of keys).


David

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


In This Thread