DEV Community

Cover image for Introduction to functional programming with Python examples

Introduction to functional programming with Python examples

Duomly on October 18, 2019

This article was originally published at: https://www.blog.duomly.com/the-most-important-aspects-of-functional-programming-with-python-examples/ F...
Collapse
 
orelkan profile image
Orel Kanditan • Edited

Thanks for the article.

Personally I really don't like the syntax for functional programming in Python, it feels like it's encouraging me not to use it.. With the verbose "lambda" keyword and needing to wrap each function instead of object chaining (list.map().filter()...). I much prefer the syntax of Scala and also Javascript

Collapse
 
radekfabisiak profile image
Radoslaw Fabisiak

Had the same, my core language is Javascript, but when I started working more with Python I'm going into closer friends with his syntax.

Collapse
 
saint4eva profile image
saint4eva

Cool article. Thank you.

I also love writing functional style in C# where it makes sense, such as Linq, Lambda Expression, Recursive pattern matching, Switch expression, local function, Ranges & slices, Property pattern matching.

Once more, nice article.

Collapse
 
daveparr profile image
Dave Parr • Edited

Really good article. I come from an R background, where map, apply, reduce is very common in a number of forms.

Something I want to pick out to ensure I've understood the python variant:

In functional programming, functions are the first-class objects, also called higher-order functions β€” the data types treated the same way as other types.

I think this sentence might be able to be refined, or my understanding might be wrong.

First class functions are a feature of the language where functions can be passed as arguments/data structures.

Higher order functions are a subset of functions (like map as you identify later), but not all first class functions are higher order functions, as a higher order function modifies the behaviour of a function that is it's argument.

So while 'print' is a first class finction, e.g. it can be given as an argument, 'map' is a higher order function, as you can map a print over a list. Conversely you can't 'print' a 'map' over a list.

Am I understanding this correctly or is this not true in python?

Collapse
 
zeljkobekcic profile image
TheRealZeljko

Would be nice to have something like the Maybe-Monad in the functools package for example. Sometimes python's None is a valid output and can not be used equivalently to Nothing.

What I like to do is to break down my problem into only functions and then build up objects from them. This way I can reduce the tests around the object and easier test the functions.

Constructive feedback:

  • I think you could have added, that map and filter return you a genrator. You wrap them in your examples in list( ... ) and get the same result as in the list comprehensions.

  • A cool thing to use is the operator module. Then you can do something like this:

from operator import add, mul
from functools import reduce
print(reduce(add, [1,2,3,4]))
# => 10
print(reduce(mul, [1,2,3,4]))
# => 24

This reduces the amount of simple lambda functions like lambda x, y: x + y and makes the code more readable.

  • Cleanly written, I really like that πŸ‘
Collapse
 
ashraful profile image
Mohamad Ashraful Islam • Edited

The most important thing of Python is code indentation. Some of your functions looks indentation error to me. Otherwise it’s a nice article.

Collapse
 
haakobo profile image
Haakobo

Push enter after each line of code.

Collapse
 
greatbahram profile image
Bahram Aghaei

Great article! I really like it! I think, you should write another article in this matter something for intermediate users and expand this concept.

Thanks!

Collapse
 
s312569 profile image
Jason Mulvenna

You misspelt Clojure and Haskell...