Comment

Comment by settrans

Line numbering a file with reduce is straightforward:

    def line_number_catamorphism(fp):
        def accumulate(a, line):
            line_number, lines = a
            return line_number + 1, lines + [(line_number + 1, line)]
        return reduce(accumulate, fp, (0, []))[1]
It may be worth nothing that zip can be straightforwardly expressed in terms of map as well, i.e.:

    zip = lambda *a: map(None, *a)
parent settrans 14 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton