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
Reducers #1102
Reducers #1102
Conversation
Remove Enum.equal? Remove File.iterator/2 and File.biniterator/2
@@ -117,16 +119,14 @@ defmodule Enum do | |||
""" | |||
@spec count(t, (element -> as_boolean(term))) :: non_neg_integer | |||
def count(collection, fun) when is_list(collection) do | |||
do_count(collection, fun) | |||
folder = fn(x, acc) -> if fun.(x), do: acc+1, else: acc end | |||
:lists.foldl(folder, 0, collection) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since both this clause and the next clause are basically doing a reduction, I think we can get rid of this one altogether.
do_drop_while(list, fun) | ||
end | ||
I.reduce(collection, [], fn(entry, acc) -> | ||
if fun.(entry), do: [], else: [entry|acc] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is wrong. Basically, after the first time the function returns false, we get the rest of the list. It should be something like:
fn
(entry, []) -> if fun.(entry), do: [], else: [entry]
(entry, acc) -> [entry|acc]
end
We are basically using the accumulator to check if something was added so far (or not).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, will add tests for it aswell.
❤️ 💚 💙 💛 💜 |
list when is_list(list) -> | ||
do_all?(list, fun) | ||
end | ||
I.reduce(collection, nil, fn(entry, _) -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace nil by true here and in the next line, and we can get rid of the true return value before catch.
I have seen that you kept the specific clauses for lists in many functions but we can get rid of many of them. We definitely should remove: We should also rewrite We should consider removing the specific lists clauses for |
@josevalim New commit with discussed changes. |
I think I prefer |
I'd call it |
It is simply something that is reducible with the |
I'd like to see how the docs will describe this new stuff before deciding on the name. |
Added documentation |
It would be nice to have some historical background of the reasons for migrating from iterator to reduce. Not necessarily in the docs, it could be mentioned in a commit message. Since the main point of the protocol is reducing a collection, I'd call it
|
The problem with calling it reducer is that, if we add another important operation to |
Yeah, I think it makes sense to call it |
@alco I am writing a blog post about it. That will be included in the release announcement. :) |
No description provided.