~/Go Iterators Are Concerning
Oct 22, 2024
Consider Python generators.
A simple function generator which clearly expresses the idea of yielding some value.
Go has decided to use a much less readable syntax, though I’d have to grant that Go is strongly typed and thus requires more literal typing to express what a function may or may not take or return. That being said, Iterators in Go are more verbose and seem to have arbitrary limitations on their use in regular code.
The return type of an Iterator is not concise and so two new types were added to the library, Seq
and Seq2
the first for iterators that yield a single value and the second for iterators which yield two values.
Consider the following snippet taken from go.dev and contrast it with Python generators
The main problem is the amount of line noise.
To take a step back and read this out loud. We’re defining a function which returns a function which takes a function, which takes an argument of type E and returns a bool. When we expand this all out we get this
|
|
Looking past the definition, I was surprised to realize that yield
is not a keyword in itself, similar to how argv
and argc
are just variable names enforced only by convention.
References