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
proposal: Go 2: Higher Order Collection Functions #31513
Comments
Some examples are actually less-clear I might argue:
However, I agree that the code and intent is easier to read in more complex cases. Nevertheless, I suspect a measurable slowdown between the new |
A couple of thoughts, but before I get to the critique, I just want to mention that I actually like such a style of programming. (Just not in Go, perhaps).
This proposal seems to depend on Generics in Go2. Personally I hope generics don't get introduced in Go. And if they happen to get introduced, implementing these functions for yourself is trivial and I'm not sure if the standard library should include them. EDIT: Another small point I want to make. But you mention:
I'm not sure if I agree there. It seems to mostly reduce typing. But that won't be the great benefit to productivity. It just might be cleaner to read (imo). |
Thank you @DylanMeeus for your thoughts.
Yes you are right.
I am not sure - is that a technical limitation?
It needs not depend on generics. If we have generics it would indeed be trivial to implement, but maybe it could be implemented without having free-for-all generics.
The ability to have cleaner code to read, is it not an improvement to productivity? |
For me, not having a clean solution without generics does sound like a technical limitation. My last argument was badly constructed, sorry, I'll try to improve it. Anecdotally, at a place where I worked we migrated to Java 8 and whilst the functional methods were cleaner to read (less clutter) they were harder to understand at a glance by people who did not use such methods before. Of course, this is just a matter of learning how to use the methods. :) Anyway, I'll leave the question of productivity in the middle. For some it'll be more natural to use and for others it will not. But let's not have that distract us from the proposal at hand :) My main complaint would be the necessity for Generics. Maybe others see a clean solution without introducing them? |
While personally I understand the need for Generics, I am concerned that it might add complexity to the language. This approach is fairly simple in usage, maybe not in its implementation - that's is the beauty of Go (go routines and channels are other such examples). I am keen to know if:
|
Marking on hold until #15292 (generics) is resolved one way or another. |
It depends on the implementation. In some cases, I might argue that there would be a measurable gain in performance where parallelism comes in effect. For example func aVeryTimeConsumingFunction(a SomeStruct) SomeValue
var results = make([]SomeValue, len(someInput))
for i := range someInput {
results[i] = aVeryTimeConsumingFunction(someInput[i])
}
//above is purely sequential
//this one can be concurrent (internal implementation)
var results = collections.map(someInput, aVeryTimeConsumingFunction) We can manually use channels and routines to achieve the same, but that is only one level of data nesting.
That is my point, with 2-3 levels of data nesting the code will become obscure with |
This is a set of specific functions that could be implemented with a general purpose generics implementation if we had one. I'm going to close this in favor of #15292 . If that issues does not succeed, we can return to this idea. |
The proposal is to bring higher order functions to slice processing in golang.
The work could be done by golang runtime or the compiler to produce required results.
I believe not only would it greatly improve the productivity of developers,
but it would take functional programming in go even further, without adding too much complexity.
The advantages:
map
can be implicitly divided across processors by the runtime to maximize resource usageRob Pike made an implementation using existing tools: https://github.com/robpike/filter
However he himself does not recommend this particular implementation.
Note that it uses reflect; not some language built-in as this proposal suggests.
Francesc Campoy also made a great talk about feasibility in his talk https://www.dotconferences.com/2015/11/francesc-campoy-flores-functional-go .
It is possible, but too complex to recommend.
Again this proposal is about having language built-ins to support higher order functions.
Many people are used to the concepts because these are very old ideas, so I will just present some examples.
map
example 1
example 2
example 3
reduce
example 1
example 2
filter
find
findIndex
each
This one is not very different or more useful than a
for-loop
but if each function call is made to run across different routines,and this is clearly mentionned in the documentation, it can be a real advantage.
The text was updated successfully, but these errors were encountered: