[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

delete_if, select and (lack of) delete_if!, self.POLS == false

Saku Ytti

4/11/2008 4:27:00 PM

Hey everyone,

I was looking today for version of Array#select that would mutate the array
by deleting selected elements from it.
I first looked at delete_if, then noticed that it actually mutates original
array and returns mutated array. I wonder would it make sense for delete_if
not to mutate and return new array and delete_if! to mutate and return deleted
elements?

Of course what I want is simple method to implement, it just seems pretty
typical desire for Array#select usage cases and also perhaps mutating
delete_if is as well bit unexpected? Problem is, suggested change would
break many current delete_if usage cases.

Thanks,
--
++ytti

23 Answers

ara.t.howard

4/11/2008 4:32:00 PM

0


On Apr 11, 2008, at 10:26 AM, Saku Ytti wrote:
> Hey everyone,
>
> I was looking today for version of Array#select that would mutate
> the array
> by deleting selected elements from it.
> I first looked at delete_if, then noticed that it actually mutates
> original
> array and returns mutated array. I wonder would it make sense for
> delete_if
> not to mutate and return new array and delete_if! to mutate and
> return deleted
> elements?
>
> Of course what I want is simple method to implement, it just seems
> pretty
> typical desire for Array#select usage cases and also perhaps mutating
> delete_if is as well bit unexpected? Problem is, suggested change
> would
> break many current delete_if usage cases.

a non-mutating delete_if is already there:

array.select{|element| not orginal_condition(element)}

ie

odds = [0,1,2,3].select{|i| not i.even?}

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




David A. Black

4/11/2008 4:32:00 PM

0

Hi --

On Sat, 12 Apr 2008, Saku Ytti wrote:

> Hey everyone,
>
> I was looking today for version of Array#select that would mutate the array
> by deleting selected elements from it.
> I first looked at delete_if, then noticed that it actually mutates original
> array and returns mutated array. I wonder would it make sense for delete_if
> not to mutate and return new array and delete_if! to mutate and return deleted
> elements?
>
> Of course what I want is simple method to implement, it just seems pretty
> typical desire for Array#select usage cases and also perhaps mutating
> delete_if is as well bit unexpected? Problem is, suggested change would
> break many current delete_if usage cases.

You should use #reject, which is non-destructive.


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.r... for details and updates!

David A. Black

4/11/2008 4:37:00 PM

0

Hi --

On Sat, 12 Apr 2008, ara.t.howard wrote:

>
> On Apr 11, 2008, at 10:26 AM, Saku Ytti wrote:
>> Hey everyone,
>>
>> I was looking today for version of Array#select that would mutate the array
>> by deleting selected elements from it.
>> I first looked at delete_if, then noticed that it actually mutates original
>> array and returns mutated array. I wonder would it make sense for delete_if
>> not to mutate and return new array and delete_if! to mutate and return
>> deleted
>> elements?
>>
>> Of course what I want is simple method to implement, it just seems pretty
>> typical desire for Array#select usage cases and also perhaps mutating
>> delete_if is as well bit unexpected? Problem is, suggested change would
>> break many current delete_if usage cases.
>
> a non-mutating delete_if is already there:
>
> array.select{|element| not orginal_condition(element)}
>
> ie
>
> odds = [0,1,2,3].select{|i| not i.even?}

Also:

odds = [0,1,2,3].reject {|i| i.even? }


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.r... for details and updates!

Saku Ytti

4/11/2008 4:47:00 PM

0

On (2008-04-12 01:32 +0900), David A. Black wrote:

Thanks everyone for fast reply.

>> I first looked at delete_if, then noticed that it actually mutates original
>> array and returns mutated array. I wonder would it make sense for delete_if
>> not to mutate and return new array and delete_if! to mutate and return deleted
>> elements?
>
> You should use #reject, which is non-destructive.

I guess I wasn't being clear, indeed reject removes the need for
non-mutating delete_if, which would make them equal. However, we're still
without method that does select, but mutates the returned elements out from
the original array.
If we'd have it, I guess select! would be bit odd name for it? And if it would
be delete_if! then mutating delete_if would be even more surprising?

Also, I may be biased with my own problem thinking that select that removes
elements from array is typical need. Just feels so at the moment.

--
++ytti

Robert Dober

4/11/2008 5:05:00 PM

0

On Fri, Apr 11, 2008 at 6:36 PM, David A. Black <dblack@rubypal.com> wrote:
> Hi --
>
>
> On Sat, 12 Apr 2008, ara.t.howard wrote:
>
>
> >
> > On Apr 11, 2008, at 10:26 AM, Saku Ytti wrote:
> >
> > > Hey everyone,
> > >
> > > I was looking today for version of Array#select that would mutate the
> array
> > > by deleting selected elements from it.
> > > I first looked at delete_if, then noticed that it actually mutates
> original
> > > array and returns mutated array. I wonder would it make sense for
> delete_if
> > > not to mutate and return new array and delete_if! to mutate and return
> deleted
> > > elements?
> > >
> > > Of course what I want is simple method to implement, it just seems
> pretty
> > > typical desire for Array#select usage cases and also perhaps mutating
> > > delete_if is as well bit unexpected? Problem is, suggested change would
> > > break many current delete_if usage cases.
> > >
> >
> > a non-mutating delete_if is already there:
> >
> > array.select{|element| not orginal_condition(element)}
> >
> > ie
> >
> > odds = [0,1,2,3].select{|i| not i.even?}
> >
>
> Also:
>
> odds = [0,1,2,3].reject {|i| i.even? }
I guess even I could come up with an odd solution. (t'is 1 funny maybe?)
R.
--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Robert Dober

4/11/2008 5:09:00 PM

0

On Fri, Apr 11, 2008 at 6:46 PM, Saku Ytti <saku@ytti.fi> wrote:
> On (2008-04-12 01:32 +0900), David A. Black wrote:
>
> Thanks everyone for fast reply.
>
>
> >> I first looked at delete_if, then noticed that it actually mutates original
> >> array and returns mutated array. I wonder would it make sense for delete_if
> >> not to mutate and return new array and delete_if! to mutate and return deleted
> >> elements?
> >
>
> > You should use #reject, which is non-destructive.
>
> I guess I wasn't being clear, indeed reject removes the need for
> non-mutating delete_if, which would make them equal. However, we're still
> without method that does select, but mutates the returned elements out from
> the original array.
> If we'd have it, I guess select! would be bit odd name for it? And if it would
> be delete_if! then mutating delete_if would be even more surprising?
>
> Also, I may be biased with my own problem thinking that select that removes
> elements from array is typical need. Just feels so at the moment.
No, I agree with you but OTOH not many seem to see the need for this
and the community is rather pragmatic about these things.
I would prefer to have a reject and a reject! but I am pragmatic about
democracy ;), and a large majority seems not to see the need for this.
Now I would love to stand corrected of course ;).

Cheers
Robert
>
> --
> ++ytti
>
>



--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

David A. Black

4/11/2008 5:27:00 PM

0

Hi --

On Sat, 12 Apr 2008, Saku Ytti wrote:

> On (2008-04-12 01:32 +0900), David A. Black wrote:
>
> Thanks everyone for fast reply.
>
>>> I first looked at delete_if, then noticed that it actually mutates original
>>> array and returns mutated array. I wonder would it make sense for delete_if
>>> not to mutate and return new array and delete_if! to mutate and return deleted
>>> elements?
>>
>> You should use #reject, which is non-destructive.
>
> I guess I wasn't being clear, indeed reject removes the need for
> non-mutating delete_if, which would make them equal. However, we're still
> without method that does select, but mutates the returned elements out from
> the original array.
> If we'd have it, I guess select! would be bit odd name for it? And if it would
> be delete_if! then mutating delete_if would be even more surprising?
>
> Also, I may be biased with my own problem thinking that select that removes
> elements from array is typical need. Just feels so at the moment.

So you want a kind of #keep_if ?


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.r... for details and updates!

Saku Ytti

4/11/2008 5:27:00 PM

0

On (2008-04-12 02:08 +0900), Robert Dober wrote:

> No, I agree with you but OTOH not many seem to see the need for this
> and the community is rather pragmatic about these things.
> I would prefer to have a reject and a reject! but I am pragmatic about
> democracy ;), and a large majority seems not to see the need for this.
> Now I would love to stand corrected of course ;).

Hmm yeah, so you'd name them something like this?

class Array;def reject!(&blk)
reject(&blk).each{|e|delete e};end
def select!(&blk)
select(&blk).each{|e|delete e};end;end

so reject! does mutating reject and returns non-rejected
and select! does mutating select and returns non-selected.

So if I'd want the 'select' behaviour I was missing, I'd
just call reject!

Upside of this solution is, it wouldn't tinker with
anything existing. But still not sure if people commonly
want something like this.

--
++ytti

Saku Ytti

4/11/2008 5:32:00 PM

0

On (2008-04-12 02:26 +0900), Saku Ytti wrote:

> class Array;def reject!(&blk)
> reject(&blk).each{|e|delete e};end
> def select!(&blk)
> select(&blk).each{|e|delete e};end;end

Actually just inside out, reject! should be select(&blk)
and select! should be reject(&blk).

--
++ytti

Saku Ytti

4/11/2008 5:34:00 PM

0

On (2008-04-12 02:26 +0900), David A. Black wrote:

> So you want a kind of #keep_if ?

Just to implement the previous suggestion correctly(tm) this time.

This is more or less what I want
class Array;def reject!(&blk)
select(&blk).each{|e|delete e};end
def select!(&blk)
reject(&blk).each{|e|delete e};end;end

now if I do array.reject! I actually get array mutated
but it returns the non-rejected.
So kinda destructive/mutating select.

--
++ytti