This article was originally published at: https://www.blog.duomly.com/the-most-important-aspects-of-functional-programming-with-python-examples/
F...
For further actions, you may consider blocking this person and/or reporting abuse
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
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.
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.
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:
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?
Would be nice to have something like the Maybe-Monad in the
functools
package for example. Sometimes python'sNone
is a valid output and can not be used equivalently toNothing
.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
andfilter
return you agenrator
. You wrap them in your examples inlist( ... )
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:This reduces the amount of simple lambda functions like
lambda x, y: x + y
and makes the code more readable.The most important thing of Python is code indentation. Some of your functions looks indentation error to me. Otherwise itβs a nice article.
Push enter after each line of code.
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!
You misspelt Clojure and Haskell...