Project

General

Profile

Actions

Feature #8421

closed

add Enumerable#find_map and Enumerable#find_all_map

Added by Hanmac (Hans Mackowiak) almost 11 years ago. Updated over 3 years ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-core:55045]

Description

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

Updated by matz (Yukihiro Matsumoto) almost 11 years ago

  • Status changed from Open to Feedback

Could you tell me a concrete use-case of your find_map and find_all_map?
Usually a block for find/find_all gives boolean so that I personally have never wanted the return value from it.

Matz.

Updated by modulitos (Lucas Swart) over 3 years ago

In Ruby 2.7, I think we can use enumerable.lazy.filter_map{..}.first as an equivalent for .find_map{..}

Actions

Also available in: Atom PDF

Like0
Like0Like0