Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Enumerator::Lazy#filter_map
[U] () { (E elem) -> (nil | false | U) } -> ::Array[U]
() -> ::Enumerator[E, ::Array[untyped]]
lazy.filter_map { |obj| block } → lazy_enumerator
enumerator.c
View on GitHub ↗
static VALUE
lazy_filter_map(VALUE obj)
{
LAZY_NEED_BLOCK(filter_map);
return lazy_add_method(obj, 0, 0, Qnil, Qnil, &lazy_filter_map_funcs);
}
Like Enumerable#filter_map, but chains operation to be lazy-evaluated.
Example
(1..).lazy.filter_map { |i| i * 2 if i.even? }.first(5) #=> [4, 8, 12, 16, 20]