[ruby-core:117241] [Ruby master Feature#8421] add Enumerable#find_map and Enumerable#find_all_map
From:
"jeremyevans0 (Jeremy Evans) via ruby-core" <ruby-core@...>
Date:
2024-03-19 22:46:33 UTC
List:
ruby-core #117241
Issue #8421 has been updated by jeremyevans0 (Jeremy Evans).
`find_map` seems like a bad name as there is no map. map implies calling the same function over all elements in a collection, and in this case, there would be a single element (or none if nothing was found). Combining `find` and `then` seems like the simplest way now if you don't want to use `break`:
```ruby
emails.find{ pattern.match(it) }&.then{ it[:identifier] }
```
Personally, I would use the following approach is I think it is clearer:
```ruby
if match = emails.find{ pattern.match(it) }
match[:identifier]
end
```
----------------------------------------
Feature #8421: add Enumerable#find_map and Enumerable#find_all_map
https://bugs.ruby-lang.org/issues/8421#change-107328
* Author: Hanmac (Hans Mackowiak)
* Status: Feedback
----------------------------------------
currently if you have an Enumerable and you want to return the return value of #find you need eigther:
(o = enum.find(block) && block.call(o)) || nil
or
enum.inject(nil) {|ret,el| ret || block.call(el)}
neigher of them may be better than an directly maked method
same for #find_all_map
enum.lazy.map(&:block).find_all{|el| el}
it may work but it is not so good
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/