Hacker News new | ask | show | jobs
by klibertp 13 days ago
TL;DR: if you want a general looping construct that works no matter the container type, you need an iterator protocol that types can opt into. This is a standard technique adopted by most programming languages that are not explicitly low-level, and has existed for the past 60[1] years. The OP (re)discovered it and thought it was worth blogging about.

Well, it is kind of interesting to see how the very basic programming building block (iteration) gets generalized without incurring syntactic costs. Whether it's worth a place on the HN front page is debatable, though.

[1] Too lazy to track the actual first implementation, but I'd be astonished if the concept wasn't well-known by the 70's.

2 comments

I got curious and checked: CLU had iterators in 1975.

> Another key feature of the CLU type system are iterators

It also had exception handling and tuple unpacking!

> For instance, writing x,y := y,x would exchange values of x and y

(I'm joking, that's "just" parallel assignment, it just looks like what you do in modern Python with unpacking. And yes, it's all from 1975.)

Details: https://en.wikipedia.org/wiki/CLU_(programming_language)

The best known programming language in the 1980s, BASIC, had a FOR that was limited to counting. A lot of people without formal CS education would think Python’s for can go over a finite set of things and live happily at that level of abstraction without ever needing to peek under the bonnet and learn how iterators work (in Python).
Sure. But you know, from personal experience, you can be a happy BASIC user writing simple games without knowing about the existence of arrays. It's a bit hard, but nothing copy & paste couldn't defeat. And while learning about `DIM` was quite a breakthrough for my 8 y.o. self, I'm not sure I'd welcome an article about it on HN's front page.

The same is true for iterator protocols: as noted in my other comment, they have existed since 1975 in almost the same form (with a few closely related variants). It's not easy to find a mainstream language that doesn't support them "since forever" (in "tech years"). They're just not news, no matter how you slice it.

Still, the average programmer lives happily without ever looking what’s under the syntax of their language. While I agree the mode here on HN is not the average programmer, there are always newcomers and larvae who need our support to develop fully into adult developers.