Skip to content

Add Enumerable#join_map for efficient map and join #13792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

prateekkish
Copy link

Summary

This PR adds Enumerable#join_map method that combines mapping and joining operations in a single pass through the enumerable.

Implements Feature #21386

Motivation

The current pattern of .map { ... }.join(sep) is:

  • Repetitive across Ruby codebases
  • Inefficient due to intermediate array creation
  • Could be more idiomatic

Usage

# Basic usage
[1, 2, 3].join_map(",") { |n| n * 2 }  # => "2,4,6"

# With objects
users = [{name: "Alice"}, {name: "Bob"}]
users.join_map(", ") { |u| u[:name] }  # => "Alice, Bob"

# Default separator is empty string
(1..3).join_map { |n| n }  # => "123"

Performance

Benchmarks show consistent 28-30% performance improvement:

Array Size | map{}.join (ms) | join_map (ms) | Improvement
------------------------------------------------------------
10         |          0.0015 |        0.0012 |       23.2%
100        |          0.0123 |        0.0088 |       28.6%
1000       |          0.1211 |        0.0866 |       28.5%
10000      |          1.2275 |        0.8531 |       30.5%

I haven't committed the benchmark code, but please let me know if I should do that.

This method combines mapping and joining in a single enumerable traversal,
avoiding the intermediate array creation that occurs with map{}.join.

Performance benchmarks show 28-30% improvement over traditional approach.
@prateekkish prateekkish force-pushed the feature/enumerable-join-map branch from 50eeb81 to 157b5e6 Compare July 5, 2025 07:38
@prateekkish prateekkish requested a review from nobu July 5, 2025 07:50

This comment has been minimized.

This ensures join_map behaves exactly like map{}.join by recursively
joining array elements returned by the block.
@prateekkish prateekkish force-pushed the feature/enumerable-join-map branch from 0d64618 to cc8e46c Compare July 5, 2025 17:48
@prateekkish
Copy link
Author

@nobu I have made the changes requested, thanks for a prompt review. Could I request another one?

@prateekkish prateekkish force-pushed the feature/enumerable-join-map branch from 7bdf007 to 3733558 Compare July 8, 2025 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants