Project

General

Profile

Actions

Feature #19787

open

Add Enumerable#uniq_map, Enumerable::Lazy#uniq_map, Array#uniq_map and Array#uniq_map!

Added by joshuay03 (Joshua Young) 10 months ago. Updated about 2 months ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:114309]

Description

I would like to propose a collection of new methods, Enumerable#uniq_map, Enumerable::Lazy#uniq_map, Array#uniq_map and Array#uniq_map!.

TL;DR: It's a drop in replacement for .map { ... }.uniq, with (hopefully) better performance.

I've quite often had to map over an array and get its unique elements. It occurred to me when doing so recently that Ruby doesn't have a short form method for doing that, similar to how .flat_map { ... } replaces .map { ... }.flatten and .filter_map { ... } replaces .map { ... }.compact (with minor differences). I think these new methods could be beneficial both in terms of better performance and writing more succinct code.

I've got a draft PR up with some initial benchmarks in the description: https://github.com/ruby/ruby/pull/10269.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like1Like0Like0Like0