Let's Get Functional With Elixir!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now we're gonna talk about functionalism so for the last I don't know seven or so years I've tried to then I tried to learn functional programming so since about 2009 2010 was the first time I'd heard of functional programming I heard about it from people that I respected read blogs from people I respected saw it on Twitter taught us all these really smart people talking about functional programming and I thought that sounds cool I should go learn that and at the time the function programming language everyone was talking about was Haskell and so I said okay I will go get Haskell I'll get it running on my machine I'll I'll use the book learn new Haskell for a greater good and I'll go through Haskell and all I'll learn Haskell and it got to the point where I was even like kind of in my delusions of grandeur thinking like I bet I can find a job in doing Haskell because this like looks like fun I don't even really write any code yet but I'm already like shopping for Haskell jobs and so as I'm going down this path trying to learn Haskell I realized that like every time I would try to learn this I'd run into something that just was a block for me and so I put it off on the shelf and I would go do my normal thing and then I come back to it you know six months later and I try again so I tried this no less than three separate times trying to learn Haskell and I always got to a point where I just ran into a roadblock I just couldn't do anything else and so I convinced myself that I wasn't smart enough to learn functional programming I've been doing object oriented for 10 or 12 years at this point I was decent at it I had a good job doing it I enjoyed function object oriented and so I'd convinced myself that okay I'll just be a nolo programmer for the rest of my life and that's okay that's not bad I'm making really cool software but I'm just not one of the smart people that's smart enough to do functional programming but over that time I kept reading Twitter and I kept reading blogs and I kept seeing people that you know are kind of the elders of our community of the development community folks like Uncle Bob and some of these other people they've been around a long time talking about how functional may play an important part in the age of the Internet and in the age of multi-core CPUs and all this kind of stuff and I thought okay um you know 30 35 at this time and I've got 30 or so 35 years before I'm planning on not working anymore and maybe I should check into this because this apparently is going to be a fad and so I came back to it again and again and again and I kept trying Haskell when I kept coming back to the idea that I wasn't smart enough and so one day I have some friends that are in Kansas that are net developers that went to Ruby developers and they were tweeting out different things and they're talking about functional programming and they talked about this program called for this language called elixir and so I looked at it at the time there was a functional programming language was like oh it's a new thing I like new things let me go check it out and so as I started looking at it I realized that this is really a functional programming language for everyone it's where I felt dumb and incapable of writing functional programming because of my not getting Haskell I never had that experience with a lick sir and I started understanding actually what are some of the functional concepts and what are some things that make functional programming functional programming and so I really realized that elixir is functional programming for everyone that's going to be the kind of the tone of the talk for today is that functional programming if you've struggled with it if you've ever thought I'm not smart enough I'm not one of those guys I'm not one of those developers that can really pick this up I would encourage you to check out elixir one of the reasons that it's a couple reasons that it's functional programming for everyone is it came out of the Ruby community and so I don't know how many do we have any Ruby developers here on a couple okay so one of the things that people always talk about Ruby and I've done this much Ruby I wrote a Sinatra API that had like two endpoints but one of the things that people always talk about Ruby is how much Ruby developers enjoy writing Ruby and how friendly the syntax is right and I can totally get that because I once tried to teach my son how to write a console app and c-sharp and I had to tell him what strings were I'd show him what integers were I had to make sure he was in yes semicolon on every single line right and he was just wanting to like write this little flash card game that would ask him a number and he'd give it the answer and like yeah before you do that we got to do all this boilerplate so I totally get it right it's an enjoyable experience that's one of the main mantras of Ruby and so elixir was created by a man named Jose Valley and he was a ruby developer for a long time and so the things that are good in Ruby he wants to bring over to elixir and I've heard him talk he never really says these were the bad things it's just here's some things I wish I could do that I could never do so he's not he's not ripping on on Ruby or anything like that it's just here's the in his mind the next iteration additionally it's a new language and that always scares lots of people because how are we going to trust this thing that's only five years old but it's built on top of Erlang and Earling is a 30 year old programming language it has its own VM like it's called the beam kind of like JVM for Java and so elixir runs on the beam and what's good about that is Erlang has been around for thirty years so you know it's tried and trued and tested and all that and Erlang has been powering telecoms for thirty years it was written by Ericsson in Sweden specifically for their Telecom functions and so when you think of distributed networks the internet comes to mind and pre-internet days distributed networks where phone networks right because you had to you had to make all the jumps and switches and all that stuff that was all powered by Earling and so they figured out how do we do distributed computing how do we do high availability how do we deploy without ever having downtime and when you think about it when you remember your landlines and even even today your cell phone LTE and 3G there's some early in our encoder landline or sent you a letter that said hey Sunday the the 1st of the month at midnight we're going to do some maintenance so I need you to stay off the phone for a couple hours and then you can come back right that doesn't happen with Erlang because you can't have that on the phones there's no way to manage that it's outside your control but I've worked at companies where our main product we sent our customers notices every three weeks and say hey this every third Thursday we're shutting down for two hours so we can deploy and so Erlang gives some of that flexibility some of that power to elixir and that's another reason they've already solved some of these hard problems that's another reason that Erlang or I'm sorry elixir becomes functional programming for everyone before we get too far into what elixir is and the syntax and all that good fun stuff I want to I want to set the stage that we're all at the same understanding of functional programming and by same understanding I mean my understanding you guys can have your own that's fine but in the next hour we're going to operate off of mine and then you come up and tell me why I'm wrong and that's that's cool so first of all there's three things we need to know about functional programming functional programming is a declarative paradigm and really that sounds I don't have a computer science background of a electril engineering background but when I hear things like big o-notation and all these people talking computer science terms my eyes glaze over and I tune it out because I just just does whatever just tell me tell me make it very pragmatic for me declarative paradigm is one of those things for me and so throughout this talk I'm going to try and break it down into my level and hopefully hopefully I'll help you guys so declarative programming really means what versus how instead of sitting there focusing on how to accomplish this with my code you're trying to tell the code here's what I want at the end of the day and you go do this for me you go get it for me and I don't really care how you do it as long as the answer is right here's a great example most of us have written for loops is anyone not written for loop alright that's that that's always right I'm not a while but we know them right there burned in there probably bad memories because all of us have had the index array overflow kind of thing well but here's our normal for loop and we're just going through a list of users we're checking in his active flag oh actually I'm coloring we're going through a list of users we're checking our is active flag and if it's active we're adding it to a new list that will then return so we're just going through and we're building up here's our one list of users and we want to list up active users at the end and so this this would be an imperative thing because you're telling it start at index zero keep going by one until you get to the end so go 0 1 2 3 4 5 until you get to the end check this flag and then shove this thing in here and then the codes not there but then return that thing for the developer so that's an imperative paradigm that's how most of us think because that's how most of us have been taught to program that's our intro to computer programming 101 in college or that's the first book of C or C++ or Java or.net one of the first things they do is like let's show you after hello world is let's show you for loops right let's show you how you consider eight over data conversely though this is a declarative paradigm and we haven't gotten into the elixir syntax yet which this is and we'll get there so it's a little strange but we can walk through it and even without the syntax we can probably figure this out right we have an enum that is a module and we're saying here's a bunch of stuff we're going to operate on enumerables right so it said anyway we're going to filter our enumerables and when we start thinking of filtering we think we're going to take something and then the end is going to be smaller than the beginning we pass it a list of users and we say return me everything that this flag is true we don't tell it how to iterate over it it can go from last to first it can go from first to last it can skip count by 5 as long as it covers every single one we really don't care what we care about is that we have a list of users that are active at the end right we're also not telling it to shove these things into a new list we're just simply saying at the end of this function call I want a list of active users you determine how to do that because I don't really care how I care that I have a list of active users so that's what declarative programming means what declarative paradigms are and we'll see that more as we go through the rest of the of the talk the other another major point of functional programming is functions are first class citizens and it's always strange when you think about functional programming because you think of well C++ has functions and C has functions and c-sharp has functions and Java has functions and MATLAB has functions and all these program all these languages have functions right and so what makes functional programming so special well it's not that it has functions clearly because other languages do it's how they use them and so here's a good example on the previous where we had the filter we could define this as a function and we could just call our new file our new variable here is active we could just assign it this function and then we can pass that variable into our filter so there's actually a couple principles here of first class of the importance of functions one is is what's rightly called first-class functions and that's where you can assign function to a variable or you can pass it around the other thing is that this is a higher-order function and that's just a big fancy way of saying it can either accept functions as parameters or not either it can accept functions as parameters and or it can return a function as its return value so normally you'd return like a string or an int you could actually return another function so if you've had JavaScript of any kind of beyond like basic jQuery and maybe even some of the jQuery those functions will sometimes return functions right and that you can then operate on so that's what being a first-class function is the third thing is functional programming is immutable and immutable is another one of those big o-notation words for me but it basically means the data it doesn't change and so if we have our list of users here and we didn't I didn't specify any names or anything like that it's just we have one that's an active flag and one that's an inactive flag or one's active flag it's true and one's inactive active flag is false and we call our same Enuma is active and we get a new active users here it's actually this users still equals this thing and active users only equals this and so I think we have some net developers if you're familiar with link some of those functions will modify the array that you're working on and some of them will return new copies with functional programming they all are going to return new copies you're not going to ever modify the thing that you're working on javascript some of them some of the some of the es like the array prototype function some of them will modify the thing and some will return it and it gets confusing because you're like Oh was I supposed to catch this in a variable or was I supposed to just know that it's going to modify the one I was working on what I want to do when I write elixir is I want to do something like this where I have my users list and I call a filter function on it and I pass in a flag but that's that's not how this works what we've already seen it's going to work this way and the reason why I actually heard some of the the guys from the core team of elixir talk about this is they do it this way because they want you to focus on the fact that EMU is the thing that's doing the work it's the inu module you're passing data to the inu module you're not having functions hanging off of your Struck's and by by forcing that in your brain it kind of helps reinforce that like it's going to be a new thing it's going to be a new copy of the thing that you're working with so the three main points that we're going to kind of look at functional programming through this lens or declarative programming functions the first-class citizens and as mutability but those are all in my mind big o-notation words that I don't like so what I would rather do is I would rather break them down and say it's what not how functions have the power and data doesn't change so pick one of those two that makes sense to you and we can operate with that if you're if you like big o-notation words and then you're set and if you're like me we can we can work with what not how at the end of the day elixir is going to make functional programming accessible so it's going to take those things and it's going to basically do what I just did on that slide it's going to take the big o-notation words and it's going to make them you know small low notation words will say or NATO tation words or something like that where it's much more accessible so the first thing we're going to look at we're going to look at some syntax verses and we'll wind up here at the end with a demo to show some of the power the first thing we're going to do is look at this pipe operator so we have this code here on the screen anyone want to take a guess at what this code is doing it's kind of ugly in my opinion no one's jumping out right it's not immediately you want to take a shot yeah go for 300 why Yeah right right so it's a little unclear right nobody was like oh instantly I know what that does right so if we look at what this function does or this block of code does it reads the file right it reads the tweet text file it then splits the file on new lines so that you could make sure that each thing is a is its own string in an array or list and as its called an elixir it's then going to trim all the extra whitespace because why do we want to tweet out whitespace and then it's going to only return you strings that are tweetable in this case less than 140 characters and so that's what this code does but it's kind of ugly because you have to start all the way in the middle to kind of end and work out instead of going top-down so what the pipe operator lets us do actually is go pipe down so this is the exact same code the exact same code I copied this code into the interactive elixir terminal or shell and and made it the other code but you can see now as you look at it if I were to ask this you guys be like okay it's going to read a file it's going to split it on a new line it's going to map it's going to it's going to perform a trim and it's going to filter right and so you're starting to look at it you're like these would be likely would be business requirements you know your your BA comes to you and they say Nate we need we want to send out random tweets throughout the day so we've got this file that has all the tweets we want to send out and so we want you to send them out we don't know we try to keep them under 140 characters but you know sometimes we don't and we don't the last thing we want is our tweeter or Twitter app to be stuck because it's trying to tweet out something 141 characters and the API is like sorry I can't let you write so your your VA comes to your your product or comes to you and they tell you these things and now your code kind of matches that right and it's you're focusing now on on the on the what you want to accomplish I want to send out tweets from this file out to Twitter the the Twitter sphere which is a lot harder for me to say than I thought it would be we'll look at another example where you you can actually debug with the pipe operator so we have our list here which is a list of lists and we want to get this value we don't want to necessarily get to we just wanna get the first value in the second list and we don't know what that's always going to be and so we would do maybe something like this where we take my list we pipe it into the inu and then we pipe it into that results into the other enum and I guess I screwed up already I should have told you what the pipe operator actually does so the pipe operator besides making your code more readable it will take the parameter above and it would inject it as the first parameter of the function call so II knew at the actual definition is Inu mat list index and the pipe operator is going to say okay I have my list I'm holding onto that I'm going to pipe that into Inu map I'm going to I'm going to wedge that in as the first parameter and then the second parameter will be the index and so then that will get us this thing right here and then I'm going to take that thing and I'm going to shove it in here and I'm going to the first one and that's going to get me the two and when you have this you know that's small you can maybe conceptualize what's going on as you working on the Twitter one or something more as more pipes in it it can become confusing like what's the state of my code what's my state of my data as I'm going through this pipe I know when I put it in its right I know when it comes out it's wrong and I don't know where it breaks down so elixir lets you do this simply with the i/o inspect and i/o inspect is kind of like console.log and what it does is it takes a value it spits it out to the console and then it returns that value so it's like a no op it basically doesn't change the data at all it doesn't do anything to the data it just spits it out and it returns that value for you so as you were going through this if you were to run this code you would see two comma three right here on your console and you go okay I know I've got the right one so this isn't where the bug is you know and I'll put some more i/o inspection so having this pipe lets you kind of chain things and make it more readable but it also gives you that flexibility of doing some debugging one of the things I hate is when you get something that's more readable to you but then it's a pain to debug right the a lot of the debuggers and and browsers are now getting better at like multi-column breakpoints but back in the day when they didn't have those it was a real pain to say like var and then like a list of like ten variables with assignments because you didn't know what was breaking right it broke somewhere on that line and as far as that was concerned as one line well things like this give you the flexibility then to see the actual state of the application the state of the data as you're going through so we're also then focusing on tweeting where we're getting a tweetable string right that's what we're focusing on as we as developers we're no longer focusing on how do I get the sweet Abel string other than the business rules right 140 characters no whitespace that kind of thing but we're not focusing on oh shoot did I do a zero index or a 1 index what are these kind of things so that we're just focusing on on the what not the how we're also focusing on function composition because we can wrap this whole thing as a function and now we have a function that we can pass around that does this and so we with this one piece of code we are now have a get contents that we give it the path and we can use that anywhere we want to so we could then use this get contents in another pipe that has a pipe that has a price that has a pipe and you can just keep passing these things around because you're able to compose your application then of smaller smaller and smaller functions until they make sense to you and make it readable so that's another advantage you're you're not only getting the declarative of the what not how you're getting the functions have the power because you're starting to compose your application with these functions so that's the first piece of syntax the second piece of syntax is pattern matching and when I the reason I picked these two is people learn start to learn elixir and you do a survey of people learning a lick sir and you're like hey what's drawing you do a lick sir would you like the two things there's two things that are always in the list and there's a bunch of things that are sometimes in the list the two things are always in the list as the pipe operator and pattern matching and it's one of those things that once I got pattern matching once I understood pipe piping functions actually sat at my desk or set up my laptop writing code and smiled while writing it and and as much as I like JavaScript which I do and as much as I like c-sharp I don't think I ever have smiled writing them so it's a it's an enjoyable experience for me to write a lick sir and this is one of the reasons so we're going to look at some examples of pattern matching the first thing is at the top here we have first comma last equals nate comma taylor and these are the things on the left and the right are two poles this is how you would write a tuple in elixir and a tuple is just a data structure I struggle really hard to not say objects so I might say objects in this talk there's no objects in elixir but that's my c-sharp and C and C++ states there are two data structures or there's a same data structure that's kind of loosely connected data and there's actually not an upper limit on the number of things you can have in a tuple so C sharp has a two or three value tuple I think that in in recent versions they might have more in a lecture it's for all intents and purposes unlimited for performance reasons it is you're better off using other data structures once you get past out four items but they're very common in elixir and we'll see that as we go through but it's just two pieces of data and so as I pattern match here my if I were to console.log out first it would show Nate and in last it would show Taylor because it says okay first is assign to whatever the first value of the tuple is and last is assign to whatever the last value the tuple is and so now I have those just by using the equals sign we can get a little bit cooler with that a little bit neater we can actually say first comma Taylor equals Nate Taylor and it will only match if the second value is Taylor so here if this is not Taylor this is an urban sorry this isn't Taylor this isn't going to match and it would actually look something like this where if we were to pass in something like a Nate Diaz it would give us an error that says there's no there's no right-hand value this right-hand value doesn't match anything with Diaz because it's expecting a Taylor so now you can start pattern matching on expected data like I don't really care what the first value is or maybe I only care what the first value is if the second value is this thing right and so now I can start pattern matching on that and I know that like okay when the second value equals the thing I'm interested in now I have the data and seeing start thinking about this if you were to think of like loops one of the great examples for me I was helping a developer recently debug some JavaScript in the in the browser he put his breakpoint on on a line and he was wanting some value to be like three and so you he was hit the breakpoint and he would inspect it oh it's not three and he hit it again it's not three and he'd hit it again it's not three you did it again and finally was able to stop him like he was hitting that key really fast I finally got to stop was like hold on let me show you the debugging trip of trick of like you can edit the breakpoint so it only stops when it finally equals three that's kind of what you have here you can be like I don't care we've got this file that I'm processing and I don't care about any of the data until this one value equals the thing that I'm looking for and then I care about this data and so you can start you can start using pattern matching that way and we'll actually see some examples as we continue on so that's like the super basics of pattern matching JavaScript in es2015 has destructuring which is what this is as well you're passing in a struct and you're getting values assigned to it so you could do this in JavaScript I don't believe that JavaScript has pattern matching so I don't think you could do this but it might but this is something that's in other languages too but it is definitely something that isn't a it is common in a function a programming language and it's common in elixir and it's one of those things that people really like so we can that's pattern matching just in general right so now let's look at some use cases why would we use pattern matching when would we use pattern matching so the first one would be on return values we have something like this where we're reading a file and we have a case statement and we have two things here it's the file so the file read is going to return a tuple in a lick sir a lot of things return to polls in elixir for this very reason because they can say the first value is okay and or error so either I'm giving you a thumbs up this thing operated as you would expect or it did not operate as you would expect and so in this case we're saying file read when it performs correctly and the file reads like maybe the files there and it opens it up and it gets you everything it's going to stick the contents in here and so you can just put that out maybe you specified a file like an earlier example we had tweets text maybe it was called Twitter text and that doesn't exist it would give you this error and it would hit this line and it would say oh I read returned an error you need to handle that and so you can now operate on this code in a way without doing a bunch of try caches you don't have to do a bunch of return value checks because you're letting the language do that for you right so anyone did anyone do like MFC C++ back in the day okay Kapil fueler nodding the thing that sucked about MFC C++ are one of the things was you never got the value you wanted on the return it was always an H wind it was a handle to the window reference and the value you wanted was an out parameter not always but a lot of the time and so what you'd have to do is you'd get this out value and you'd have to check the H wind and you'd have to make sure that H wind was okay and then if it was then you could go fetch the out value and you could do whatever you wanted with it so elixir makes that really simple you don't have to do any of the checking you just say if it's okay do this thing if it's an error do this thing and with file read those are your only two options it's either only ever going to be okay or error but as you write other functions you could you could imagine that you could have other values right like I'm struggling to think of one right now but you could have other values where you're catching the function return value and your pattern matching on it you can also pattern max trucks and sews trucks are the data structure almost a data object structure the data object is data structure of elixir and they would look something like this where you could pass in a date and here we see another tuple right and so it's day I'm sorry month day year and actually this was just a date one date time would be a tuple of tuples where you'd have day month year and then our minute second as a second tuple and so if we were to have like a special event function and we're like hey what's today you know maybe it's our our internet we want to say Happy New Year's or today's pay day or something like that when you log in or you want to write a slack bot or something like that you would have a bunch of different special events and so the way this works is we have 10 31 1517 was when Luther nails his theses to the the door in Wittenberg 10 31 all the other years of history is Halloween 10 12 is my birthday my Twitter handles down here if you want to send me gift cards that'd be great one one is New Year's we don't care what month it is or what year it is but one is always the first of the month because that's how our calendar works we don't care what month or what year I'm sorry what month or what day but 2000 was y2k and then if we don't match anything it's boring and so what would happen then is you would pass in a date and it would it would start at the top and it would go until it found something that it matched and these underscores mean there's going to be a value here but I don't care what it is that's another reason personally I love a lecture because I have opinions about software but I hate arguments about software and so a lot of times I'm just like I don't care just go do the thing right that's kind of what this underscore means like I don't really care just do the thing right just just fine whatever you're right go and so kinda what the underscore is like I don't care what year it is it's Halloween it's 10:31 I don't care what what year it is it's New Year's let's go watch the Rose Bowl or the parade or something like that and so here you can start pattern matching with this value and you can easily break these things down and it's a lot to me anyway it looks a lot better than having if else if else if else if else and it just is like yeah let's find the one that matches so that's pattern matching Struck's we can pattern match function parameters so we have something like this where we're going to write a function that's going to talk about languages so we call it is dynamic and we've seen this before where there's a colon and then a thing we saw it with okay we see it here with false and true and unknown these are called atoms and elixir in Ruby they're called symbols so it's the same thing the same concept as a symbol in Ruby they're constants where the value of the constant is the thing so the value of the atom C is the letter C the value of elixir Adam is the string elixir excuse me it's very problem in in in elixir too just to find these and use them to pass around to kind of let you know different things so we can pattern match on this in our function we actually have the same function to find three separate times right here I could have just left that as underscore but I wanted it to be a little bit more expressive for the people reading my code so I want to say oh it's some other language so it's not a very good function it only knows about seeing elixir and there's lots of other languages out there right but we can walk through how this would work so we're going to use our is dynamic and we have is dynamic as JavaScript dynamic well what it's going to do is it's going to go in and it's going to say well elixir doesn't match JavaScript so I can't I can't handle that one and so it's going to go to the next one and it's going to say oh I'm C I can't handle JavaScript I don't match so I can't I can't handle that and so it's going to fall down to the third one where it says is dynamic literally anything else and it's going to tell you I don't know because this program is incomplete or is very bad one of the two so if you did it's dynamic see what would happen is it would say okay it's dynamic C doesn't match elixir so I'm out again but it's dynamic does match C so I'm going to go there and I'm going to stop and it's going to be false it's not going to hit this bottom one because it's reached the matte it's matched a function and it's going to operate that function and it's just going to be done and so the order does actually matter because if you're going to essentially as we'll see here in a minute with recursion the order matters because it's going to start at the top of where you define it and it's going to operate those in those in that order and so if you get them out of whack you could actually short-circuit your your your loop that you think you're operating on until we actually I mentioned recursion nobody ran out of the room crying yet so that's good imperative programming a lot of times recursion is scary I've been programming professionally for 17 years this month I think actually 17 years next week maybe and then as a hobby for you know 30 years or something like that and I hate recursion in in like C and C++ and C sharp because it's it's always like I'm always trying to keep track of that state right and it's always inside inside some kind of for loop so recursion is a thing that I find when you start talking about it people's eyes glaze over and they want to leave the room but it's a small room and they're embarrassed and so they won't but that's okay elixir is going to make a recursion nice and easy for us or easier so we're going to take this code and we want to reverse a list so we have 1 2 3 4 we want 4 3 2 1 now this is what the code looks like so we have our reverse list and we haven't really looked at functions up until now so we'll pause here for a minute and talk about functions functions are defined with this def macro and as do in macro so almost all the keywords and elixir aren't keywords they're almost all macros which allows you to beyond the scope of this talk but it allows you to write a lot of meta programming in a lick sir elixir itself has a lot of macros in the in the language itself but you define this as a def you're defining a function and you there's no curly braces there's no semicolons it's simply do an int and these are public functions if we wanted a private function you would used FP to indicate private also notice there's no return statement it's simply the last statement the last executed statement is what's returned this will screw you up when you start doing a lick sir because you'll start typing return statements and it'll screw you up after doing an elixir session and going back to JavaScript where you don't put the return statement and you wonder why your function didn't do anything and you spend like 10 or 20 minutes trying to chase that down you go oh there's no return statement not that I've done that or anything but that will happen so what this does is it says okay if the only parameter I have is a list then call this one with an empty list so that the font doesn't translate great on the screen but that's just two empty brackets so then it's going to say okay well here I've got a list and an empty list while I skipped one so this would only hit if the if the list you're passing in is empty and it's going to do reversed it's going to just return whatever this value is this one is going to match and so we actually see a couple things here it matches because it's got a one list and two an empty list but even the function meters are matching as well head and tail so in functional programming a list is nothing but the first value and the rest of the values so that's the head is the first value and the the rest of the IDS are the tail so if we were to think of my name is a list Nate Taylor Nate that would be the list in would be the head a te would be the tail and so this is just saying I want access to to the first item in that list and I want to I'll pass in the rest and so then it recursively calls itself and so we don't want to get into too much of the details but basically it just takes the tail as the new list here and it pins the head to whatever you've got so the first time will actually walk through this we have one two three is what we're trying to reverse right so we start we match because we only have one item it's the only function we can match because all the others require two parameters so we match the first one and we do whatever it says to do well what it says to do is call call reverse with your list and give it an empty list so we go back through the the cycle again because we're using the exact same function name so now we have reverse one two three with an empty list and so it no longer is going to hit reverse because we now have two parameters instead of one I'm sorry that's back it's no longer to hit reverse they're all reverse it's no longer going to hit this reverse because we have two parameters instead of one it's also not going to hit this one because our list isn't empty there's stuff in there so it's not anything that it doesn't hit that it does hit this one because there's stuff in our list and there's an empty list so we reversed right now is our empty list head is 1 and 2 3 is the tail so what reverse is going to do is it's going to call reverse with head 2 comma 3 and an empty list which is the same as as then going to comma 3 and then appending 1 to an empty list so that comes back up and it looks like this we now have 2 comma 3 as our list and we have one in our item so again it's not going to hit that one because we still have two parameters again it's not going to hit this one because our first parameter is not an empty list so again it hits this one and it does the exact same thing now it breaks out two and three into head and tail and it knows that our reverse list is 1 and it says okay well tail is 3 so I'll call myself one more time and I'll append two to my existing list so now the third time or four times through we've got three and two , one still doesn't hit the first one because we have multiple parameters still doesn't hit the second one because we don't have an empty list still hits the third one because we have a list in another list and so what it's going to do is it says okay heads this time is three and I don't have a tail and my reverse list is two and one so it's now going to call reverse with an empty list 3 comma 2 comma 1 so that's what it looks like so still not going to hit the first one right because it's still 2 parameters but it now can hit this one because it does match on the empty list and so now we're now we're at the terminal function right this is the last one we know we know there's nothing else to reverse and so if you look at the code it said just when you get to this one just return whatever this thing is and so it just says 3 2 1 and so reverse is a simple example with maps and lists I'm sorry with lists but it's I think it's a great way to look at it because because you're not you're not in my mind the struggle I always had with recursion was recursion functions were never small recursion functions were always big I don't know why I don't know if there's a rule that says recursion functions have to be 50 lines long nested in 4 loops but that's how it always worked in my code and in code I worked with but here we just said we had what a total of five lines of code that did a reverse and we were able to just say okay when you hit this this is a function and when you hit this this is a function when you hit this this is a function and for me one of the reasons that it helps simplify recursion or make it accessible it's because I get a look at those independent functions or a look at those functions as independent functions so I think okay what happens when they pass in this data I'm going to do this thing and that's all I care about and now I'm gonna write the next step okay what happens when they pass in this data I'm going to do this thing and that's all I care about right I don't have to try and keep the state in my head all the time I'm just like okay well when this thing happens I know what to do when I have nothing left in my list let's return the list so elixir is making recursion more accessible alright so here's the demo and this is see if this works not the demo the demo should work although my slide should have worked earlier too so we'll see I want to break this out because I want activity monitor and I want to view on a window CPU usage I want to view frequency very often every second perfect so this is a great room for this because although I don't have to ask if anyone can see that in the back because you guys are like 6 feet away so IEX which you see up here in the upper left-hand corner stands for interactive elixir and so it's a ruffle you can come in and and I can do you know something like one I'll have to be in the terminal after the 1+1 and it's going to do the math for me and I could write any kind of elixir function so this is great when you're first starting to play with a lick sir because you there's the syntax kind of weird and you're trying to experiment with some things and so when I write JavaScript sometimes I'll just open up a browser and just open up console.log and just start playing with things to kind of see I don't have to do that anymore vs code now has some support for me where I use console in the browser but that's kind of the same thing here you have a sandbox a playground that you can play with you can also run your applications in interactive mode which is what I've got going on right now and so this allows it to compile my application and I can now make individual function calls into my application and say what happens if I call this what happens if I call that so it's exactly what we're going to do I've got a function here that's the one I want so I've got a function here it's we're calling this get functional flow I've got a couple functions I've got benchmark which is going to tell me how long this thing took to run and then I've got this function and just ignore this in text for now we'll get back to that in just a minute but what this application does is it takes two copies can cat two concatenated copies of the King James Bible and I picked that one because it's a big text and it's free and so no one's ever going to complain to me about copywriting and they're all in this one giant text file that's a couple you know a few dozen KS big and and it's a big enough file that kind of shows what I want to show what I want to do is I want to get the word count of every word in that text and so the code here is going to open the file it's going to kind of do some of the same things we did with the Twitter one going to open the file it's going to trim the words it's going to split on spaces it's going to split on new lines it's going to lowercase all the words because we don't care if capital a and and lowercase and that we don't care that they're two separate words we just wanna know how many times they occurred it's going to then sum up each word and it's going to return that as a list sort of descending Li it's going to then so we can do this really quickly me I'm going to take off the benchmarks so we can just see the thing happen and we can see what the data is going to be out benchmark will tell us how fast it's running and that's that's the point of the demo to take a few seconds here and by a few I mean like 15 or so seeing see there's a hundred twenty eight thousand or I'm sorry yeah hundred twenty eight thousand twenty-nine instances of the word thaw and the King James Bible there's actually half that because there's two copies right not surprising it's an English which it's an English word where work the top words are going to be thought and up to that little small things right so that's all this that's all this code does but it shows it's going to show some of the data processing so we're gonna go back to this one and what I want you to do is I want you to watch as we kick this off I want you to watch the CPUs over here so this is my macbook pro it's a 2012 I think it's got eight cores which so most of our laptops have had eight cores for a while and we can see while I'm doing nothing but presenting PowerPoint I don't need to have anything else open besides this okay I'm Tasha so there's there's like you know four of the cores are kind of trying to do something from time to time right the other four are pretty much dead and it makes me as a developer go why do I have four cores that I'm not using right so let's run this and we can see okay the four cores that are active you know they're kind of jumping around maybe maybe what a third maybe forty percent of a core gets used and they're bouncing around okay it took nine point eight seven seconds not not horrible to parse all of this I mean we're talking about thousands or millions of words in the text so it took nine point seven seconds but I'm pretty impatient as a person I recently I told you guy that started in C++ and and our build times there if you had to rebuild all was 45 minutes and that that that was painful but that meant that I could go get a coke and I could go catch up with other developers that were also in the middle of a rebuild right two months ago I was on I am on a JavaScript project that's using webpack and they made some changes and the web pack config took 30 seconds to run and I was ready to kill somebody I was like I used to deal with 45 minutes I'm dealing with 30 seconds why can't it be two seconds and they did fix it it's now instantaneously but I was like you know how spoiled am i that 30 seconds is too slow for me so we're going to do something different this time we notice what we're calling flow count instead of stream count but it's gonna do the exact same code I'm sorry gonna do the exact same function it's going to do the exact same thing it's going to spit out this data it's going to do it in it in a functional way that we'll look at here in a second so we're going to kick this off watch the CPUs man day I'll just instantly get pegged pretty much right and then it's done and I didn't all get paid they got pretty close most of them got paid and if you're like me you're probably thinking well what if I don't want them all page or if I'm doing other things with flow you can do that in a lick sir but the thing that the impact the powerful thing to me is for 17 years I've wanted to do things where I get to use the power of the computer and for at least the last let's say 12 years I have multi-core processors that are a pain in the butt to write multi-core code for any of the dotnet developers ever played with parallel link right any of you ever really got it to work okay one guy said not one guy yeah I have and it took me a week I think to get it to be tuned the right way this this was like a hello world example for flow for elixir like there is no real special code there was no real thought here from my perspective there was tons of thought from the elixir team and tons of thought from the core team because it's focusing on that declarative paradigm right we want to tell the computer what we want I want you to use all the available resources to count these words for me that's what I want I don't care how you do it that's up to you guys right that's up to you as language creators and it's the core team maintainer I want I want this and that's exactly how they they orchestrated it so we're going to go back and we'll walk through some of what this is doing I can get my mouse to work here okay so this was the function we called and we see get functional flow is the module name that's the the module that has benchmark it's the module that has stream count notice because we haven't called this out before haven't really seen modules before so modules are kind of like classes except for in an object-oriented land classes have data and functions modules will only have functions because because you're not passing around data but notice here that modules have a capital letter capital G our functions have lowercase letters and that that's not a that's not just a coding preference that is elixir will call you on that like I can't find this module if it's a lowercase one or I can't find this like you typed capital D benchmarking sorry I can't find the benchmark function I don't which talk more module I don't know if you're talking about so that is that is a standard that they use so we have benchmark as a function we have three count as a function the other thing then that we is probably a little bit more foreign you guys do you guys are smart people you would have figured that out in no time at all with the error messages and just kind of looking at the code you would be like a module function got it and you're still smart people what might you might not figure out is this is the ampersand and that's that's telling it I'm going to pass you in a reference to a function and you might have figured that out some other programming languages use the ampersand as a reference right so if you hate if you use those languages the other thing that I've never seen before and and so if you're smarter than me which is not a challenge then you might have seen this but I've never seen it before it's a slash zero and this is this is what's called arity in elixir and arity is another one of those well that's neat and updated here oh there we go okay I thought isn't update there for a second so energy is another one's Big O notation where it's let me let me define it's just simply the number of parameters a function takes so because we've already seen the pattern matching elixir is a dynamic compiled it's a dynamic language so it's not going to have the polymorphism that you're used to of like oh this is an this is an int and this is a string and that kind of stuff right and so what it does that it wants to know how many which of these are you going to call you're going to call the one has zero parameters you can call the one has one primary you have to call the one that has two parameters it also needs to know that because at times you might specify a function that has one parameter or you might call a function it takes two parameters and you specify one and the other one's going to be supplied later so it needs to know don't call the first one that only has one parameter called the second one that has to and I'll wait for that second parameter to show up when you give it to me so an era T is just simply the number of parameters a function takes so we had to tell it call the stream count slash zero even though the only function called stream count in this entire application takes no parameters and there's not another definition of it you still have to specify it so it does take it first is a little noisy after a while it's kind of like curly braces and C sharp you just tune them out and you don't realize it anymore for good or bad so let's talk about how this work elixir as I've mentioned is built on top of Erlang processes and I'm sorry I've mentioned is top on Erlang Erlang has this concept of processes and we all know what processes are because we have operating systems at processes that's not what Erlang processes are Erlang are very very lightweight the default size for an Earling process is about 1k so if you just spin up and don't do anything with it it's going to take about one case so you can have literally thousands of these processes in an OS process right and so the way the process works is we've got some code here and we've got some data here doesn't have to be a database it's just easy for us to say that that's the data size right this is a process and so this code can talk to this data and it knows what to do with this data and it's all contained inside this process this will not work these processes are not allowed to talk to this processes data which is not going to happen so that's one way that it enforces immutability it enforces to make sure that data doesn't change this guy's trying to work on his code and if this guy comes in and changes the code out from underneath them this guy's kind of hosed right because he's like oh I was doing something and now it just switched and that thing I wanted to do next is gone so in some times in those like in link and net if you do a for each and you don't actually have it over a list if it's over an enumerable and you try to make a change it blows up because you're trying to change the thing out from underneath it and it throws a runtime error so you would have something somewhere here but it's just not going to happen this you can't even get to this state in the lik search you can't have something that's trying to talk to the data because elixir and Erlang just won't even let you it's another one of those kind of declarative benefits right like you're not you don't have to worry about managing state in this way so you don't have to worry if you accidentally do this instead what happens is each of these have their own state their own dotted-line process and then these arrows indicate that they're passing messages so one can pass a message to two or three two can pass it back two or three confessors back to two or one they can talk and so what this has led some people to say is that elixir is the purest object-oriented programming language they've ever seen even though it's a functional programming language because we're getting back to Allen Kay's idea of object-oriented which is things that are packaging passing messages between them right this is called the actor model so if you go in after this you're you're blown away you leave Nebraska code after this because nothing could possibly better than stalk and you spend the whole day learning a lick sir you're going to see actor models you go what's that it's just this I'm a thing I'm going to pass messages to another thing and that thing knows what to do with my message this is this is the this is the the secret sauce so to speak of how elixir was able to distribute this across all the course so let's take a look then at the code so this would have been the first line of the King James Bible and we can see I put up enough so that we can have a repeating word because that's when it gets interesting so we have the word thought twice so the way flow worked was it took each of these and it's this isn't this was for illustrative purposes it probably did not I can almost guarantee it did not spin up a process per word in the Bible and the King James Bible but it's for illustrative purposes right but it's going to say okay and you go to this process though you go to this beginning so on and so on and so on right and then so now it's got them all split up it's done the string split it's done the string trim it's done the well hasn't done the lowercase because I didn't lowercase God there but it would of write it that's where we're at and so now it says okay now I need to sum these up and so it's okay in you go here though you go to process to beginning God created oh I've got the I've already got us somewhere let me go through the with the other does let me group all of the those together and in the flow code this is one keyword or one function call called partition and it's just telling it divvy up the data that's all I had to do is sell it partition it I didn't tell how to partition it I do have some control over that so if I wanted to control say the number of processes I could do that or if I want to control the backpressure like how much are you willing to spin up before you before you leave alleviate some of that pressure you have some of that control for the demo I didn't care so I just had partition and I'm done so then what happens is each of these say how many ins do I have I have one how many does right I have two how many beginnings and so on and so forth so you can start to think that this could very easily then be instead of processes you could these are self-contained things right so what's stopping these self-contained things from being put on multiple cores of the CPU nothing because there's nothing that is going to alter in only this process is going to alter in this guy over here can't do anything with the word in so they can start farming this out two CPUs and they can say I'm not even CPUs thing start firing about two cores and that's why we saw the spike of all eight cores just take off because they had this power of just okay I don't know I know no one's going to touch my data and as you think about it when we went 20 minutes ago 30 minutes ago when I was talking about earlier I said it controlled the telecom industry right and you start thinking about if you were if you were if I was in your audience until he told me that I'd be like well but I don't do distributed computing like I don't I write apps I might have six servers running my application if it's like a super powerful application but I won't do distributed computing but this lets me do distributed computing on my chip right so it's not truly geographically distributed is distributed across my chip and and that was the key the key point for me I heard an interview with Joe's a valium on a podcast one time and he said that was one of the reasons he chose Erlang was because he realized we all have distributed computers that we carry around in our laptops and if I can make those things easily distributable now I can start leveraging the power of the cores and now I can start making parallel linked super trivial because I can just say hey distribute it and when you're done go for it and you notice if the keen observer which is not me so I pointed out because maybe there's others the non CPU spiking code took like nine or ten seconds and the CPU spiking code took like two seconds and so you might think well it should be roughly 1/8 right and it should be but there's some overhead right so there's some thing there is some management there in terms of forming these things out I did this excuse me I actually did this demo with a much bigger copy like I think I copied it 10 times or something like that just to kind of see and as you made the file bigger and as it had longer to go it actually did approach 8 or 1/8 of the time because the overhead became basically standard or standardized and the work then was able to be distributed so as you do more work it gets closer but it is so pretty cool right I mean we're talking 1/5 of the time with really no work for me other than just remembering to call this one instead of call it remembering to use the flow module and to use the partition keyword and so I'm able to then do this work so at this point we've covered a lot of things recover some of the syntax we've we've shown some of the reasons of like why should I care about elixir right why should I care about distributed computing I'm not a telecom you might be wondering we're kind of getting close on time so you might be wondering okay well how do I get started that's a good question because we didn't talk about that yet because I wanted to save that for the end I wanted I want you guys to have that wow moment of seeing the cores right if I start with how do I get started and we start going through websites everyone's glazed over and by the time I show the cool thing you guys have already left the room either mentally or physically so the first thing is that you can go to elixirs website its elixir leg org they have an installation page and you can see Mac and UNIX and Windows and that's probably like that's not surprise it's probably not surprising 1.5 that there's a docker right because everyone's doing docker that's that's really great if you want to play with a lick sir today tomorrow Monday just but you don't want to install it just go grab the docker image and start playing with it and then when you're like yeah this this isn't what I wanted just to leave the docker image or you know this is what I wanted you go and install it the possibly surprising one though is there's a one point for how do I install it lick sir on a Raspberry Pi there's a part of a lick sir called the nerves project which is working with a lick sir on embedded devices raspberry PI's and some of the Arduino and some of these other things like that some of those micro controllers and now you start thinking wait a minute this is a distributed computer that I can put on raspberry PI's I can start getting multiple of my raspberry PI's to share the load and talk to each other with this programming language yeah that's exactly what you can do so now you can start building your own botnet work in your house and have it you know take over and become sentient and run your house right all because you can install raspberry potting soil elixir on Raspberry Pi so it does give you that power like even if you want to move off of distributed off of a chip to actual distributed things grab two raspberry PI's have them pass messages between and it's very simple this used to be much longer presentation too long and there's a it's a it's like a one-line thing to have my computer talk to arts computer until arts computer to run a lick sir code I don't have to I don't have to wire it up I just say hey arts computer I know you've got this code go run it and you do that using a shared cookie so that's you know if both if everyone knows the cookie then they're like great I'll run the code if you ask me a cookie I don't know I have to assume that's malicious code I'm not going to run it so you can do that with raspberry PI's there's also some blatant self-promotion in this I have two courses on elixir at portal site so one is getting started with the lick sir it does a much deeper it's a two and a half hours or something like that where we walk through the syntax and I tell you about atoms and strings and I tell you about all this kind of data and so it's called simply getting started with elixir if you have plural side it's already on there if you don't hear some pre-trials come come grab some I also then have a follow up course to that called getting started with Phoenix Phoenix is the Ruby on Rails for a lick sir and the people in the things in the electric community hate when people say that because they want it to be its own thing and it is and you shouldn't write it like you're writing Ruby but it's it's the de facto web framework and so there's both of these I'm the kind of person that I don't do good with abstract thoughts in terms of learning something new I have to have something pragmatic and tangible so both of these will walk you through building an application with elixir and with Phoenix you'll actually have a website that's up and running I think within the first or second module of this course and you'll have an application that's doing something within the first or second module this course and you build on it because that's how I learned and I figure maybe other people learn that way I have a running I have a I have a gem tracker so I trained I was mentioning this gentleman up front when his co-workers is a Brazilian black Brazilian jiu-jitsu blackbelt and I trained with him and I wanted to know how much time am i spending at the gym just for vanity reasons and so I wrote an app that the tracks I want to go to the gym I take a sparring class or I go to the gym and I do a conditioning class and that app is written in Phoenix and it's hosted on Heroku so you can you can build a Phoenix app and you can push the Heroku for free just like you can with Ruby getting all the advantages of that not self promotion but this is a big part of how I learned a lick sir is Exorcism IO has anyone heard of this the Ruby guy awesome so Exorcism IO is I think a really really cool website and if you want to learn a new language you should go there and check it out they have I don't know 50 60 different languages and they're always adding more and the way it works is there's a list of exercises and this is about a small example of this is the first page of licks or exercises and it works kind of like git so you would say I want to start a lick sir and so you would you would pull the elixir repo you would Exorcism fetch it and it would say okay your first problem is the hello world problem and we're going to give you three files we're going to give you a text file or markdown file maybe that describes the problem we're going to give you the code file that you're going to write your code in we're going to give you that test file with the tests already defined some of our going to be commented out but they're all defined and as these tests pass once all the tests pass you know that you've done the exercise to the to the degree that they wanted it and you can push it up once you push it up anyone on Exorcism can come look at it which sounds kind of scary but it means once you push it up you can go look at anyone else's so you can see oh wow how did they how did they know to use that that's how I learned about string template in elixirs because I push one up and I was literally the only person in like the first 50 that didn't use string templating like where did these guys learn this at like what is this syntax and so I'm going through and like oh that guy must just be a genius mega okay after about the fourth or fifth one I'm like okay I just clearly missed something right so if you go find mine mine now use a string templating because I pushed it back up again I don't want to be only guys I didn't putting right but it's kind of like a get PR so you can if you work on a team you know you guys all want to go learn Russ or you want to learn a lick sir and you get together as a workgroup you can then review each other's you can make comments I've left comments were like oh that was really cool you taught me something right you can like each other's comments code and stuff so this is a great tool I think elixir has 50 exercises and they walk you through there a lot of them are specific to that language so there are there are no for loops and elixir you use recursion like we showed there's no for loops I waited till now to tell you guys I've seen it run out of the room so there's not going to be things here where you use for loops there's no problems for that because you wouldn't do it that way but there are things we like introductions into recursion and you kind of build and build and build and for the most part I think a couple of them might be out of order but for the most part they build so the first one is the easiest and the last one is probably the hardest and you can skip around too if you want if you're like okay I've done enough regex is in a lick sir I don't need to do more regex as you can skip around so all of that let's make sure we circle back and demystify functional programming for my sake you guys are all smarter than me you probably would have picked up Pascal you probably already have picked up Pascal I didn't it took me forever functional programming comes down to three main things what not how and it's not just the syntax right as we as we saw through some of the demos how elixir is structured is focused on the what not how not just the individual how do I build a program but the language itself is focused on the dynamic from the declarative paradigm the what not to how functions have the power I can pass these functions around I can pipe them together I can chain them together I can start building almost like paragraphs with my code by changing these functions together and then the data doesn't change and the data doesn't change for me as learning functional programming was like the thing I cared about the least for the longest time like yeah declarant is awesome yeah I can pass around functions okay I don't care that data doesn't change and then I started realizing that you could do cool things when the data doesn't change like distributed computing becomes way easier and so data not changing immutability is a huge thing and so all of that is to come back and say that a liquor makes functional programming accessible it makes it accessible for someone like me who did object or need programming for 10 to 12 years with no real like no computer science background and I'm able to write functional programs using a liquor because elixir is a language to write programs and I'm not trying to bad-mouth Haskell but my experience with Haskell was Haskell's of programming exit programming language that exists so that there's a programming language called Haskell elixir is not that way elixir is we want you to write applications we want you to enjoy writing applications so we will do what we can to make functional programming accessible if you guys that's all I've got if you guys have questions I'd love to take them but what I like to do is I don't want to hold everyone hostage for one or two questions so anyone is free to go I'll take some questions while I'm tearing stuff down you can catch me in the hall for about the next two or three hours I have to get someone back to the airport later today my twitter handle has been down here there's a hashtag you can reach me all that way and if you want put all site cards a plural site trial come come grab some so thank you you
Info
Channel: Nate Taylor
Views: 11,378
Rating: 4.9544158 out of 5
Keywords:
Id: wVrnoxNbOts
Channel Id: undefined
Length: 58min 32sec (3512 seconds)
Published: Sat Jul 15 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.