[Prev][Next][Index][Thread]

Re: Removing a specific item from a collection?



On Tue, 2 May 2000, Hugh Greene wrote:
> On Tue, 2 May 2000, Dustin Voss wrote:
> > I was surprised to find that there doesn't seem to be a way to remove, 
> > say, element 5 from a stretchy-vector.  ...
> 
> I don't actually remember ever hitting this (in 2-3 years of Dylan
> programming!) but ... An easy answer is
> --------
> define method remove-by-key!
>     (seq :: <sequence>, key :: <integer>)
>  => (new-seq :: <sequence>)
>   choose-by(curry(\!=, key), range(), seq)
> end;
> --------

Or (duh) an even easier (and probably more efficient) version is

--------
define method remove-by-key!
    (seq :: <sequence>, key :: <integer>)
 => (new-seq :: <sequence>)
  replace-subsequence!(seq, #(). start: key, end: key + 1)
end;
--------

(And I *have* used replace-subsequence! a few times.)

Hugh (dons dunce's hat)




References: