7 Functional Programming Techniques EVERY Developer Should Know

preview_player
Показать описание

In this video, I'll walk you through 7 functional programming techniques and demonstrate how they work. Although Python is not a purely functional language, functional programming can significantly improve your Python skills.

🔖 Chapters:
0:00 Intro
0:32 #1. Recursion
4:45 #2. Structural Pattern Matching
6:17 #3. Immutability
8:49 #4. Pure Functions
11:11 #5. Higher-Order Functions
15:53 #6. Function Composition
18:42 #7. Lazy Evaluation
20:47 Outro

#arjancodes #softwaredesign #python
Рекомендации по теме
Комментарии
Автор

This is a nice, concise overview of some core, simple, practical functional concepts. I just want to make one note about your section on function composition. Notice at 16:33, Copilot suggests an implementation of the compose function for exactly 2 functions, which applies the functions in the correct order (based upon the mathematical definition of function composition). Specifically, the composition of functions f and g, which would be called as compose(f, g), first applies g (the right hand function) to an input (x), and then applies f to the result (i.e., compose(f, g)(x) == f(g(x))). However, the compose function that you instead implemented, taking any number of functions of type Composable, applies the functions in order from left to right, not right to left, because reduce traverses the list of functions from left to right. Therefore, your resulting compose function is not truly composing functions in the mathematical sense, and is generally called by other names in other languages (e.g., foldl or pipe) or even in existing functional Python libraries. For your compose function to behave similarly to the original Copilot suggestion, you would need to traverse the list of functions from right to left. This is also evidenced by the fact that in your original code leading up to your introduction of function composition, you had the expression (at 15:10) sort_fn(add_10(multiply_by_2(data))), but then (at 18:22) had to reverse the order of the functions in compose(multiply_by_2, add_10, quick_sort) to get the same result because your compose function applies the function in the reverse ("wrong") order.

cjdaniels
Автор

Worth noting that copy() only returns a shallow copy. So it's a new list but if the original elements were mutable (yours were ints, so that's fine) then modifying them in the new list will also modify them in the old list. This can be a big gotcha in parallel code.
Deepcopy from the copy module is _almost_ always what you need when you need an expensive copy of non fundamental types.

DrGreenGiant
Автор

As always, it's another masterpiece. Thanks, Arjan.
I believe the input ‘data’ at 20:09 could still be a list, instead of an iterator; Only the return needs to be an iterator
In addition, we can still achieve the lazy evaluation 20:24 by simply using the result of the map function, without casting into a list

victoradukwu
Автор

Recently tried out Gleam was super fun! Python is my main language so really cool to see these ideas in Python, great timing Arjan!

TheUpriseConvention
Автор

Bold of you to assume I'm going on any dates.

TobyDillman
Автор

How have you managed to explain quicksort in 10 seconds better than my professor in a whole godamn semester. God.

jonathan
Автор

Really enjoyed this video. The advanced concepts are appreciated.

thisoldproperty
Автор

This is really spot on
I was struggling with function composition for awhile

benfung
Автор

I use some of these techniques. Matching structures, composition and lazy code are the new ones I am about to discover thanks to this video. Thx for this great work 🙏

Semicolon
Автор

Dear Arjan, please add 1 second pause after printing code, because its hard to stop video to read code properly

ziond
Автор

Another great video from ArjanCodes. I love the pace, the tone and the way you explain concepts. Very clear and to the point.
Many thanks!

josecastron
Автор

Another benefit of pure functions: They are easy to cache, as you know that putting the same values inside will give you the same result.
This can help you, if you have long calculations on certain values.

EliasLiomanKirchgässner
Автор

Another great video. As a newbie, these really jelp to understand the important thingen. Thanks yuo!

justinhall
Автор

I've been doing a lot of the stuff in this video in Rust, it's nice to see a Python implementation of the same ideas which I can start to use in my day job.

gpcureton
Автор

I just understood what yield does, thank you Arjan

guyothomas
Автор

in the bubble sort, the inner loop starts from the outer loop starts which means that there are a subset of elements in the beginning that are not sorted.

BenjaminMorrison-pzin
Автор

0:28 "As a side effect..." I saw what you did there.
Nice recursion example... quicksort is so much more likely to be used than factorial.

edgeeffect
Автор

Thx Arjan! Partial is very useful! Its new for me😊

sviteribuben
Автор

Thank you for this amazing video! I learn a new good coding practice every time I watch one of these. I was wondering if you could do a video on the toolz and cytoolz libraries, which actually facilitate the application of most of the functional concepts that you demonstrated in this video, in addition to other goodies which deal with iterators and dictionaries. For example, they actually have a function called compose_left, which allows one to create a function from all the function arguments provided to it. It also allows for piping data through a series of functions, and is heavily pro-lazy evaluation. Cytoolz is the Cython-based equivalent of toolz, having the exact same functions, but much faster. Would love to know what you think of it, and what might be the best ways to apply the functions from this library to enable better coding in Python.

tuskofgothos
Автор

Great video! I have a question, can't you just do this instead:
```
def multiply_by_x(data, x: int) :
return map(lambda y: x * y, data)

def add_x(data, x: int) :
return map(lambda y: x + y, data)
```
As map returns an iterator, similarly to yield from.
So the same as your initial functions but do not cast it to list to iterate over the iterator, just return the iterator.

Wolar