This series compares Python iterators to Haskell’s list datatype, taking a deep dive into the itertools
library and the corresponding functions in the Haskell standard library.
A Python iterator is a sort of sequence that, unlike a list, computes each subsequent value only as it is needed rather than computing the entire list all at once. In simple cases, Haskell’s list type ([]
) fulfills a similar role. In some more complex situations, we may use a “streaming” library like pipes
.
The next lessons go through all of the Python functions that return iterators. If you’re familiar with the itertools
module, these lessons will get you started quickly with Haskell’s Data.List
module:
Iterator slicing –
itertools.islice
Iteration to infinity –
itertools.count
,itertools.cycle
,itertools.repeat
Chain –
itertools.chain
Mapping –
map
,itertools.starmap
Zipping –
zip
,itertools.zip_longest
,enumerate
Take while –
itertools.takewhile
,itertools.dropwhile
Filtration –
filter
,itertools.filterfalse
,compress
Grouping –
itertools.groupby
Accumulation –
itertools.accumulate
Tee –
itertools.tee