Anouk Ruhaak – Get started with Functional Programming | otsconf 2015

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's start functional programming functional programming is getting a whole lot of attention and I'm for one find that also very confusing at times if you ask three different programmers what is functional programming exactly like in one sentence you're very likely to get even four different answers and that's confusing and all those answers may also include words like lazy evaluation or functors or monads and they sound scary and bacon what are they and today I wanted to talk a little bit about what really is functional programming and really cut through all the hard words and go through the basics of it I also want to explain a little bit why the hype because functional programming is not new so why are we all of the sudden all talking about it and last but not least I will use Python to give you some examples of functional programming in a language that is not even purely functional so what is functional programming what is it really all about and the answer is it's all about state or better it's all about the absence of state and what is state take this door for example this door can have one of two states it can be open like it is right now or it can be closed those are the states of the door that's pretty easy right let's look at that in code we have an object door the door has a property which is true now if we want to close the door as or if that property is open and open is true the door is open now what if we want to close the door we simply change the value of the property to false in doing so we change the state of that property another example is the variable below which is OTS kampf and awesome OTS come so OTS comes awesome let's make that even more awesome by adding two exclamation marks that's very simple you just take the variable and add two exclamation marks bye-bye once again changing the state of this variable and that's important I'm going to give you one more example because it goes a little bit to side effects as well this is a function that does something that I'm sure no none of you ever wants to do but it takes a list with names and then for every name in that list and doubles it basically its name plus name and insert and changes that so that it prints out young instead of young young instead of young Kim Kim instead of Kim etc in so doing this function changes the state of the list and because it changes something outside of it the function list is not inside functions outside of it we call this a side effect of the function and in this case the function doesn't do anything else all it does is entire purpose is having a side effect it doesn't return anything so that is state now if you're coming from imperative programming and you're looking at this and you're like yeah I do this every day these are very normal things for me why are you making such a big deal about state and also why would I want to use anything that doesn't allow me to do this this is pretty awesome like why not and the reason is there are a couple of problems with State the first problem is there's a thing called race conditions and has very little to do with horses now race conditions happen when you have a program that the outcome of the program depends on more than just a code you wrote it actually depends on the sequence of execution or the timing thereof it that sounds a little abstract let's take this example and we're going to do groceries today there's a lot of groceries in this talk and here we have a grocery list with some fruits reading healthy and a basket and ultimately the goal is to get all the things in the list on the list in the basket and we even do a little check to make sure it's the things that we want to put in the basket are not already there because we want one of every item now if we execute this on one single threat it's probably going to go okay just get everything in the basket and be very happy but what happens if we execute these two treads instead of one and I did a little drawing here instead of threads we have people and the people share the grocery list so they both check on the same list and they both put the items in the basket using the function you just saw before how would that go one possible scenario is the first person checks for Oh is there an app on the basket nope it's empty and then goes and get the Apple puts Apple in the basket at which point the second person who's a little slow to start looks at the basket ins like is there an apple in there yes there's an apple in there so it does absolutely nothing goes to the next item then both check the list is there an orange no there's no orange both people run through the supermarket getting the orange putting orange in the basket at which point there's two oranges in the basket that's a race condition and this is something that this sounds a little ridiculous with your supermarket and your groceries but this is something that actually happens and actually happens quite a lot and it's a big problem because if I run this program again instead of two oranges I may end up with two cherries or I may end up with two of everything or just one of each making it incredibly unpredictable and making this a very hard buck to reproduce if at all so finding this buck is hard it's going to take you time and this is one of those times where you put the luck statement in your coat before you run it and then all of the sudden it works whereas before it didn't just because you slowed down the execution a little bit those are base conditions and they are relevant today more so than ever before because we now have computers that don't have just one core they don't have two cores they have many multiple cores your phone has more than one core which means that we're going to execute programs in parallel so we're going to break them down a little pieces and execute all these pieces on different threads or processes and that makes a problem like a race condition an actual problem we're dealing with today this is also why it's getting so much attention today and but it's not the only problem with state and other problems you can have with state is that it can make things incredibly complex now we only had a grocery list and we only had to check the basket but what if you don't just have a basket what if it also depends on your mood that day or whether the supermarket is the right kind of supermarket or all these different states that the world can have and the more stage you have in your program the more checks you need to make for whether it's in the right state so you'll end up with this hugely complex thing that even a very talented human being will find mind-boggling and will end up making at least one mistake at one point so that's complexity I'm not saying functional programming is not hard it's just not this complex in this specific way and then we have a last one unpredictability and you saw a little bit of that in race conditions but in this case I'm more referring to the unpredictability of functions and I did my very best to create the most unpredictable function in the world which is a very simple one as well it basically takes the external variable X takes its value multiplies it by two and then change into the state of X as well so it's like their state everywhere and the first time you print that you get to the second time you print the exact same thing you get for just by looking at that function you have no idea what it will return and this is unpredictable and this is something you don't really want in your program you cannot just look at your code and know what it's going to do you have to check some other part of your code first so these are some of the problems with State and this explains why writing code that has the least possible amount of safe is very often a good idea but again if you're coming from a purely function period of world you may still be wondering yeah but but changing the state of a variable I do that how do I write something without that so I wanted to give a couple of examples and remember our door it was very simple right it could be open or closed the end state was that it was closed so I start with false and then I want to open it and I change the state to true now let's say we cannot actually change the state of this property what would we do in this case I created a struct and immutable struct that is door I gave it an ID just for fun and false because it's closed then there's a function and that function opens the door it takes the door as an argument and then from that door gets the current state and ID and returns a new object that is the ID of the door and true because right now we want it to be open so we have a new door this is crucial we did not change the actual door object created before this is it the functional way of writing the same thing I know it looks a little bit more complicated than the first one but bear with me now remember that very unpredictable function from before there's quite a simple way to make that incredibly predictable and that is by giving X as an argument to the function and returning x times 2 in this case you can look at the function and you know that for a given X it will always return the same thing which brings me to the two kind of defining characteristics of a functional program and a functional program which is as stateless as possible will have immutability variables objects will not be mutable and it will be predictable meaning that for any given input of a function it will be Darren that same output so like we saw before if if your input X is 10 they will always return 20 it doesn't depend on some external state so these really of you if you think like what is functional programming all about and ultimately it is this now let me go back to the race condition and see what went wrong and how we can fix that in a more functional way what went wrong is that both treads shared the same state and they were mouths manipulating the state same states namely the state of the basket now we could quite easily solve this problem by each giving them half of the grocery list their own basket they will go out in the supermarket do their own shopping at the end of the day come back with you filled baskets and then simply put those baskets together that's a very very simple way to solve this problem and in doing so we still haven't made the entire code functional right there still the function internally are still non-functional so just as a fun exercise I wanted to go through them and see how we can go from imperative to functional code or functional functions to recap a little bit what the what the problem was all about we had the groceries in the basket we had to get groceries function and just to make it a little bit more fun I did I added a buy groceries function and because we don't really have a price the price will be the number of characters in every string in the grocery list I'm pretty sure your local supermarket will not agree with this but so we have to buy groceries and we will just add up all those characters and then we'll have a total sum and that knowing that sum equals buy and then finally so this is how we then call the functions one after another in a very stateful world how to make this functional let's take the first one in the imperative solution we use a for loop in a functional function one way to avoid using a for loop is by using a recursive function in a recursive function means that if a recursive function is a function that calls itself so basically what we do here is we take we have a function that takes the grocery list and a basket checks if they're still groceries on the list if there is no groceries the list we will just give you the basket because we're done we're done shopping if there are still some groceries on the list we will create a new basket which is the old basket plus the first item on the grocery list and we will chip that item away from the grocery list and give both things as arguments back to the function and we will repeat it and repeat that and repeat that until once again there are no more groceries left in which case we are done and if you've never seen a recursive function before this looks a little tricky so look look over it at the wrong time but eventually what this does is it does never change the state of a variable it just gives it back as an argument and recreates the safety another way to do this not a way to replace a for loop and this is why I added a function is by using a map and this is one of those things where pythons really awesome because it comes with map built-in and what we do here is we create another function which is by item and as you may remember buying an item basically just means calculating the sum of the characters in the string and and that's it so that's all this function does and then buying all the groceries means summing up all of that so what we do here is we have a map that maps over all the items we have in our basket the basket was an argument to the function and so we get this basket we map over all the items in the basket and for every item we calculate so the number of characters right and then once we do that for that item we put it into a new list and then map returns a new list with all these sums of characters so instead of manipulating them or changing the basket we create a whole new list again we're not changing any states ultimately we also do a sum of that list because we want to get the total I think that one is pretty straightforward and so these are the functions and then the way we can call all of that is by doing if nested so we have the get groceries function that takes as an argument the grocery list as well as an empty basket and what it returns is a full basket and that will then be the argument for your bike rosary's function which will return the total of all the characters of all the strings in the grocery list or in this case in the basket which ends up at 37 which is like whatever and so this are your functional groceries this is how you do it the functional way and one thing when you look at this you can note that because all these functions are self-contained because they don't rely on some external state it becomes much easier to just give them to different threads and execute them separately whereas in an imperative way of doing this with where you have some external states that becomes much harder and well as we solve race conditions are much more likely to happen so this was functional programming 101 like 100 or 1 and if anything I hope you got from this that functional programming is all about statelessness it's not completely stateless but it's almost completely stateless and that is the hard part if you're coming from imperative programming this is what makes functional programming hard it's not all those words I mentioned before it's not Mona's or lazy evaluation it's the fact that it doesn't have state because your mind is so used to using state everywhere in your program that figuring out how to save the same problem the problems you've been solving for so long in a completely different way is is the thing that makes it so hard and yes then there are all these other concepts and language features like monads but they come they exist because in a stateless world they can exist and they help you do things in the stateless way they are not however what functional programming is all about one other thing you can do this in Python like I did today and I really wanted to show you that it's not about the language you use it's not it's a little bit but not completely about the language you use and it's not about the big words however if you really are excited about functional programming and you want to learn functional programming I would encourage you to use a functional language like Haskell or closure because these languages will force you to actually write stateless code whereas Python I mean anyone who's ever written code with Python probably knows that you don't have to it's a choice it's really nice to be forced also these languages are optimized for doing things a functional way which can also be very very helpful um that was my talk thank you you
Info
Channel: OpenTechSchool
Views: 28,695
Rating: undefined out of 5
Keywords: dortmund, opentechschool, ots, community, anoukruhaak, otsconf2015, conference, open source, education, Functional Programming (Programming Language Paradigm), open, otsconf15, otsconf, tech, conf, technology, 2015
Id: 6f5dt923FmQ
Channel Id: undefined
Length: 19min 3sec (1143 seconds)
Published: Thu Oct 08 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.