[Feature proposal] Enumerable#group_by should take a second block to allow setting the value

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Feature proposal] Enumerable#group_by should take a second block to allow setting the value

David Carlin
Elixir has a great feature in its Enumerable,group_by/3 function where you can pass 2 functions to it. The first being what to group by (called a key_fun). The second being a function that returns the val (called the value_fun). See: https://hexdocs.pm/elixir/Enum.html#group_by/3

By default the value_fun is a function that does nothing ie. fn x -> x end (making its default behaviour the same as Ruby/Rails group_by method)

I would like this functionality in Rails. What do you think, dear community?

(Apologies if this has been brought up before. I did a quick search but found nothing)

Thanks

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature proposal] Enumerable#group_by should take a second block to allow setting the value

Kasper Timm Hansen
I think we’ll stick with `posts.group_by(&:id).transform_values { |v| v.title.uppercase }` — and it’s not because we don’t `value_fun`! 😄


Den 5. dec. 2017 kl. 01.48 skrev David Carlin <[hidden email]>:

Elixir has a great feature in its Enumerable,group_by/3 function where you can pass 2 functions to it. The first being what to group by (called a key_fun). The second being a function that returns the val (called the value_fun). See: https://hexdocs.pm/elixir/Enum.html#group_by/3

By default the value_fun is a function that does nothing ie. fn x -> x end (making its default behaviour the same as Ruby/Rails group_by method)

I would like this functionality in Rails. What do you think, dear community?

(Apologies if this has been brought up before. I did a quick search but found nothing)

Thanks

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
Kasper

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature proposal] Enumerable#group_by should take a second block to allow setting the value

Alberto Almagro
In reply to this post by David Carlin
Hi David,

For me it is confusing that #group_by does something else besides of grouping, it feels like a side effect to me. In my opinion, the solution that Kasper proposed reveals better the intention of the code.

Thanks for your proposal!

El martes, 5 de diciembre de 2017, 1:51:14 (UTC+1), David Carlin escribió:
Elixir has a great feature in its Enumerable,group_by/3 function where you can pass 2 functions to it. The first being what to group by (called a key_fun). The second being a function that returns the val (called the value_fun). See: <a href="https://hexdocs.pm/elixir/Enum.html#group_by/3" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fhexdocs.pm%2Felixir%2FEnum.html%23group_by%2F3\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGBuLmEfqr6cptFHi7q9NAS5ZMU0Q&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fhexdocs.pm%2Felixir%2FEnum.html%23group_by%2F3\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGBuLmEfqr6cptFHi7q9NAS5ZMU0Q&#39;;return true;">https://hexdocs.pm/elixir/Enum.html#group_by/3

By default the value_fun is a function that does nothing ie. fn x -> x end (making its default behaviour the same as Ruby/Rails group_by method)

I would like this functionality in Rails. What do you think, dear community?

(Apologies if this has been brought up before. I did a quick search but found nothing)

Thanks

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.