[ruby-talk:02356] Re: Function of Array.filter surprises me

From: matz@... (Yukihiro Matsumoto)
Date: 2000-04-04 01:24:26 UTC
List: ruby-talk #2356
Hi,

In message "[ruby-talk:02354] Re: Function of Array.filter surprises me"
    on 00/04/03, Quinn Dunkan <quinn@euro.ugcs.caltech.edu> writes:

|filter also makes sense to anyone who's used any language other than smalltalk
|that has this function (except perl, of course, which uses 'grep' apparently
|in an attempt to confuse shell programmers).  Uh, the non-destructive version,
|I mean (that ruby calles `detect').

I think you mean `select' or `find_all'.

|It's not a huge leap for a programmer to imagine:
|
|a.collect   -> `a' with function applied
|a.collect!  -> `a' with function applied, and modifies `a' to the same value
|
|(or map/map!, which is, after all, The Standard)

Hmm.

|then we could also have:
|
|a.filter    -> elts of `a' that are true for function
|a.filter!   -> elts of `a' that are true for function, and modifies `a' to
|               the same value

I'm not going to change `filter' behavior, so there would not be
`filter!'.  But I may declare it deprecated in the future if I find
better name.

|Notice a certain consistency?  *Most* of the standard methods have nice
|behavior here: given a method `foo' I don't have to look in the docs to know
|what `foo!' will do.  That's a big win, I think.

Agreed.  The reason for collect/filter is that I felt

  collect values in place

is weird, comparing e.g.

  sort elements in place
  substitute string in place

But if you guys feel OK for collect!/map!, I sould seriously consider
renaming `filter'.

|Hmm, that won't work... how do you use a recursive iterator?  Probably you
|don't... I'm not thinking properly in ruby today :)

Pass block using &block; e.g.

|    def foldr(&block)
|        if self.length == 0 then
|            raise ValueError, "can't fold empty list with no initial value"
|        elsif self.length == 2
|            yield self[0], self[1]
|        else
|            yield self[0], self[1..-1].foldr(&block)
|        end
|    end

							matz.

In This Thread