16 ms·
Go proposal: new package to provide generic slice functions
- bfrog 6y agoI found it funny the first comment seemed to deride the use of function pointers, compare, map, and filter as too different than the for loop centric Go. Even C provides sort with a function pointer for compare...
- asdfasgasdgasdg 6y agoI've seen this type of argumentation around go and it boggles my mind every time. Saying "we are not sure if we'll need a version of this function that accepts an override to the type's default comparison" only makes sense if you've never used a programming language other than go. Or the function sort.Slice for that matter.
- nitrix 6y agoIt's always the same thing. People find a language/library too complicated, so they go on their own to write a simpler version, only to find out that they missed very important cases that prevents it from having the same qualities. Grudgingly, they then gradually re-introduce what they initially strongly opposed against, but now they also have to work around their current design and bogus abstractions that wont let that happen easily. That's going to create a lot of cruft, edge cases and API complications, to the point where a bystander might decide it's too complicated and, they too, go on their own write a simpler version. Go hasn't learned a single thing.
- choeger 6y agoBut at least it keeps grad students busy in the programming language departments ;). Imagine that all we had was Haskell and maybe a strict variant of it. These guys would quickly run out of topics that could be explained in less then 5min to a casual developer.
- jolux 6y agoI’m not denying that there are a lot of difficult concepts and million dollar words in advanced functional programming but I will say that I personally took years to understand what was going on with OOP when I was learning programming. Functions made sense, classes and objects and inheritance and subtype polymorphism did not.
- bfrog 6y agoPure functions are incredibly simple thing to understand, much like an algebraic equation you provide inputs and get an output. There's a lot to love and learn from functional programming.
- deleted 6y ago[deleted]
- bollo 6y agoTimes and paradigms change. We need to thank these people or we would still be using Java EE
- mohanmcgeek 6y agoMore importantly the hardware changes and the patterns designed to plug platform shortcomings would have to go away.
- pjmlp 6y agoI rather use JEE, now JakartaEE, as Go as yet to offer anything that has parity with it.
- pjmlp 6y agoTwo anecdotes why I hate the way they went with //go:generate as workaround. Borland first attempt to C++ "generics" was the initial release of Borland International Data Structures 1.0 (BIDS) around 1990, where they used the preprocessor to generate multiple copies, something like #define LIST_T int #define LIST_TYPE MyIntList #include <bids/list.h> Rice and repeat for all required types, when BIDS 2.0 came out this was already replaced by experimental templates support. Around 2005 it was common to use Eclipse EMF framework, alongside plugins to generate Java code (<= 1.4) that would create type safe subclasses from collections with Object. So learning is a hard process, then again this was their point of view when C was created, > Although we entertained occasional thoughts about implementing one of the major languages of the time like Fortran, PL/I, or Algol 68, such a project seemed hopelessly large for our resources: much simpler and smaller tools were called for. All these languages influenced our work, but it was more fun to do things on our own.
- LanceH 6y ago> Go hasn't learned a single thing. How do you explain the success then? Go hasn't tried to be all things to everyone. There are lots of things out there that need simple solutions and Go provides for that with and nice middle ground of dev and computer performance. If I want to read from one computer, write to another computer, concurrently, with low footprint, and speed, it's a great tool. If Go has learned nothing, are Go programmers just picking something more difficult that performs worse? I've found it has dislodged services that were previously written in Node, Ruby, or a whole J2EE stack. Sure there are people out there trying to make it all things to everyone and it fails in some of those things, but it does great at others.
- kubb 6y ago> How do you explain the success then? A megacorp funding 100 engineers to work on the compiler, libraries and tooling for 10 years
- a1369209993 6y agoIn other words, the same way you explain the success of eg Java.
- erik_seaberg 6y agoJava was a safer C++. Go was a safer C. They both ignore a lot of good ideas from other languages, but I find it easier to forgive Java given its age and how much it’s caught up.
- kubb 6y agoJava also had multi-billion dollar marketing campaigns.
- yiyus 6y agoEither Go is a horrible language that did not learn anything or it is a language developed by some of the best engineers for a long time. If you are claiming that both things are true, then how do you explain it?
- frou_dh 6y ago> Even C provides sort with a function pointer for compare... That's been in non-generic Go for a while (passing a closure for sorting): https://golang.org/pkg/sort/#Slice https://golang.org/pkg/sort/#Slice
- int_19h 6y agoThe specific argument made there is even stranger. It's essentially saying that if HOFs are adopted, they will inevitably replace for-loops for common cases - and that is a bad thing! In other words, it's trying to block a feature that it specifically acknowledges would be highly popular in the name of simplicity and purity.
- qsort 6y agoI agree with the goal of the package, but I feel it doesn't do a good enough job of picking a lane between the imperative and functional approach. If you're going functional, a list is both a functor and a monad, so you definitely need at least map and flatMap for the abstraction to make sense. On the other hand there are a lot of "in place", "insert", etc, which encourage working with mutation. You probably need a bit of both, but I'd like a more top-down design better.
- sabellito 6y agoI think it's to do with the community and what people who are really into Go are interested in. The first reply to the thread deems "map" and "filter" borderline-useful, but "reduce" not worth it - that type of misunderstading just shows that the author (and the 60+ people who +1'd it) didn't spend enough time understading functional constructs yet.
- foldr 6y agoReduce is basically "loop over this list while maintaining state". If you have loops and state built in, it doesn't buy you a whole lot. I don't think the aim of the package is to move Go towards a more functional style. Rather, it's to provide a set of utility functions that mesh with the style of existing Go code. For example, it would be useful to have a utility to 'pop' an element from a slice, even though that's not a particularly functional idiom.
- sabellito 6y agoI definitely wasn't arguing about what should or should not be in the package. I just replied to the parent comment with my idea as to why the discussion around this point is happening in the proposal.
- tsdlts 6y agoOr maybe they understand them perfectly and have come across enough obtuse code using reduce to not want it as part of the language.
- parhamn 6y agoI can't wait to see what comes out of the channels/goroutine generic packages! Given go's culture it would be really interesting what they deem should make the cut and what shouldn't.
- mutatio 6y agoThe generic slice functions would certainly reduce boiler plate, but what I miss in comparison to Rust is the concept of iterators. In Rust if you were to iterate over words in a string you'd get borrowed words, so in practice lots of things are non-allocating (unless you choose to collect / take ownership) - in Go etc. instead of windowing over a string you typically fully allocate the []string result (e.g. strings.Split / strings.Fields). Sub-slicing in Go can be used to make your own iterators, but the ergonomics take a hit.
- iib 6y agoYes, iterators are typically a hard[1] problem in Go. I think this essay[2] describes perhaps the closest lazy eval iterator pattern in Go, which is much more complex to write than languages like Python. [1] https://ewencp.org/blog/golang-iterators/index.html https://ewencp.org/blog/golang-iterators/index.html [2013] [2] www.catb.org/~esr/reposurgeon/GoNotes.html [2020]
- johnmaguire 6y agoClickable: http://www.catb.org/~esr/reposurgeon/GoNotes.html http://www.catb.org/~esr/reposurgeon/GoNotes.html
- throwaway894345 6y agoI don’t understand what’s hard about iterators in Go after reading that article. I’ve written a lot of Python and Go (15 and 10 years respectively) as well as a bit of Rust and while iterators are certainly simpler in Python and Rust since Go lacks generics, most of the “challenges” presented in the article seem petty (complaints about for loops). The easiest way to write an iterator in Go is the closure/generator method and you can use it in a for loop like so: for x := next(); x != nil; x = next() { ... } Not as nice as a for/range but not a real problem. With generics we can have libraries that operate on iterators like map, filter, reduce although I also think people make way too big a deal about a tiny bit of for loop boilerplate (there are legitimate reasons for generics but I don’t think small amounts of boilerplate are among them).
- psanford 6y ago
- the-smug-one 6y ago>The higher-order function style also tends to be much slower, This shouldn't be the case. A HoF-call should be optimized into a JMP or be inlined directly. The issue of performance is more likely to come from non-mutating functions operating on datastructures which had mutation in mind.
- creata 6y agoHere's my understanding: to get higher-order function calls to compile to fast code, you need some kind of compile-time code expansion: templates in C++, specialization in Haskell, monomorphization in Rust, etc. But if you make it too easy to have code expand into multiple instances, then you lose the fast compile times that Go's famous for, and that's a hard balance to strike.
- the-smug-one 6y agoGiven a call map(a, f) and a loop for _,e := range a { f(e); } both ought to have the same code size. Yes, map(a, f) will need to be inlined, but the for-loop is already inlined.
- creata 6y agoYou're right. To be clearer, the emphasis was on making it too easy to specialize: people don't just stop at maps and filters, they specialize everything, and that can have a significant impact on compilation times. For example, in Rust, people write stuff like [0] for the tiniest bit of syntax sugar, and they often don't bother extract out the non-generic part. [0]: https://news.ycombinator.com/item?id=24088407 https://news.ycombinator.com/item?id=24088407
- ben0x539 6y agoThe current idiomatic go style is to write your own for-loops for everything. Isn't that just manual specialization executed by the programmer rather than the compiler?
- deleted 6y ago[deleted]
- solosoyokaze 6y agoA language that is functional from the ground up can be very beautiful, concise and a pleasure to program in. I agree with you though, that bolting functional programming onto Go will make it worse. Especially with the millions of lines of legacy Go code out there, it would essentially become an entirely different language.
- kubb 6y agoLuckily, with the design of generics as it is now, we'll be able to add the parts that are missing from this proposal ourselves in custom libraries. And even if the Go team doesn't implement generating efficient machine code for higher order functions, it will only matter in the 1% of the codebase that is performance critical, and I will gladly switch that to a loop.
- p5v 6y agoGreat to hear! This only encourages me to continue writing my book on generic programming with Go (https://gumroad.com/l/generic-golang https://gumroad.com/l/generic-golang)
- joelfolksy 6y agoSigh. Yes, tell us more about why the style you've written 3-4 orders of magnitude more code in is inherently simpler/clearer.
- stevebmark 6y agoMy biggest surprises learning Go were that it didn't have generics, and that it didn't have native map/reduce/filter. I think of many programs in terms of these three constructs and combining them.
- int_19h 6y agoIt's interesting that some of the comments on this issue are complaining that HOFs are non-Go-like, and that if adopted, they will "take over" for-loops, which would be undesirable.
- skywhopper 6y agoYep, different languages have different idiomatic styles, for lots of reasons. When I’m learning a new language that’s very different, I have found that breaking through these mental roadblocks opens my mind to a lot of new possibilities and considerations. No language can be everything to all people and purposes.
- specialist 6y agoLanguage enhancement proposals should also specify impact and expected benefit. How much are slices used in other languages? A little? A lot? We now have a zillion open source projects. Someone could semgrep that mountain, do some analytics. Figure out if adding slices, or whatever, is worth the bother.