Almost any modern mainstream imperative programming language has some stuff of functional programming.
But not all of them are really cool.
E.g., LINQ looks very nice in .NET world. After several years of practice it drives like something native, as it was from the begining of time.
Conversely, I don’t feel the same at line of python.
regions = set()
for s in regions_strs:
for token in s.split(‘/’):
regions.add(token.strip()) # functional implementation
regions = reduce(
lambda regions_set, regions_str:
lambda regions_set, region:
regions_set | set((region.strip(),)),
set()) As you can see imperative implementation is much more clear and compact.
Well, for now (one year later) I have a bit clear and simple really functional solution:
unique_regions = iunique(token.strip() for token in itertools.chain(*(s.split('/') for s in regions_strs)))
where iunique is a special generator that used as filter for abstract iterator
def iunique(iterable, key=lambda x: x): """ unique filter """ seen = set() for elem, ekey in ((e, key(e)) for e in iterable): if ekey not in seen: yield elem seen.add(ekey)