I don't have much experience with Python. I'm trying to code in a functional style like I'm used to from Java and JavaScript e.g.
var result = getHeroes('Jedi')
.map(hero => { hero: hero, movies: getMovies(hero) })
.filter(x => x.movies.contains('A New Hope'));
I'm trying to do something similar in Python but I can't get the same chaining style. I had to break it down to two statements, which I don't like:
tmp = ((hero, get_movies(hero)) for hero in get_heroes('jedi'))
result = ((hero, movies) for (hero, movies) in tmp if movies.contains('A New Hope')
I have two questions:
Thank you.
In the Python community, Pythonic is the word describing code that doesn't just get the syntax right but uses the language in the way it is intended to be used. It improves the overall code quality from maintainability, readability and efficiency perspective.
Functional programming is a programming paradigm in which we try to bind everything in pure mathematical functions style. It is a declarative type of programming style. Its main focus is on “what to solve” in contrast to an imperative style where the main focus is “how to solve”.
Many programming languages support some degree of functional programming. In some languages, virtually all code follows the functional paradigm. Haskell is one such example. Python, by contrast, does support functional programming but contains features of other programming models as well.
NumPy: Functional programming routinesTakes an arbitrary Python function and returns a NumPy ufunc. numpy.frompyfunc(func, nin, nout) piecewise() Evaluate a piecewise-defined function.
As someone who adores functional programming, don't write in functional style in Python.
This hard and fast rule is a little ham-handed, and there are certainly ways to do what you're trying to do using typical functional tools like map
, filter
, and reduce
(called functools.reduce
in Python), but it's likely that your functional code will look uglier than sin, in which case there's no reason to prefer it over something imperative and pretty.
result = []
for hero in get_heros("Jedi"):
movies = get_movies(hero)
for movie in movies:
if "A New Hope" in movies:
result.append((hero, movies))
This could be done with a list comprehension, but is probably less readable.
result = [(hero, movies) for hero in get_heros("Jedi")
for movies in [get_movies(hero)] if "A New Hope" in movies]
Generator expressions are the Pythonic approach, but a functional solution is possible via a combination of map
and filter
:
mapper = map(lambda x: (x, get_movies(x)), get_heroes('jedi'))
result = filter(lambda x: x[1].contains('A New Hope'), mapper)
IMO they way of doing that in a functional style in python (not pythonic actually), using map
and filter
:
result = filter (
lambda x: x[1].contains('A New Hope'),
map(
lambda x: (hero, get_movies(hero)),
get_heroes('jedi')
)
)
The pythonic way (not very functional) would be to use a generator expresion:
result = ((hero, get_movies(hero)) for hero in get_heroes("jedi") if "A new hope" in get_movies(hero))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With