Skip to content

Commit 6853f8c

Browse files
committed
[GR-20446] Implement Enumerator::Lazy#filter_map
PullRequest: truffleruby/2667
2 parents a101c74 + 3c68160 commit 6853f8c

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Compatibility:
2929
* Update `File.basename` to return new `String` instances (#2343).
3030
* Allow `Fiber#raise` after `Fiber#transfer` like Ruby 3.0 (#2342).
3131
* Fix `ObjectSpace._id2ref` for Symbols and frozen String literals (#2358).
32+
* Implemented `Enumerator::Lazy#filter_map` (#2356).
3233

3334
Performance:
3435

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- encoding: us-ascii -*-
2+
3+
require_relative '../../../spec_helper'
4+
require_relative 'fixtures/classes'
5+
6+
ruby_version_is "2.7" do
7+
describe "Enumerator::Lazy#filter_map" do
8+
it "maps only truthy results" do
9+
(1..Float::INFINITY).lazy.filter_map { |i| i if i.odd? }.first(4).should == [1, 3, 5, 7]
10+
end
11+
12+
it "does not map nil results" do
13+
(1..Float::INFINITY).lazy.filter_map { |i| i.odd? ? i : nil }.first(4).should == [1, 3, 5, 7]
14+
end
15+
end
16+
end

spec/tags/truffle/methods_tags.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ fails:Public methods on TracePoint should include parameters
5757
fails:Public methods on TracePoint should include raised_exception
5858
fails:Public methods on TracePoint should include return_value
5959
fails:Public methods on ENV.singleton_class should include freeze
60-
fails:Public methods on Enumerator::Lazy should include filter_map
6160
fails:Public methods on Enumerator::Lazy should include with_index
6261
fails:Public methods on IO should include set_encoding_by_bom
6362
fails:Public methods on BasicSocket should include read_nonblock

src/main/ruby/truffleruby/core/enumerator.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,15 @@ def drop_while
393393
end
394394
end
395395

396+
def filter_map
397+
raise ArgumentError, 'Lazy#filter_map requires a block' unless block_given?
398+
399+
Lazy.new(self, enumerator_size) do |yielder, *args|
400+
result = yield(*args)
401+
yielder.yield result if result
402+
end
403+
end
404+
396405
def select
397406
raise ArgumentError, 'Lazy#{select,find_all} requires a block' unless block_given?
398407

0 commit comments

Comments
 (0)