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]]
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