Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pythonic style for functional programming

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:

  1. Is there a way in Python to approach the first style?
  2. What is the idiomatic way of doing this in Python?

Thank you.

like image 840
Nikolaos Georgiou Avatar asked Jan 04 '19 09:01

Nikolaos Georgiou


People also ask

What is Pythonic way of coding?

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.

What is functional programming style?

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”.

Can you use Python for functional programming?

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.

What is NumPy functional programming?

NumPy: Functional programming routinesTakes an arbitrary Python function and returns a NumPy ufunc. numpy.frompyfunc(func, nin, nout) piecewise() Evaluate a piecewise-defined function.


3 Answers

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]
like image 71
Adam Smith Avatar answered Sep 30 '22 06:09

Adam Smith


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)
like image 36
jpp Avatar answered Sep 30 '22 06:09

jpp


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))
like image 27
Netwave Avatar answered Sep 30 '22 05:09

Netwave