-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
List.filter_map #2185
List.filter_map #2185
Conversation
Thanks, this addition has been long overdue! One question: Is such a function available in Batteries/Core/Containers, and if so, under which name(s)? One remark: You made the function tail recursive. I think it becomes more and more difficult to justify than |
All of them under the same name: As for the tailrec discussion: I just copy pasted what was in |
This should ideally be supported by benchmarks but how about to let it use "direct" recursion up to to a certain depth and, if that depth is exceeded, use your tail-recursive implementation on the remaining tail? |
I don't think that any other function in the stdlib does this at the moment, and I don't plan to be the first person to add one like that. |
Yes, that's the usual way to get a function that works on long lists without the extra cost on small lists (and unrolling the loop a few times more than compensate the overhead of counting elements). One downside, though, is that such implementation might results in loosing inlining opportunities (with flambda) since the callback's body would be duplicated after inlining. |
I believe that the implementation is correct, but I would still like to see tests for it. |
init does that. |
Indeed, I stand corrected. Anyway: if we want to go with a "simple" version (i.e. either tail-rec or not tail-rec), I'd vote for the tail-rec one, because that's what's already used in the compiler and I don't want to:
|
I think it's already very useful to have this added to the stdlib as-is -- improved implementations using unrolling can happen at a later time, and be done consistently on all the relevant functions at the same time. |
Ok, let's go with the tail-rec version. Please add a test, and this is good to merge I think. |
Test added. |
@alainfrisch feel free to click the "squash and merge" button if you're happy with this :) |
I'm happy but I'd suffer @gasche's wrath if I accepted a Changes entry without proper author attribution. |
Ah! Indeed. I've updated the entry. |
No description provided.