Skip to content
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

Suggests reduce vs inject, but they're the same method #37

Open
krainboltgreene opened this issue Nov 23, 2015 · 13 comments
Open

Suggests reduce vs inject, but they're the same method #37

krainboltgreene opened this issue Nov 23, 2015 · 13 comments
Labels

Comments

@krainboltgreene
Copy link

The chapter on combining objects suggests that reduce and inject are different methods for different tasks.

@franzejr
Copy link
Owner

I think it was @rafaelsales who did this. Both are not combining the elements? This part here? http://best-ruby.com/idiomatic_ruby/combine_elements_in_collection.html

@imogenkinsman
Copy link

It's very unclear in the example that reduce and inject are aliases.

@rafaelsales
Copy link
Collaborator

It's unclear just as in ruby documentation: http://ruby-doc.org/core-2.2.3/Enumerable.html

Both words also has 6 letters. Why? Have you seen the examples?

Why? Because the different names help understand the intention of the code.
It's really about writing idiomatic ruby code.
My examples show patterns of code that should be either refactored using reduce or inject word

@krainboltgreene
Copy link
Author

Idiomatic ruby doesn't necessarily mean semantic or good practice. I guess this book should figure out which it cares more about: Idiomatic or semantic.

@rafaelsales
Copy link
Collaborator

The language makes available both reduce and inject words and clearly states different examples for each of them.
So what can we do to help people choosing between them and write the Best Ruby code?
Let's show the choices we have in the language that produce same result, and state which ones are good choices in which situations.

@krainboltgreene
Copy link
Author

Are we looking at different examples? They look the same to me.

@rafaelsales
Copy link
Collaborator

They are not. Compare the examples in the book with the examples in ruby doc. Basically you won't find the use of numbers.inject(:+);

@krainboltgreene
Copy link
Author

Oh, you're comparing the Best Ruby book vs the Documenation. Yes, of course, that's my point. Best Ruby suggests there's a difference implicitly where as there is zero difference in the documentation.

@franzejr
Copy link
Owner

@krainboltgreene I think I got your point. You are saying we are not making the BEST or at least a better idiomatic approach, we are just saying inject and reduce can do the same thing.

@franzejr
Copy link
Owner

Another point of view can be: How can I combine two elements? I can use inject and reduce and those are idiomatic ruby codes. I think, at the beginning this was the point and right now I think you are asking us what is the BEST or even the most idiomatic ruby code. I don't know the answer, what do you think? Probably we should define what is idiomatic ruby. I can combine two or more elements in collections using a non-idiomatic ruby code, but If I use inject OR reduce, I will use an idiomatic ruby code.

@franzejr
Copy link
Owner

In this section: "idiomatic ruby code", we should create a sort of idiomatic ruby code, it means: I can do this thing using a non-idiomatic ruby code, BUT I can also use some approaches in the language and that will look more idiomatic ruby, but it does not mean this is the best approach OR this is the BEST idiomatic ruby code. What do you guys think?

@rafaelsales
Copy link
Collaborator

@krainboltgreene Why the documentation uses reduce(:+) in both methods examples? And why the doc. uses blocks only for inject in both methods examples? Why the documentation doesn't even mention one is the alias of the other? And finally why both exist if the word reduce is the popular one out there?

@krainboltgreene
Copy link
Author

Why the documentation uses reduce(:+) in both methods examples?

Largely because documentation is boring and cruby core documentation has a huge amount of hoops to jump through, so Copy/Paste becomes the most efficient solution.

Why the documentation doesn't even mention one is the alias of the other?

Because it probably appeared obvious to the writer.

And finally why both exist if the word reduce is the popular one out there?

Probably the same reason we have File.write and File.open { |f| f.write } and File.read and File.open().read (though the last one has different side effects). Ruby is an language built on top of many other language idioms. I doubt there's an intent in the design.

For what it's worth I would just talk about reduce and be done with it. I mean I learned how to merge objects doing list.inject(:merge), so...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants