kachayev / fn.py

Functional programming in Python: implementation of missing features to enjoy FP
Other
3.35k stars 204 forks source link

Made iters.flatten flatten nested iterables of any kind, not just lists #19

Closed calebsmith closed 11 years ago

calebsmith commented 11 years ago

First off, thank you for fn.py.

I feel like I've implemented flatten() or something like it on several projects. I thought I would contribute a version that doesn't require the input to be nested lists, but nested iterables of any sort (not including strings).

I kept the original tests and added some for the additional behavior. I also changed the docstring to match the new behavior.

I've tried to match my contribution to the existing style, but just let me know if I've missed anything.

calebsmith commented 11 years ago

Refs #20

calebsmith commented 11 years ago

I see what you mean.

I'm trying to think of another way to handle basestring/str in a python 2 vs 3 agnostic way. Perhaps creating something new like "string_type" that either points to str or basestring depending on python version?

kachayev commented 11 years ago

It's better to define basestring for Python 3.

calebsmith commented 11 years ago

I made the isinstance check more explicit as another attempt at this and a possible way to avoid having to fiddle with differences in python. lmk thoughts.

I also added test cases for bytes and bytearrays because I think we don't want to iterate on them, but return them as is like the way strings are handled.