Project

General

Profile

Actions

Bug #7690

closed

Enumerable::Lazy#flat_map should not call each

Added by marcandre (Marc-Andre Lafortune) almost 12 years ago. Updated almost 12 years ago.

Status:
Closed
Target version:
ruby -v:
r38794
Backport:
[ruby-core:51401]

Description

I would expect that

array.flat_map{...} == array.lazy.flat_map{...}.force

This is not always the case:

[1].flat_map{|i| {i => i} } # => [{1 => 1}], ok
[1].lazy.flat_map{|i| {i => i} }.force # => [[1, 1]], expected [{1 => 1}]

Note that Matz confirmed that it is acceptable to return straight objects instead of arrays for flat_map [ruby-core:43365]

It looks like this was intended for nested lazy enumerators:

[1].lazy.flat_map{|i| [i].lazy }.force # => [1]

I don't think that's the correct result, and it is different from a straight flat_map:

[1].flat_map{|i| [i].lazy } # => [#<Enumerator::Lazy: [1]>]

This is caused by Lazy#flat_map calls each (while Enumerable#flat_map only looks for Arrays/object responding to to_ary).

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0