Pure vs. Impure Iterators in Go
created: May 29, 2025, 8:57 a.m. | updated: May 31, 2025, 6:35 p.m.
Among all iterators, “pure” iterators arguably distinguish themselves as the easiest ones to reason about; as such, don’t they deserve their own qualifier?
¶Another question arises: if “pure” iterators are easier to reason about than “impure” ones are, shouldn’t iterators be designed as “pure” whenever possible?
Seq [ string ] { return func ( yield func ( string ) bool ) { for len( s ) > 0 { var line string if i := IndexByte ( s , '' ); i >= 0 { line , s = s [: i + 1 ], s [ i + 1 :] } else { line , s = s , "" } if !
Seq [[] byte ] { return func ( yield func ([] byte ) bool ) { s := s // local copy for len( s ) > 0 { var line [] byte if i := bytes .
IndexByte ( s , '' ); i >= 0 { line , s = s [: i + 1 ], s [ i + 1 :] } else { line , s = s , nil } if !
6 months, 2 weeks ago: Hacker News