diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e14fd114414..97be784f1715 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Compatibility: * Implement `String#undump` (#2131, @kustosz) * `Errno` constants with the same `errno` number are now the same class. +* Implement `Enumerable#tally` and `Enumerable#filter_map` (#2144 and #2152, @LillianZ). Performance: diff --git a/spec/tags/core/enumerable/filter_map_tags.txt b/spec/tags/core/enumerable/filter_map_tags.txt deleted file mode 100644 index 09b6dd0d9d5a..000000000000 --- a/spec/tags/core/enumerable/filter_map_tags.txt +++ /dev/null @@ -1,3 +0,0 @@ -fails:Enumerable#filter_map returns an empty array if there are no elements -fails:Enumerable#filter_map returns an array with truthy results of passing each element to block -fails:Enumerable#filter_map returns an enumerator when no block given diff --git a/src/main/ruby/truffleruby/core/enumerable.rb b/src/main/ruby/truffleruby/core/enumerable.rb index 88b0aa054b69..15cbbcf23d5f 100644 --- a/src/main/ruby/truffleruby/core/enumerable.rb +++ b/src/main/ruby/truffleruby/core/enumerable.rb @@ -924,6 +924,19 @@ def reject ary end + def filter_map + return to_enum(:filter_map) { enumerator_size } unless block_given? + + ary = [] + each do + o = Primitive.single_block_arg + v = yield(o) + ary << v if v + end + + ary + end + def reverse_each(&block) return to_enum(:reverse_each) { enumerator_size } unless block_given?