renkun-ken / rlist

A Toolbox for Non-Tabular Data Manipulation
Other
204 stars 28 forks source link

strange behaviour of list.all() #105

Closed adolfoalvarez closed 9 years ago

adolfoalvarez commented 9 years ago

Hi @renkun-ken , first of all thank you for this amazing package!

I want to report a strange behaviour both in the CRAN and dev versions when using list.all for a simple list. For example, this returns TRUE when should be FALSE:

list.all(list(2,-5,10), x ~ x < 0)
[1] TRUE

When all other functions work ok, for example:

list.count(list(2,-5,10), x ~ x < 0 )
[1] 1

Actually it seems that in this example, list.all always returns TRUE:

list.all(list(2,-5,10), x ~ x > 1000)
[1] TRUE

Is this a bug or I am using a wrong notation? Best regards! Adolfo.

renkun-ken commented 9 years ago

Thanks for your report of this behavior. It looks like list.all does not correctly handles lambda expression like x ~ x > 0 but it works correctly with . > 0 where . represents each list element of list(2,-5,10). I try to reduce unnecessary computations in the current implementation of list.all and list.any by using early-jump-out mechanism in which one violation of condition breaks the check-loop. However, the use of R's try-error in this case is a bit ugly and might not be optimal, so I'm considering a better way to implement it.

A work around at the moment for this bug is to use . > 0 instead of a lambda expression. But this is definitely a bug that should be fixed. Thanks again for your reporting.

adolfoalvarez commented 9 years ago

Thank you for the fast answer! :+1: If is useful I can tell you that I found this trying to replicate results from an old post in my blog (http://adolfoalvarez.cl/the-basics-the-classics-and-the-magics-about-lists/) This was published on August 24, 2014 and in that moment list.all seemed to work ok.

Best regards, and thank you again!

renkun-ken commented 9 years ago

The reason why list.all worked before is that the function at that time was still using the old mechanism, i.e. it evaluates the condition for all list elements no matter whether there's already one violation that determines the output to be TRUE or FALSE.

adolfoalvarez commented 9 years ago

That was fast! I just downloaded it and it works. Thank you very much @renkun-ken !!!