News for hackers. Stuff that matters.
Re:Why it’s hard for programmers to write a program to flatt... by andr3w321 on Friday February 24, 2017 @04:18PM (#13727985)
Some python 2

    def flatten(x):
        def flatten_generator(x):
            if type(x) == list:
                for y in x:
                    for z in flatten(y):
                        yield z
            else:
                yield x
        return list(flatten_generator(x))

    assert(flatten([1, [2,3]]) == [1,2,3])

    ary = [1, [2,3], [4, [5,6]]]
    print flatten(ary)
Re:Why it’s hard for programmers to write a program to flatt... by falcolas on Saturday February 25, 2017 @10:26AM (#13731926)
I'd personally change `if type(x) == list` to `if isinstance(x, (list, tuple, set))` to catch all common iterables and their subclasses.