From: "baweaver (Brandon Weaver)" Date: 2021-11-29T04:46:13+00:00 Subject: [ruby-core:106306] [Ruby master Feature#18366] Enumerator#return_eval Issue #18366 has been updated by baweaver (Brandon Weaver). Interesting. It seems the common usecase you have isolated is similar to the idea of composing some function with the idea of `map`, much like we may see with `filter_map`: ```ruby [1, 2, 3].filter_map { |v| v * 2 if v.even? } # vs [1, 2, 3].filter { |v| v.even? }.map { |v| v * 2 } ``` I have mused on the idea of making a more generic composition mechanic for Enumerable methods, but this can be awkward given Ruby's OO semantics. This does get close to the idea of Transducers ([read more here](https://medium.com/@baweaver/understanding-transducers-in-ruby-209766372c39)) in which you can combine effects to bypass the inefficiencies as demonstrated by [Rich Hickey in Clojure](https://www.youtube.com/watch?v=6mTbuzafcII). Anyways, I feel like we're trying to approximate composition of Enumerable methods, which syntactically is a hard task to do but could be incredibly useful. Not sure how I'd go about it myself, but this is an interesting start to the idea. ---------------------------------------- Feature #18366: Enumerator#return_eval https://bugs.ruby-lang.org/issues/18366#change-94934 * Author: sawa (Tsuyoshi Sawada) * Status: Open * Priority: Normal ---------------------------------------- Some `Enumerable` methods return one or more of the receiver's elements according to the return value of a block it takes. Often, we want such evaluated value rather than the original element. For example, suppose we want to know the character width sufficient to fit all the strings in an array: ```ruby a = ["Hello", "my", "name", "is", "Ruby"] ``` We either have to repeat the evaluation of the block: ```ruby a.max_by(&:length).length # => 5 ``` or create a temporal array: ```ruby a.map(&:length).max # => 5 ``` both of which seem not to be optimal. I propose to have a method `Enumerator#return_eval` that returns the evaluated value(s) of the block: ```ruby a.max_by.return_value(&:length) # => 5 a.min_by.return_value(&:length) # => 2 a.minmax_by.return_value(&:length) # => [2, 5] ["Ava Davidson", "Benjamin Anderson", "Charlie Baker"] .sort_by.return_value{_1.split.reverse.join(", ")} # => ["Anderson, Benjamin", "Baker, Charlie", "Davidson, Ava"] ``` -- https://bugs.ruby-lang.org/ Unsubscribe: