Functional Programming in Python
While working on Scoggle I came across a number of Functional Programming concepts that I could use to implement it - much to my surprise. Here are the concepts I came across:
Map
return list(map(lambda x: self.get_file_without_extension(x), files))
Filter
This example has a map and a filter and is very similar to a Haskell list comprehension.
= [os.path.join(root, f) for f in filenames if f.endswith(".scala") and strategy(root, dirnames, f)] hits
Higher-Order Functions
In this example the returned m is a reference to the constructor of a class.
def get_class(self, kls):
= kls.split('.')
parts = ".".join(parts[:-1])
module = __import__( module )
m for comp in parts[1:]:
= getattr(m, comp)
m return m
Currying
We get to curry a function with Pythons functools library:
= fp.partial(cut.match_test_file, None, None) matches_file