Question: How to get size of cloned lazy enumerator

Question

How to get size of cloned lazy enumerator

Answers 1
Added at 2017-11-30 17:11
Tags
Question

We have an Enumerator::Lazy object

a = [1,2,3].lazy.map {} #=> <Enumerator::Lazy: #<Enumerator::Lazy: [1, 2, 3]>:map>
a.size #=> 3
a.clone.size #=> nil

Does anyone have correct explanation of such behaviour ? I know that size returns size of the enumerator, or nil if it can’t be calculated lazily. When we clone object it returns

a.clone #=> <Enumerator::Lazy:<Enumerator::Generator:0x00007fdaa80218d8>:each>
Answers to

How to get size of cloned lazy enumerator

nr: #1 dodano: 2017-11-30 19:11

I know that size returns size of the enumerator, or nil if it can’t be calculated lazily.

size for a Enumerator is not necessarily a real thing ( or atleast not what one would think it is) which may be the reason this change was implemented.

For example

[1,2,3].to_enum.size
#=> nil

[1,2,3].to_enum(:each) { 1 }.size #=> 1
#=> 1

or better still

[1,2,3].to_enum(:each) { "A" }.size 
#=> "A" 

When we clone object it returns a.clone #=> <Enumerator::Lazy<Enumerator::Generator:0x00007fdaa80218d8>:each>

This seems to be a change in 2.4 where it appears that it reverts back to the :each method (possibly through enum_for) because in 2.3 the reference to map is retained as is the size. Obviously due to the reversion no iteration has occurred and size cannot be determine in a "lazy" fashion and thus nil

Source Show
◀ Wstecz