From: William James
Subject: Re: Assoc to retrieve all associations?
Date: 
Message-ID: <gn4fs201gn4@enews4.newsguy.com>
Francogrex wrote:

> Assoc:
> Args: (ITEM ALIST &KEY (TEST '#'EQL) TEST-NOT (KEY
> '#'IDENTITY)) Returns the first pair in ALIST whose car is
> equal (in the sense of TEST) to
> ITEM.  Returns NIL if no such pair exists...
> 
> Suppose we have this simple example:
> : (setf my-list '((test 1) (paper 2) (pen 2) (paper 3) (paper
> 4) (pen 7)))
> : (assoc 'paper my-list)
> : (PAPER 2)
> 
> OK but is there any way/function, maybe a variation of assoc,
> to get a list of all pairs in my-list with 'paper in them
> (instead of just the 1st ocurrence) without resorting to
> loops? Thanks

list = [[:test, 1], [:paper, 2], [:pen, 2], [:paper, 3],
  [:paper, 4], [:pen, 7]]

list.select{|a,b| a == :paper}
    ==>[[:paper, 2], [:paper, 3], [:paper, 4]]
From: Marco Antoniotti
Subject: Re: Assoc to retrieve all associations?
Date: 
Message-ID: <a8788484-1e14-4183-a9ca-a9b0745724ce@i38g2000yqd.googlegroups.com>
On Feb 13, 7:58 pm, "William James" <> wrote:
> Francogrex wrote:
> > Assoc:
> > Args: (ITEM ALIST &KEY (TEST '#'EQL) TEST-NOT (KEY
> > '#'IDENTITY)) Returns the first pair in ALIST whose car is
> > equal (in the sense of TEST) to
> > ITEM.  Returns NIL if no such pair exists...
>
> > Suppose we have this simple example:
> > : (setf my-list '((test 1) (paper 2) (pen 2) (paper 3) (paper
> > 4) (pen 7)))
> > : (assoc 'paper my-list)
> > : (PAPER 2)
>
> > OK but is there any way/function, maybe a variation of assoc,
> > to get a list of all pairs in my-list with 'paper in them
> > (instead of just the 1st ocurrence) without resorting to
> > loops? Thanks
>
> list = [[:test, 1], [:paper, 2], [:pen, 2], [:paper, 3],
>   [:paper, 4], [:pen, 7]]
>
> list.select{|a,b| a == :paper}
>     ==>[[:paper, 2], [:paper, 3], [:paper, 4]]

Now, now...  Don't waste time on these trivialities.  You still have
to produce non-copying array slices and lazy calling in Ruby.

We are still waiting.....

Cheers
--
Marco