News for hackers. Stuff that matters.
Re:Python iteration by settrans on Wednesday April 25, 2012 @02:34PM (#3890388)
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)
Re:Python iteration by ned_batchelder on Wednesday April 25, 2012 @04:26PM (#3890889)
I'm sorry, is this a joke? "straightforward"? Why would you use that code when you could implement the same thing more simply like this?

    def line_number_mogrifier(f):
        num = 0
        for line in f:
            yield (num, line)
            num += 1

The functional proponents say that eventually everything will be functional, I guess I'll wait and see how that goes over. People think procedurally. Functional constructs may have some technical advantages, but if adopting them shrinks the pool of effective developers, it won't catch on.