News for hackers. Stuff that matters.
Re:Don’t Be Scared Of Functional Programming by chris_mahan on Wednesday July 02, 2014 @06:17PM (#7980146)
For completeness, I should add that I don't know what "reduce" means in this context.
Re:Don’t Be Scared Of Functional Programming by dllthomas on Thursday July 03, 2014 @01:11AM (#7981540)
https://docs.python.org/2/library/functions.html#reduce

Reduce (also frequently called "fold") turns a list of items into a single item (of maybe the same type or maybe a different type). It does this based on a function that tells you how to update that single item as it looks at each list item in turn.

For instance:

    def fn(a,b):
        return str(b) + a + str(b)

    reduce(fn, [1,2,3], "0")
That says:

    Start with "0" (we call this the accumulator value)
    Read the first element of the list (1)
    Pass "0" and 1 to fn
        Turn 1 into "1"
        Stick it on the front and back of "0"
        Return "101".
    Use "101" as our new accumulator value.

    Read the next element of the list (2)
    Pass "101" and 2 to fn
        Turn 2 into "2"
        Stick it on the front and back of "101"
        Return "21012".
    Use "21012" as our new accumulator value.

    Read the next element of the list (3)
    Pass "21012" and 3 to fn
        Turn 3 into "3"
        Stick it on the front and back of "21012"
        Return "3210123".
    Use "3210123" as our new accumulator value.

    Notice that we've reached the end of the list, return our latest accumulator value.