for in r-devel

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

for in r-devel

Gabor Grothendieck
Regarding, this news item for r-devel:

‘for()’ loops are generalized to iterate over any object with ‘[[’ and
‘length()’ methods. Thanks to Hervé Pagès for the idea and the patch.

Below dd is an object for which [[ and length work but the result is
still numeric rather than Date class in  "R Under development
(unstable) (2016-03-15 r70334)" as observed in the comments to:
http://stackoverflow.com/questions/36074344/why-does-for-convert-date-to-numeric#comment59794873_36074344
Expanding on that:

    dd <- Sys.Date() + 0:1

    dd[[1]]  # [[ works
    ## [1] "2016-03-18"

    length(dd)  # length works
    ## [1]  2

    for(d in dd) str(d)  # gives numeric rather than Date class
    ## num 16878
    ## num 16879


--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: for in r-devel

Michael Lawrence-3
Thanks for pointing this out. The commit you cite was reverted a couple
days later, because we are still thinking about how it should work. While
it appears Date would benefit, weird things happen e.g. with POSIXlt and
other data structures in the wild. This is due to inconsistency in the
behaviors of length() and [[() methods. One thought is introducing a
generic that coerces to something with consistent behavior, e.g.
iterable(). We were even thinking that an iterable might be allowed to
return NA for length, in which case it would need to support methods like
next() and hasNext(). Ideas welcome.

Michael





On Fri, Mar 18, 2016 at 7:25 AM, Gabor Grothendieck <[hidden email]
> wrote:

> Regarding, this news item for r-devel:
>
> ‘for()’ loops are generalized to iterate over any object with ‘[[’ and
> ‘length()’ methods. Thanks to Hervé Pagès for the idea and the patch.
>
> Below dd is an object for which [[ and length work but the result is
> still numeric rather than Date class in  "R Under development
> (unstable) (2016-03-15 r70334)" as observed in the comments to:
>
> http://stackoverflow.com/questions/36074344/why-does-for-convert-date-to-numeric#comment59794873_36074344
> Expanding on that:
>
>     dd <- Sys.Date() + 0:1
>
>     dd[[1]]  # [[ works
>     ## [1] "2016-03-18"
>
>     length(dd)  # length works
>     ## [1]  2
>
>     for(d in dd) str(d)  # gives numeric rather than Date class
>     ## num 16878
>     ## num 16879
>
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel