very simple question about enum.all? and any?

Discussion in 'Ruby' started by scooterm@hotmail.com, Jan 7, 2008.

  1. Guest

    %w{ ant bear cat}.all? {|word| word.length >= 3} #=> true
    %w{ ant bear cat}.any? {|word| word.length >= 3} #=> true

    Both of these return true or false as expected. The question is
    is there an equivalent method that returns the original enum object
    itself (instead of just "true") if the test is satisfied? And nil or
    false
    if otherwise?

    I know code for this can be easily written, but the reason for the
    question is a matter of style; because it would be
    really nice not to have to specify the enum object more than one
    time if the test is satisfied.

    For example, in simple english, the "any" test would be: "If any item
    passes the test, then return all of the items".
     
    , Jan 7, 2008
    #1
    1. Advertising

  2. Robert Dober Guest

    On Jan 7, 2008 5:15 AM, <> wrote:
    > %w{ ant bear cat}.all? {|word| word.length >= 3} #=> true
    > %w{ ant bear cat}.any? {|word| word.length >= 3} #=> true
    >
    > Both of these return true or false as expected. The question is
    > is there an equivalent method that returns the original enum object
    > itself (instead of just "true") if the test is satisfied? And nil or
    > false
    > if otherwise?

    For #any? the response to your question is yes, #detect or #find. But see below.
    For #all? however there is none, you shall define what you want, the
    first, the last a random element satisfying the block?
    Does not make too much sense for me, but maybe you could give us a usecase?

    >
    > I know code for this can be easily written, but the reason for the
    > question is a matter of style; because it would be
    > really nice not to have to specify the enum object more than one
    > time if the test is satisfied.
    >
    > For example, in simple english, the "any" test would be: "If any item
    > passes the test, then return all of the items".

    This indeed is just a different case, I myself have enhanced grep to
    do this in my labrador library, so you can say
    enum.grep{|e| e.length > 2 } a common idiom to do this is

    enum.map{|e| cond(e) ? : e : nil }.compact

    HTH
    Robert

    --
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
     
    Robert Dober, Jan 7, 2008
    #2
    1. Advertising

  3. 2008/1/7, Robert Dober <>:
    > On Jan 7, 2008 5:15 AM, <> wrote:
    > > %w{ ant bear cat}.all? {|word| word.length >= 3} #=> true
    > > %w{ ant bear cat}.any? {|word| word.length >= 3} #=> true
    > >
    > > Both of these return true or false as expected. The question is
    > > is there an equivalent method that returns the original enum object
    > > itself (instead of just "true") if the test is satisfied? And nil or
    > > false
    > > if otherwise?

    > For #any? the response to your question is yes, #detect or #find. But see below.


    I don't think so. The OP wanted the enum not the element(s).

    > For #all? however there is none, you shall define what you want, the
    > first, the last a random element satisfying the block?


    He wants the enum. Like in

    def my_test(enum)
    enum.any? {|x| x.length >= 3} ? enum : nil
    end

    > Does not make too much sense for me, but maybe you could give us a usecase?


    That would be good. At the moment I cannot see the advantage of the
    proposed solution because if you wanted to work with the return value
    you would have to store it in a local variable anyway. Otherwise you
    would end up invoking an Enumerable method on nil.

    > > I know code for this can be easily written, but the reason for the
    > > question is a matter of style; because it would be
    > > really nice not to have to specify the enum object more than one
    > > time if the test is satisfied.
    > >
    > > For example, in simple english, the "any" test would be: "If any item
    > > passes the test, then return all of the items".

    > This indeed is just a different case, I myself have enhanced grep to
    > do this in my labrador library, so you can say
    > enum.grep{|e| e.length > 2 } a common idiom to do this is


    The bit above can be done with #select - IMHO no need to enhance #grep for this.

    > enum.map{|e| cond(e) ? : e : nil }.compact


    What is the advantage of the bit above over

    enum.select {|e| cond(e)}

    ? Am I missing something?

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Jan 7, 2008
    #3
  4. Robert Dober Guest

    On Jan 7, 2008 4:37 PM, Robert Klemme <> wrote:
    > 2008/1/7, Robert Dober <>:
    > > On Jan 7, 2008 5:15 AM, <> wrote:
    > > > %w{ ant bear cat}.all? {|word| word.length >= 3} #=> true
    > > > %w{ ant bear cat}.any? {|word| word.length >= 3} #=> true
    > > >
    > > > Both of these return true or false as expected. The question is
    > > > is there an equivalent method that returns the original enum object
    > > > itself (instead of just "true") if the test is satisfied? And nil or
    > > > false
    > > > if otherwise?

    > > For #any? the response to your question is yes, #detect or #find. But see below.

    >
    > I don't think so. The OP wanted the enum not the element(s).
    >
    > > For #all? however there is none, you shall define what you want, the
    > > first, the last a random element satisfying the block?

    >
    > He wants the enum. Like in
    >
    > def my_test(enum)
    > enum.any? {|x| x.length >= 3} ? enum : nil
    > end
    >
    > > Does not make too much sense for me, but maybe you could give us a usecase?

    >
    > That would be good. At the moment I cannot see the advantage of the
    > proposed solution because if you wanted to work with the return value
    > you would have to store it in a local variable anyway. Otherwise you
    > would end up invoking an Enumerable method on nil.
    >
    > > > I know code for this can be easily written, but the reason for the
    > > > question is a matter of style; because it would be
    > > > really nice not to have to specify the enum object more than one
    > > > time if the test is satisfied.
    > > >
    > > > For example, in simple english, the "any" test would be: "If any item
    > > > passes the test, then return all of the items".

    > > This indeed is just a different case, I myself have enhanced grep to
    > > do this in my labrador library, so you can say
    > > enum.grep{|e| e.length > 2 } a common idiom to do this is

    >
    > The bit above can be done with #select - IMHO no need to enhance #grep for this.

    I am not worthy, I am just posting nonsense, arrgh

    >
    > > enum.map{|e| cond(e) ? : e : nil }.compact

    >
    > What is the advantage of the bit above over
    >
    > enum.select {|e| cond(e)}


    You tell me ;)
    >
    > ? Am I missing something?

    No I have been quite stupid...

    But one thing really strikes me, if OP wanted the enum
    e.any? {} && e
    e.all? {} && e
    would be what he wants, right? I must be quite confused....

    Cheers
    Robert

    >
    > Kind regards
    >
    > robert
    >
    > --
    > use.inject do |as, often| as.you_can - without end
    >
    >




    --
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
     
    Robert Dober, Jan 7, 2008
    #4
  5. On 07.01.2008 22:50, Robert Dober wrote:
    > But one thing really strikes me, if OP wanted the enum
    > e.any? {} && e
    > e.all? {} && e
    > would be what he wants, right? I must be quite confused....


    No, actually this is better than the ternary operator thingy that I
    suggested above. Umpf! That's a good example that communities can turn
    up better solutions than individuals. (Although, since only Robert was
    involved...) :)

    I am still waiting for a proper use case of this though. OP?

    Cheers

    robert
     
    Robert Klemme, Jan 8, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    480
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,627
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    425
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    516
    Dave Thompson
    Feb 14, 2005
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,299
Loading...

Share This Page