News for hackers. Stuff that matters.
Re:Why people are angry over Go 1.23 Iterators by klabb3 on Monday June 17, 2024 @09:50PM (#40713308)
I’m using Go daily. Usually new features in Go are addressing some real need or pain point, or standardizing on something that’s become fragmented in the 3p ecosystem. So I’m a little confused with the proposal – maybe I’m living in a parallel Go universe, but I don’t think I have ever needed or even wanted iterators. Can someone point to a real world use case where these iterators really make things better?
Re:Why people are angry over Go 1.23 Iterators by skldj28d2 on Monday June 17, 2024 @10:48PM (#40713607)
Basically every custom data structure right now has some custom implementation of iterators. This will set a standard and make them usable with range loops. Even simple library methods like scanner.Scan, strings.Split, regex.FindAll or sql.Query should return iterators.
Re:Why people are angry over Go 1.23 Iterators by 38 on Tuesday June 18, 2024 @01:12AM (#40714260)
> scanner.Scan, strings.Split, regex.FindAll or sql.Query should return iterators

Will never happen. Best you can hope for is new functions

Re:Why people are angry over Go 1.23 Iterators by skldj28d2 on Tuesday June 18, 2024 @01:53AM (#40714444)
I know. There are already proposals to add new functions to many of these packages that return iterators.
Re:Why people are angry over Go 1.23 Iterators by 38 on Tuesday June 18, 2024 @09:15AM (#40717463)
no there arent
Re:Why people are angry over Go 1.23 Iterators by 38 on Tuesday June 18, 2024 @12:45PM (#40719779)
Re:Why people are angry over Go 1.23 Iterators by tapirl on Tuesday June 18, 2024 @03:06AM (#40714889)
But is this a problem?
Re:Why people are angry over Go 1.23 Iterators by klabb3 on Tuesday June 18, 2024 @06:02AM (#40715852)
> strings.Split, regex.FindAll

But they already do, slices are iterables, and the input is bounded. Plus you have lots of other benefits like indexability. What need would it solve?

> scanner.Scan, sql.Query

Right, these are not suitable for slices because they’re unbounded. But still, what’s the use case? You still have roughly the same amount of code, no? Even the same noisy if err != nil checks. Can you provide a snippet that highlights the benefits?

Re:Why people are angry over Go 1.23 Iterators by Zababa on Tuesday June 18, 2024 @06:09AM (#40715905)
https://github.com/golang/go/issues/61405#issuecomment-16388...:

> Can you provide more motivation for range over functions?

> If the results can be generated one at a time, then a representation that allows iterating over them scales better than returning an entire slice. We do not have a standard signature for functions that represent this iteration. Adding support for functions in range would both define a standard signature and provide a real benefit that would encourage its use.

> There are also functions we were reluctant to provide in slices form that probably deserve to be added in iterator form. For example, there should be a strings.Lines(text) that iterates over the lines in a text.

Re:Why people are angry over Go 1.23 Iterators by MarkMarine on Monday June 17, 2024 @10:13PM (#40713444)
It’s called out in the proposal, but generics allow custom container types to be created like ordered map. Iterators allow nice methods to be created by designers of these containers types, and used in a way that is similar to the go collections like map and slice. Mentioned earlier in the comments, someone brought up in order tree traversal as well.

I don’t see an issue having the for range syntax that works magically for go std lib containers extended to custom containers, especially since go std lib omits so many valuable containers

Re:Why people are angry over Go 1.23 Iterators by phplovesong on Tuesday June 18, 2024 @12:10AM (#40713996)
The database driver has this row.Next() thingy that does not work with range.
Re:Why people are angry over Go 1.23 Iterators by sapling-ginger on Tuesday June 18, 2024 @12:09AM (#40713987)
You have never used for..range in Go? That's an iterator.
Re:Why people are angry over Go 1.23 Iterators by unscaled on Tuesday June 18, 2024 @12:06AM (#40713969)
This is the textbook definition of the Blub Paradox[1]. If you've mostly used Go (or moved to Go from JavaScript before it had iterators, or from Java where iterators are a royal PITA to write), it won't make sense. That's okay, since it is often hard to imagine how a feature will be used without getting used to it yourself. I'm pretty sure that most FORTRAN 66 developers coming to Go would be puzzled on why you need for loops at all, when you can write DO loops and specify the line number where the loops end (older Real Programmers[2] don't even understand why you need loops at all, since you can always GOTO anywhere you'd like).

So to cut it short, iterators, as Go implements them, should be useful at least for the following cases:

- Iterating over custom collections (e.g. trees, concurrent maps, queues) that are not provided by the language (and "range for" doesn't have custom code for inside the compiler).

- Iterating over a collection in a different way, for instance filtering out some elements or iterating over the collection backwards (as in the giving example).

- Reification - if abstract iterators can be passed to functions (instead of actual slices), you can write functions that operate on a theoretically infinite stream.

- Chaining iterators. If iterators can be reified, you can also chain them together. This is somewhat cumbersome with the syntax Go 1.23 will offer, but in many languages you can write code like this:

  let incompatibleTransactions = user.Accounts
    .Filter(account -> account.Balance <= 0)
    .FlatMap(account -> account.PendingTransactions())
    .Filter(tx -> !tx.AllowCredit())
  
Once you know what each function does, it is clear that the code goes through all the accounts that have a zero or negative balance and returns all the pending transactions that do not allow credit from these accounts.

The equivalent imperative code is harder to figure out at first glance

  incompatibleTransactions := []Transaction{}
  for _, account := range user.Accounts {
     if account.Balance < 0 {
         for tx := range account.Transactions {
             if !tx.AllowCredit() {
                 incompatibleTransactions = append(incompatibleTransactions, tx) 
             }
         }
     }
  }
If the accounts and transactions collections are not built-in the iteration code would even look worse. This code is harder to read since it conflates the What (what we want to achieve with the code) with the How (how to implement this with a for loop). Obviously, this is what imperative programming is all about, and Go is indeed becoming less imperative.

[1] http://wiki.c2.com/?BlubParadox [2] https://sac.edu/AcademicProgs/Business/ComputerScience/Pages...

Re:Why people are angry over Go 1.23 Iterators by neonsunset on Tuesday June 18, 2024 @02:36AM (#40714704)
Yup.

This is why I'm despairing so much over Go's popularity after using C#/F# and Rust which have rich and idiomatic iterator APIs. Go implementation is plain ugly, flawed and, most of all, is going to be slow unless you write it in the most verbose way due to Go's compiler weakness.

There are languages that move us forward, Go sets us back by 20 years.

Re:Why people are angry over Go 1.23 Iterators by Karrot_Kream on Tuesday June 18, 2024 @04:21AM (#40715317)
Some people enjoy expressiveness in their programming language. Some people don't. Some people like both, I'm one of them, and for some programs I choose expressive languages like Haskell or Common Lisp and for others I use unexpressive languages like Go or Zig.

Take these things a little less personally. Live and let live. People think differently from you, and that's okay.

Re:Why people are angry over Go 1.23 Iterators by neonsunset on Tuesday June 18, 2024 @07:23AM (#40716437)
This is not about personal choice but rather Go's undeserved adoption in domains it has absolutely zero business being applied to. This leads to me to generally approach engineers promoting it as misguided and severely lacking in some area of knowledge the moment Go is suggested to be used anywhere beyond light networked microservices or CLI utilities.

Instead, people shoehorn it into data processing and databases, the domain at which .NET is incomparably more adept due to performance primitives it offers, if we still talk high-level-ish languages with automatic memory management. Of course if there is no such requirement, there are many (like Zig, Rust and C++) systems programming languages that are better in every possible way at solving the task. And yet, we get Go and startups struggling to scale. But all the interesting problems in which Go is attempted to be applied to, where the solution, if successful, is despite Go not because of it, would have benefited from C#/F# which offer much better control and performance assurances, without having to fight the tool.

Re:Why people are angry over Go 1.23 Iterators by Karrot_Kream on Tuesday June 18, 2024 @01:24PM (#40720105)
Take these things a little less personally. Live and let live. People think differently from you, and that's okay.

I have meticulously benchmarked Go in previous roles and also used Go as a load generator and with smart allocation strategies the language is quite fast. I think you're letting your emotions cloud your judgement but that's nothing new in PL discourse. There's no better way to bring out those that identify with/against programming languages than PL discourse.

Re:Why people are angry over Go 1.23 Iterators by samatman on Tuesday June 18, 2024 @01:55PM (#40720402)
> I use unexpressive languages like Go or Zig

See, I would not call Zig unexpressive. I've written a number of iteration structs in Zig, because it has true optionals this is easy, using them is just:

    var iter = thing.iterateSomeWay();
    while (iter.next()) |item| { 
        // do stuff
    }
It's a low level language, yes, but an expressive one given that design constraint. Go just makes this stuff harder, because nil is just zero wearing a costume, an antipattern it inherited from C. In Zig every type can be nullable, and the compiler takes care of representing the null case distinctly from the load-bearing one.
Re:Why people are angry over Go 1.23 Iterators by pjmlp on Tuesday June 18, 2024 @07:25AM (#40716454)
Unfortunately Microsoft is also to blame, instead of .NET, all their Azure contributions to CNCF are based on Go and Rust, and all their recent CLI tools are written in Go (older ones used Python).

Also by not doing enough to make UNIX culture startups have a different opinion of Microsoft, instead of still doing M$ jokes.

The same kind of startups that won't have any qualms adopting Swift, or dealing with its current poor state on GNU/Linux.

Re:Why people are angry over Go 1.23 Iterators by foldr on Tuesday June 18, 2024 @04:41AM (#40715411)
I've used plenty of languages with iterators but have also never really missed them very much in Go. So I think this is a flawed analysis. The proposed Go iterators are just syntax sugar over some existing code patterns. They'll be nice to have but one can very easily do without them.

By way of analogy, consider that almost any language is missing lots of features present in some other languages. For example, Go does not have inheritance. If I do not miss that feature while using it, that does not necessarily mean that I'm a Blub victim.

Re:Why people are angry over Go 1.23 Iterators by klabb3 on Tuesday June 18, 2024 @05:53AM (#40715801)
GP here. Yeah I don't think I’m blubby in that sense. I used to work daily in Rust, including with custom collection types. And lots of other language experience with iterators. Go has maps and slices, and slices fit 99% of my use cases. You can index, reslice, concat, append, sort, sort by, clone, and yes, iterate with range (although the range syntax itself is merely a small convenience).

As for the chaining, yes I do miss filter and map but is again totally doable with vanilla slices (they just haven’t added those methods to the slices package).