 video that's how I contacted him. His top is really good. I basically followed the loving Alexa by Dave after his tutorial and it was like a breeze to read that textbook so I really recommend Nate's talk and he has a lot of other lectures on JavaScript and other things so I let Nate do the talking now. Okay, yeah well good morning or good evening everybody. I'm glad to be here. Hopefully we can get through this and I'll be clear for everybody. If you guys have questions or whatever I'm open to being interrupted just have somebody on the Zoom interrupt me and something's unclear we can go back over it or if not we can have questions at the end as well. So with that we'll just go ahead and jump on into it. So like they said my name is Nate Taylor we're going to talk about getting functional with Elixir today. My path into Elixir started probably about seven or eight years ago. I tried to learn functional programming for quite a while back around 2010 or so some of the blogs I was reading some of the people I was following on on Twitter and other places kept talking about functional programming and they kept talking about how important it was going to be in the coming years particularly as CPUs got more cores and we didn't really they weren't accelerating the speed as much they're giving us more cores. They kept talking about how important functional programming was and so I thought okay well I better learn functional programming and what I did what a lot of people did back then I found Haskell and I started reading through online books like learning what Haskell for a greater good and all those kinds of things and the same thing kept striking me every single time I would I would start this book and I would okay I understand the first chapter I kind of understand the second chapter by the third chapter I was in over my head and I just I just quit and I repeated this process numerous times until I finally came to the conclusion that maybe I'm just not smart enough for functional programming. They were talking about things like Monads and all this other kind of stuff and I was a well established object-oriented developer at this point that I've been in my career about 10 years I knew C++ I was doing some .NET at the time and I understood all of that stuff it made sense to me I was talking about objects and all that good fun stuff but when it came to the functional side it just it never really made sense the list of comprehension and all that stuff and so I just talked it up said okay well I don't have to just stay in the object-oriented space that's where I'm good that's where I'm smart I'm not smart enough for functional programming so I kind of put it on the shelf for a while and I just ignored it and then you know a couple years later six months later something like that I read another blog that talked about how functional programming made it easier to write software and change the way you thought about software and the programs were smaller and more compact and all those things were appealing to me and I thought well let me try it again you know and I went through this process again and I made it a little bit further in the Haskell books and I gave up again but then just I guess by dumb luck I was following several people in the Ruby community even though I've never ridden Ruby and they started talking about Elixir and I didn't really know what it was at the time but new language is intrigued me so I started reading up on Elixir and that's what I found out about Elixir and I found out that Elixir is really functional programming for everyone and I say that not really tongue-in-cheek I say that it's the point that if I can use it to learn functional programming then that anyone can and that's really the design of Elixir and it's one of its strong points is that it's it's a practical application and so I kind of like to put fun a little bit at Haskell I think Haskell is a great language but sometimes I feel like Haskell exists so that there's a program language called Haskell whereas Elixir exists so that applications can get rid of and that was kind of the shift for me in terms of getting me on that path as some of you probably know it was influenced by Ruby it came out of the Ruby community Jose Valin who was the creator of Elixir was a movie developer he started looking at the things that he liked about Ruby and the things that he didn't like about Ruby and he tried to read the things that he liked about Ruby into Elixir and one of the things that when you talk to people that write Ruby they highlight that Ruby is supposed to be a language in which you enjoy writing software and I can honestly say that that came with Elixir when I get to write Elixir code it's just a joy it makes me smile I was looking at some Elixir code last night that I've worked on and it just reminded me of like oh yeah that was that was really an enjoyable time which which anytime you can have an enjoyable time writing software it's gonna be easier to learn but it's also built on the Erlang VM which makes it super exciting to me which is kind of counterintuitive because Erlang is a 30-some-year-old language and in today's world at least in the circles I run in nobody's really excited about stuff that's 30 years old they're excited about the stuff that came out last night I do a lot of JavaScript stuff now and so JavaScript is changing on a daily basis and and new libraries are being released and everyone wants to talk about those but the reason that the 30-year-old Erlang VM is exciting to me is because it's it's tried and it's true it's proven right the global telecommunications network is built on Erlang and so when you think about what's a system that needs to be all the time needs to handle failures all the time and and needs to be able to work without delays without telling you without telling your users to shut down the system that the telephone system is it right I've never gotten a phone call from my telecom my telephone provider saying hey we need to stay off the phone Monday night from from 11 to midnight because we're gonna roll out a new software update unfortunately I've done that to my users on my website and my other applications and so Erlang gives us that ability to you know have some of these hot releases to have this ability and if you think about what's more global and distributed than the phone network well it's the internet right it's the things that we're writing now and so I think that's really exciting that Elixir gets to take advantage of this this software that's been in production for 30 years while bringing some new ideas to the market so that's kind of my right my real quick introduction of what Elixir is and so I wanted to try and break it down into what is functional programming so I said it took me seven years to get to this point because I wasn't smart enough and and so let's let's set the stage so that you and me and everybody else are all on the same page but when we say functional programming what do we need well the first thing we need with functional programming is that it's a declarative paradigm and that's one of those big words I call them big-o notation words if you have a computer science degree that you found a lot of big-o notation I don't have a computer science degree I have a leftful engineering degree and so big-o notation was not something I learned until I was in the industry but declared it's a declarative paradigm and what that really means is what versus how and so we have this code this is some JavaScript code but it's I'm sorry this is some C-sharp code but it's very common code for anyone that's done some C-style programming whether it's C-sharp Java JavaScript C++ this is something that's very similar to or familiar to a lot of people and as we walk through it really quick we'll see that there's this for loop and it's going to start at zero and it's going to run through all of the users until it gets to the last one by eight and it's going to do that by incrementing by one every single time and then it's going to say okay well if let's check this user and if it's active let's add them to another list of users called active users and at the end somewhere below where this code snippet is there'd be a return of that active users to give the code the list of active users and this is the comparative paradigm but the declarative paradigm is much smaller here we have a lister syntax even about filter we're giving it our users list and then we're giving a anonymous function where we just say return everything where you dot is active is true and so with this one line we're changing it from telling the compiler rather how do I go get the data to just simply telling the compiler this is the data I want all right so back here we had to tell it start at zero go you know increment by one every single time check this flag add this here but on this code we just say hey I want all the users that have an is active flag that's set true and so this is the beginning of the what versus how with functional programming with a declarative paradigm we're trying to tell the computer what do I want and not so much how I get there and I like to joke around and say that it's kind of like a robot right I'm telling my computer go give me this data and it just knows how to go do it as opposed to me telling it first do this then do this then do this and so that's the oh yeah so ahead of my slides here but so that's the declared imperative the the next part of functional programming is that functions are first-class citizens and this is something you hear a lot when you start learning functional programming and it sounds really cool but you're not necessarily sure what it means or I wasn't necessarily sure wasn't it and so if we go back to that filter what we could do is we had this anonymous function this you is active we can make that is active like we see on the screen here we're just saying is active is now this function and we can call our same emu filter by passing in that is active and so now our filter contains two parameters it contains a variable that's representing a list of users as containing a variable that's representing the active there is active function and so we're passing around this function the same as we are passing around a list of users or any other data and so that's what it means for functions to be first-class citizens and then the third major component of functional programming is that it needs to be immutable and this one's a little debated in the functional programming community some people say absolutely has to be beautiful some people say not necessarily but with elixir elixir has a mutability and again that's another one of those big O notation words that makes us sound a lot smarter than maybe we really are so a beautiful data is a more of a simple concept once we get behind the vocabulary there so here we have a list of users like we've seen elsewhere and we have to we have the first one shows that it's active and the second one says it's false or is it an active rather and then we want to do our emu filter users that is active we're going to assign that to our active users variable there on the left after that's done our users variable still has the two users is active is true and is active is false but our active users is a is now a variable that holds a list that is only having that only has one item in it and that's the exact it is true and so it didn't change the users variable it only created a new one it will not so functions that elixir will not change the data that they're operating on they will instead return a new object this is an object or a new data structure that is that is not changed so it's not gonna change the current one it's only gonna produce a new one and this gives you a lot of advantages it makes it easier for memory management oddly enough it seems like it wouldn't but it allows some performance improvements because it's able to tell that at what point in the object tree did the object chain or did the structure change and and it knows if the top the top node is still the same reference and nothing below that change and so immutability gives us some performance improvements it also gives us that assurance that bypass users around a bunch of different functions none of them are going to change that data out from underneath I like to say that if you think about having candy bars and and I gave you guys all a candy bar and then I took a bite of my candy bar the beautiful candy bars mean that your candy bars don't have the bite taken out but if they were if they were beautiful we were all sharing the same candy bar as soon as I took a bite out of mine a bite would have disappeared from all of yours and nobody wants that on a candy bar right like I don't want to be holding a candy bar that suddenly a bite is disappearing out and so the immutability nature of functional programming gives us some of that security and some of that surety that that the data we're working with isn't changing unless we're telling you how Elixir handles the filters is a little bit different than how I wanted it and this kind of stays with the immutability so what I wanted to do when I came to Elixir was write code like this I want to have users that filter his act and insure enough if you do C sharp if you do JavaScript if you do some of these other languages you might see some very similar to this but that's not how Elixir does it right Elixir does it this way where you call the edu got filter and then you pass in users and it took me a while to figure out why then one day I was listening to a podcast and I think it was Jose talking about it but anyway one of the core electric people was talking about when you do it this way it reminds you that you're passing the data into this function and so it's not that you're operating on this list of users you're instead operating on an edu got filter where you're supplying all the arguments and so it's a subtle shift to remind you that the data in Elixir is immutable and it's not going to be changing inside your functions so those are the three pillars and I hope that you know I was a little bit quick so I want to get to more of the elixir stuff than necessarily the functional programming stuff but it's declared a paradigm it's a function this first class citizens and its immutability as I've already said you know it took me seven years to get to this point or eight years to get to this point I call those big on location words so what I like to consider them as it's not declared a first class of immutability but I like to consider it what not how functions have the power and the data doesn't change and when I think of it that way when I started when I started coming to terms with that that was another turning point for me with functional programming I started to say okay now I'm starting to understand why would I why would I do functional programming at all and why would I do it in Elixir or with Elixir rather so Elixir is going to make functional programming accessible it's going to make it something that I can understand and if I can understand it then I'm a hundred percent sure that you guys in the audience you folks in the audience can understand it that your friends can understand it that your co-workers can understand it because Elixir is there to try and make that enjoyable make that understandable so I thought what we do is talk about a couple that the key things that people really gravitate to when they come to Elixir and they might be almost like gateway drugs so to speak they're the things that get people excited and get into the language and then they move on into the bigger stuff or the more detailed stuff which we'll see a demo at the towards the end of the talk here so the first one that you know if you were to ask new Elixir users about almost everyone says they love the pipe operator and the pipe operator I think the best way to show how it works would be to start with an example so here we have this code on the screen there's let's see one two three four more opening parentheses there's a lot of commas it's just kind of a jumbled mess if you throw this up on the screen like it is now you can go okay what does this do it's probably going to take you a little bit of time to process through and do I start left to right or do I start right to left or do I start in the middle of work my way out I'm not exactly sure what's going on well when we use the pipe operator what we can end up doing is we can split this up and pipe data from one function call into the next so it's really all this goes doing is it's reading a file if that tweets not a text there in the stream it's going to split the file on new lines that's the the R curly brace slash in to trim all the extra white space which is the strip and then it's only going to get strings that are tweetable and you can tell this goes a little bit older because it's got 140 characters so I wrote this before Twitter expanded it's 180 but if we take that and use the pipe operator it looks like this so we take our path and we're going to path our pipe or sorry we're going to pipe our path into file dot read and and then we're going to pipe the result of that in a string that split we're going to pipe the result of that into even dot map then we're going to pipe the result of that in the fn dot either the email dot filter and so how this works is the pipe operator will take the result of a function and it will inject it as the first parameter in the next function and so sometimes that's a that might be take a minute to get your head around because it would be easier I think in our minds or at least if I was writing the language I would have done it this way but it's probably good that I didn't write the language because it makes more sense this way in the long run it's easier to think of let me tack this on at the end of the function but that's not how that's how it works so it's going to pipe it in there before the first parameter and so as we look at this we think okay well if I saw this code right if I always would interview and they were asking me about what some code does and I saw this on the screen I could I could have a pretty confident answer that I knew what was going on okay it's going to open a file split it on new line clean it up and then find strings that match a criteria but that's a lot different than this code right this would take me a lot longer and I'd be a lot less confident and so that's the first thing that people really start to enjoy with a lecture that I found anyway is that pipe operator and it's focusing then on the first part the first pillar of functional programming is focusing on the what not the how all we want is treatable strings right where I don't care how the the application gets it to me I don't care what it means to open a file and read it I just want you to read the file I don't care what it means to filter the string and I want you to filter right and so it's focusing on telling me computer what you want out of the out of the code at the end of the day and not necessarily how to go about getting it also it shows us that functions have the power because now we can start composing this into a bigger function so we can get all of this and say you know maybe we define a function called get content where we pass at the path and now we can pass this around all over the place where we could you know probably a better name now that I look at the code and then like get treatable streams or something like that to be a little more expressive but now we can start composing these functions into other functions and start passing them around giving us a little bit more power over what we do the other main thing that developers really clean to when they come to a lecture is pattern matching and I'm actually something that takes a little bit of time to get used to because at first you think oh it's probably just like a switch case statement or a case statement but it's got a little bit more power than that and so the first experience with me and pattern matching was actually the equals sign here and if you read Dave Thomas's book on elixir he talks about how the equals is not an assignment operator it's actually it's actually performing matching and so it's technically wrong in elixir but a lot of people do it anyway it's technically wrong to say that we're a signing person last and they the tailor here but what we are doing is we're saying we've got this to pull here and a to pull is denoted in elixir with just those curly braces and and then it can take any number of parameters separated by commas here we have a two element to pull you have a three four five you can actually have an infinite number of elements near to pull performance that performance wise it's not going to be smart you need to switch to something like a map or a list but it is possible with elixir so anyway we've got a to pull here and on the right hand side maybe we're passing around a to pull of users names so mine here is Nate comma and Taylor's maybe the first the first value is the first name the second value is the last name and we do a match here and so after the result of the first line where we have that first comma last equals Nate comma Taylor at the end of that first now many equal Nate and last is going to equal Taylor if you if you kept up with excuse me if you kept up with the JavaScript community they started doing destructuring with ES 2015 and that's really what this is we're destructuring my Nate Taylor to pull into two variables first and last and that's kind of cool like we can get some use out of that we can probably imagine some cases in which we could destroy stuff and it would be better for us but elixir's pattern matching goes beyond that so if we take this code and again we have on the right side Nate and Taylor on the left hand side that we have first and then we have the same string Taylor what this is going to do is it's only going to match data in which the second parameter on the right hand side is Taylor and so then it's going to still assign Nate to the first parameter and so we can now start saying okay well I want to deal only with the tailors and so I would use the pattern matching here similarly excuse me similarly if we had the same thing on the left first comma Taylor but on the right we had Nate comma Diaz well now we're gonna get an error this is the error that you would see in your elixir code or in your IE in your in your terminal we tell you that there's no match on the right hand side because Taylor doesn't equal Diaz and so there's no way for it to perform that match and so already we're starting to get into some of this pattern matching we're now the data on the left actually needs to match the data on the right so that's a real high level of pattern matching and like I said we can probably imagine some use cases without the useful but let's let's look at some of those so the first thing we can do is we can pattern match return values and so if we go back to this file dot read where we pass a file name in two things can happen when you open a file on your computer right it either opens which is great or it doesn't maybe the file doesn't exist maybe it's locked by another user maybe it is corrupted all sorts of errors could happen that would keep it from opening the file well with this code we have a case statement here where we're telling you to read the file and then do one of two things so the first line it's going to match on okay with a file and the second line is going to match with an error and a reason and so again we see the two goals here on the left we have a two bullets okay and you'll notice on that the first element in those two goals there's a colon and then the name so colon okay and colon error these are atoms in elixir and if you've done ruby they're they're similar to the symbols in ruby but they're just constants and the value of the the constant colon okay is the string okay and the value of the the atom error is the string error and you can find your own atoms but this provides a way for elixir to do some error handling so file that read is going to return one or two structures it's even going to say everything was okay in which case the first line is going to get hit and what it's going to do is it's just going to spit out the contents of the file to the terminal that's what IO puts will do or it's going to say no I wasn't able to open that file it's locked or it's corrupted or whatever it doesn't exist and so it's going to return a two goal with the first value of error and then give you a reason and so excuse me you're able to spit out that reason to the terminal or to the window in some way and so this is the way that you can pattern match on the return values in a non pattern matching language there would be a lot of there would be some ifs and elses in here right there would be like if the first parameter is an error and then spit out the answer otherwise you know spit out the file again I go I can go back to JavaScript because I think it's kind of ubiquitous because I will do a lot of recently but often times in JavaScript you'll see those callbacks where error is the first parameter and we don't have pattern matching like this so we have to check like if the error exists to do this thing otherwise good listening but with Elixir we don't have to do that we need to say I'm going to put these two lines in the the language itself will know which line to call based on how it matches those patterns. But you can also pattern match structs so you don't just have to pattern match return value or function return values you can pattern match structs and so here's one that I call a special event kind of you know a little bit of a dummy thing but you pass in a date and in Elixir Elixir uses a tuple for a date it actually uses a tuple of tuples so the first tuple in the in a date time is going to be the date and the second tuple is going to be the time and then the date tuple is going to be month day year for the for the elements of the tuple and so what I want to do is only give this function a date and I want it to return me an atom that tells me different things and so the first one there you can see is October 31st of 1517 that would be the day that the process of reformation but any other time that is October 31st apart from the 1517 it's just Halloween and so what we want to do is what differentiate we want to say well it's 1517 something big kind of happened then over in Germany but all the rest of the time nothing big has happened on October 31st it's just Halloween and so what happens then as you can see both of them have 10 both of them have 31 but then the third value is that underscored and in Elixir pattern matching underscored means yes there's a value there no I don't care what it is so it's kind of a wild part it's kind of like an asterisk or or just a you know whatever matches here is great the next one you can see would be 10 October 12th of every year is my birthday or we can say something like the first of every year so one one would be new years or we can start getting creative but we can start mixing in our underscores and so on the fifth wall excuse me the fifth line there we have an underscored one underscored and so that what that's saying is every month and every year the first day we're going to return first of the month and then the third there was a sixth one there we've got underscored underscored two thousand we could say oh that was any time there was we were dealing with Y2K and we get all the way down to the bottom and we have this plain underscored it's not a it's not our two bull anymore right it's just an underscored so what it does is Elixir starts at the first line that 10 31 1517 and it tries to match each and every single one of these and if we get to the Y2K one we have a matching thing then we need to have this kind of catch all this underscored to show that it's just a boring every day you know nothing special going on if we don't have that it's going to return to that error kind of like when we were trying to match Taylor to DS because it's not going to be a match but this allows you some flexibility it allows you to have some power over your matching and provide some wild cards which case statements normally wouldn't do if you were to take an imperative language like C or C++ and try to do a case statement there with some wild cards it's it's I'm not even sure you can do it if you can it's gonna be really ugly and really difficult to understand probably hard to maintain but with Elixir it's it's much more powerful and it's much more straightforward about what's going on but we can also so so far all we've done is shown pattern matching in like switch case statements right but we can also pattern match function framers and this starts to get some power more power into a little so here we have this function and it's just called is dynamic and so we're checking various different programming languages and we want to know is this programming language dynamic and so the first one we're going to have an atom of C and C is that is not a dynamic language by any stretch the imagination so we're going to return the false out and then we check is Elixir dynamic and we say yes Elixir is a dynamic programming language so we'll return the true app and then we have a third function there is dynamic and it's underscored other and we're gonna return and unknown so our program isn't very smart and only those two languages that no C in Elixir and it knows which one's dynamic and which one's not but the third line there the underscore tells us we don't care what this parameter is we're gonna match on everything that's not already matched but we can still name it so we could have I could have just left it is dynamic underscore but you can also give it a variable name and just pre fix the variable name with an underscore so that you can kind of communicate to the people reading your code and so that's what I did here is to say okay any other programming language we're gonna say unknown so we'll walk through how does this work so let's take JavaScript and we pass in a JavaScript atom to our is dynamic function what it's gonna do is the first time it's gonna say is dynamic going man this JavaScript match Elixir it says no those don't match so it's gonna go on it's gonna gray that one out say I can't process elixir can't processes then it's gonna say does JavaScript match C so say no those two don't equal so it's gonna cross that one off and say okay the C function can't process this and finally it's gonna come to this dynamic other and it's gonna say well I I'm a casual so I will I will process this and I will tell you that it's unknown or we could go with this dynamic C and again we'll start with the elixir no the elixir says well I elixir and C don't match so I can't process you but then it goes to if I gave me C and it's oh yeah I I match that C and C are the same so I will I will process that and I'll return false because it is a it is not a dynamic language but the point here is that it stops after that second function call it it never it never goes to the is dynamic other and so it returns false as opposed to unknown and then of course if we were to go through and run elixir it was that it would hit the first one it would match it would return true and then it would stop so those are some of the big concepts of elixir with the what not how and the powers of functions I want to do a demo for the power of immutability so I'm gonna break out of this PowerPoint here for a second let me pull up my activity monitor okay so what I've done well I'll do it I'll do the demo version we'll kind of walk through what's happening so get functional flow benchmark get functional flow stream count so what what we're gonna do here this is gonna go through and it's gonna process a text file and it's going to read this in and it's gonna count all the words and it's gonna sum up that all the words and it's gonna tell us how many of each word were how many how many times each word was used and by using this benchmark function that I wrote it's gonna just simply return the time so it took us nine seconds to to read this text file and and what I did for the text file was I took the King James Bible in English and I copied it twice so that there's multiple copies in this text file because that's a pretty significant size of text and it's public domain so I didn't have to like go generate anything and so I was able to create this text file and have it process it and so if you think about you know I mean it's not talking gigs of data we're talking you know k's of data hundreds maybe even 100k data at that and it's at nine seconds and that's I mean it's not bad I guess that's but it's not great so if we change this though from stream count to flow count which is a function that we'll look at here in a second and we run the same thing what we'll see is it's up 2.1 seconds 2.2 seconds so like about a fourth of the time right so what we can do is watch over here watch the CPU this is the this is an 8 core machine that I'm running on and so right now you can kind of see four of the cores are kind of active this one is probably the most active and it's about a third of the way maybe a fourth of the way and none of the other four are active so if I do stream count again and we're gonna let this run for the nine seconds okay now these four cores or you know maybe this one gets almost a half these others are kind of doing stuff but but it might not be related to stream count right this could just be like my zoom meeting over here is this core and again it took 11 seconds to sign so I go into the flow count again we watch the the CPUs well now they all instantly pay out to full and then they just drop down and so what's happening here is because of the immutability of the data we'll look at how this works in a second because they usually the data I was able to use all eight cores in my processor and take this this calculation that's going from nine or 11 seconds down to something that's going in two seconds that's pretty exciting because the code I wrote for this isn't really any different than the code I wrote for a stream count it uses a different namespace and that's about it but because of how Elixir is architected behind the system it behind the scenes rather it provides us this way of allowing us to to distribute this data across our board so we'll jump back into the presentation yes thank you yeah yeah going good so far thanks I think it again died out I'm not sure that I might be there twice now I am yeah that's okay that's okay I think we can hear you now hold on so sure that can you see my screen yes okay so you guys saw the demo right we saw the CPU spiking yep okay so what we'll do then is we'll get we're gonna jump back into the presentation where walk through what was going on in the demo so give functional flow was the module that I wrote for this demo and you can tell it's the module name because it was an uppercase and then benchmark and stream count were some functions I wrote for the demo and you can tell in part that they were functions because they're lower case and so the that kind of makes uppercase a function name or lower case a model name elixir what we plan to give you a warning that's probably not all that earth shattering you've seen that before the ampersand is used in other programming languages to say pass this value by reference it's used in elixir to pass around functions so specifically functions that that are written by custom functions that I wrote for that you write and then the slash zero at the end here that's what they call airy airy is another one of those big functional programming words or big big of notation words airy is just the number of parameters that a function takes and so stream count theoretically could have a version that takes zero parameters a version that takes one it takes two that takes any number of parameters in reality in this code if there's only one version of stream count it takes zero parameters but elixir doesn't know that so we have to tell which version are we wanting to run so we're telling the total the version that takes zero parameters so let's talk about what's actually happening in this code or in this in the demo here so we look at a delixir process it might go something like this the purple block there at the top would represent some code that we have and the bottom you know cylinder would represent some data now I didn't use the database symbol it doesn't have to be a database as we saw in the demo it was a text file it could be a database doesn't happen but this dotted line around these two represents an elixir process there's some code and it's operating on some data and elixir has a lot of these processes in fact they're early processes we said early on that early was the foundation of elixir early has processes and they're not they're not operating system processes and so that's that's one source of confusion a lot of times people think oh my gosh I have all these elixir processes that's gonna really slow down my computer it's not the default size of an elixir process is something like 1k or actually maybe a little less it's not an operating system process at all it's a it's its own process inside the Nina so we have this elixir process and what we can't do though is we can't share this data so we can't have one source of data and have these three processes talking to this one source of data instead what we have to do is we have to have each process having its own data and so you can see here with the arrows that the first process can talk to the second the second can talk to the third and the first can talk to the third third can talk to the first and second basically they can all talk to each other and they do this not by manipulating each other's data but by passing messages and so in a lot of ways this is actually somewhat similar I've heard some people in the elixir community kind of tongue-in-cheek say that elixir is the most object-oriented programming language they've ever worked with and they say it kind of tongue-in-cheek but there's some truth there because what it's doing is it's passing messages between these processes and that kind of goes back to what the original intention of object-oriented programming was if you look at things like small talk it was to pass messages between objects hey or a meeting all right so it was the past messages between these processes and that's exactly what elixir is doing and this is how it maintains its immutability so process one knows that the data that it has is not going to change unless it changes it right it process to can't come in and change its data out from underneath it either can process three the only person that can interact with process one's data is process one and so what that ends up letting us do then is it allows us to just split these processes up and send them to different computing units right so in the case of the demo we can send these different processes to different CPU cores in the case of earth lane we can send those different processes to different nodes wherever they may be in the world the same thing with the lecture we can we can have a distributed a massively distributed network of elixir processes running and and so one of the things if you if you read some about the history of elixir is one of the reasons early was picked is because all of us today have distributed computers that we use on a daily basis but they're distributed on a single chip right so it's oftentimes we think of a massively distributed like a DNS network or something like that where it's distributed geographically and that's definitely a possibility but we also have one that's distributed locally it just happens to be on different cores on our CPU and so there's really no difference whether we're going to hand off some code to go run you know I'm going to send some code to you guys and run in Singapore that's that's distributed or I'm going to send some code from core 1 to core 2 of my CPU to run there's really no difference there maybe apart from a little bit of latency across the network as we send it further and further away so how does this work well what I said I took the King James the English King James Bible and wanted to find the word count and so the first seven words or the first six words rather of the of the of the text there is in the beginning got created up and I think at least the first six so we kind of see what's going on here so I want to practice this by saying this is not exactly how flow works in elixir but this will give us the idea so it might start and break this up and give us a process that has process one has the word in process to has the word done process three has the word beginning process for has a word out of process five has the word created a process six has the word the and then what's going to do is it's going to take those words it's going to trend run so process one says okay I'll handle every instance of the word that very often times of the word in process Tuesday all handle every instance of the right now And so it's going to take process two and process six and combine them into this one lighter purple box there at the bottom. And then process three is going to say, I'll give you all the beginnings, and four equals god, and five equals creative. And so what it's now done is it is now accumulated all of the instances of those words in that process. It hasn't done anything else with it. It doesn't know how many there are. It says, hey, if you have the word the, send it to me. Or if you have the word in, send it to me. And so it's going to start collecting those. It's basically kind of like building up a list of all of these. Well, then it needs to go through and sum all of those up. And so process one goes through and checks the length of its list and says, oh, I've got one. Process two says, I've got two, three, four, and five. I'll say they've got one. Now, you know, I said this isn't exactly how it works, because a process could have multiple words. It could have, process one conceivably could have in the beginning. Because there are thousands of words in this text, or maybe even tens of thousands of words in the text. And it didn't spin up tens of thousands of processes. But it's kind of a good theoretical way to think about it. It's trying to wrap your mind around it. And so what it ends up doing, then, is it can send in to, let's say, CPU core one. And it can send the to CPU core two all the way through. And what it knows is that when the process that's counting the word the, and it's going to go through and count how many times this word exists, it knows that nobody else has the word the. And it knows that nobody else is going to change its list. And so they can just work as fast as, as I say, as fast as humanly possible, but as fast as computerly possible. They can just fly as fast as it wants and count all of that stuff. Because it doesn't have to worry about any of the other processes, because none of them are going to change this data. And this is where the ability really starts to come in. Because we know the data is not going to change, now we can start farming it out. And you saw in the demo that what that meant was, all eight of the CPU is just instantly spiked. Now, you're probably wondering, you don't have to, you can set limits on that. You can control how much of the CPU it takes, how many of the cores, all that kind of stuff. But for the demo, I thought it looked really cool just to show all eight of the cores picked out. In reality, you probably wouldn't want that, especially on a production server, you wouldn't want something taking all the cores, even for a couple seconds, because who knows what else might need to happen during that time. But that's how this flow works. And flow is something that I believe is now in Elixir. If not, it might still be in the experimental namespace. But it was something that was released or announced in the 2016 Elixir conference in the keynote there. They walked through this. And it's a way that, I didn't write really any different code. I wrote some code that opened a file and split the words on whitespace. It's kind of like we did with the tweet example early on. But I did it with the stream instead of trying to load all that into memory. Flow does something similar, but all I had to do was change the namespace from the stream to flow, and the code just worked. So it actually, Elixir handles kind of a, a, what am I thinking about? The words escaped me right now. Well, basically, it queues up a bunch of work and it hands it off to a child process that then starts aggregating it. And so that's something that's either native in Elixir or if it's not yet native, it's still in the experimental stage and it's getting very close to being native in Elixir. So all of that's kind of a, hopefully it gets you a little more excited about Elixir and gets you thinking about some of the things that you can do with Elixir. I like to wrap things up by talking about, okay, well, what can I do to get started? I talked about it taking me seven years to get into functional programming. You guys, by the fact that you're in this user group tonight are already further ahead than me, but I don't want anyone to have to spend seven years to learn this because I think it's, I think it's powerful and I think it's helpful. So how do I get started? Well, the first thing is install Elixir. Won't talk too much about this, I'm gonna assume that you guys have. The one cool thing that I see here, so this is an old screenshot because I think it's one, yeah, it's telling us, hey, Elixir, one forge at least. And we're up to one six now. But you have your normal OSX and Unix and Windows ones, which are not surprising, it's important in every language or every operating system. You can get Docker images, which is cool if you want to play with it and you don't want to necessarily install it, you can just use a Docker image. But the thing that's really cool to me is that 1.4, that's the Raspberry Pi. So there's a project called Elixir Nerds, which does embedded Elixirs. And the reason that's exciting and interesting and cool to me, even though I haven't actually done anything with it, is when you start talking about distributing about how Elixir can be distributed easily, can easily be distributed in computer, and then you start talking about Raspberry Pi's. Well, now you have cheap devices that will run Elixir and you can start having, like you could create a botnet in your house or you could create SkyNet from Terminator in your house with a bunch of Raspberry Pi's. And they can all be talking to each other and sharing Elixir code. And it's going to be actually kind of cool because I think a lot of us, I don't know what you guys age are, so I'm 40, so when I was growing up as a kid, that was the kind of stuff I wanted to get into, right? Having all these little robots and all this kind of stuff running wild. And with Elixir and Raspberry Pi and the Nerds project, you can do that. Do a little bit of a self-promotion. You guys already heard, I have a getting started with Elixir course on the portal site. One of the things that I do when I learn software and whether it's Elixir or anything else is, I have to have something tangible. I've got something that I actually do with it. I can't just go and learn the syntax, that doesn't work for me. So what getting started with Elixir does, it walks through the syntax for sure, and it gets way deeper than this presentation will. Talks about data types and all that kind of stuff. But at the end, we create an application that processes some data. This one will tweet out string source, and that's where I pull that code from. And so it'll walk you through learning Elixir to the point that you can then write an application. Once you do that one, or if you're already comfortable with Elixir, I have a second course on the portal site as well called getting started with Phoenix. It does something similar. It takes an application and it's kind of like Yelp style review application, and it creates a web application for that. It is in Phoenix 1.2, however, it was well known that Phoenix 1.3 was gonna change how they structured the projects. They were gonna create umbrella projects. They were gonna separate the logic of Phoenix out of the Phoenix portion of the application into an Elixir application. All of that stuff was well known before 1.3 came out. So even though the course uses the 1.2 library, it uses the 1.34 because there was a lot of concern in the Elixir community that people were just writing Phoenix applications without really extracting it out into Elixir. In fact, when I was at ElixirConf in 2016, I can't tell you the number of times I heard that Phoenix is not your application. And that kind of comes from the Ruby world where there's just one big giant Ruby monolith for projects and what the Phoenix guy or what the Elixir guys wanted was to say, okay, no, there's this core Elixir component. And then the Phoenix portion is just how you deliver it to the web. And so the course walks through all of that and you learn what all the ins and outs of Phoenix in your building application. So that's the way it's self-promotion. We'll get that out of the way. How I learned Elixir in large part was from Exorcism.io. So Exorcism.io is a website that has, I think last I saw 50-some different languages on it. It's got the standard ones like Java, C++ and .NET and all those good, but kind of boring ones. And then it's got things like Elixir and Rostin, Go, some of the other maybe newer or trendier ones. And excuse me, what Exorcism.io is it'll actually give you exercises, small, bite-sized exercises. And when it gives them to you, it's kind of like get. So you'll do a pull, you'll say pull the next exercise, or in fact I think with Exorcism. But anyway, you're saying give me the next exercise. And it's gonna deliver three things to you, three files. It's gonna deliver a text file that tells you the goal of the project. It's gonna give you the code file in which you'll write your code. And then it's gonna give you a test file in which they've already defined tests. And I think most of the time the tests are commented out because if all the tests are failing, that's a little overwhelming. And so what you'll do is you'll uncomment one test at a time and you'll write the code to make that test pass. And then once all of your tests pass, you know that you've completed the exercise and you can push it back up. And once you push it back up, you actually get, it's actually, like I said, it's kind of like get. I think it's built with that in mind because you can then start reviewing other people's other people's work. And so something like your guys' user group, this might be a really cool way to check each other and share how you did things because you could work together. You don't have to work, you know. Okay, this week everyone do the number one exercise. You could work hour fast or however slow you wanted but the code's always there to go check. And so I like to tell the story that so the first one you do is Hello World. And the first one you do and I think every language is Hello World. And this one was just basically a stream interpolation or a stream manipulation. And I pushed mine up and I was pretty happy. I got it working and then I started reading other people's code just to kind of see how they did it. And I started seeing this weird hashed sign in everyone's code and I started looking and was like, oh, that's how they do stream interpolation in Elixir. But I didn't know that before I did the exercise. I just saw everyone else doing it. I researched it, oh yeah, the stream interpolation. So if you go look at my exercise in my own for Hello World, you'll see it has stream interpolation because I didn't want to be the only person on exercise in my own that didn't use stream interpolation. So I made some changes and I pushed it back up. But these were really small, well-contained exercises that get you into the thinking of Elixir. My favorite one, and I don't see it on this list, but my favorite one was you basically wrote your own list model. So you wrote your own map, you wrote your own reduce, you wrote your own reverse, and maybe some of the others, maybe like a count and a filter, I don't remember. But it was fun, not that I would ever use that code. I mean, why not just use Elixir's built-in one? But it helped me understand things like how Elixir handles recursion and how it does a lot of this other kind of stuff. And so it was a very fun thing, and it was done in such a way that I might sit down on a Saturday and try to do one, and then I look up and it's been a couple of hours and I've done four just because it's fun. And so that's something that I would recommend trying out and checking out and just seeing if that helps you with different exercises and stuff as well, or lists or stuff. So finally, we're going to wrap up. How do we demystify a functional programming? Well, remember, it's what not how. So we're going to tell Elixir what do we want. I want a list of strings that are to be able, or I want a word count of every word in this text. Functions have the power. I'm going to create some functions that I passed to other functions, and I'm going to create these small, bite-sized functions that will let me pipe them. You know, I can now include my function in a chain of pipes so that I can pass this function around and clean up the data and make it smaller and more compact. And then the data doesn't change. And once I know the data doesn't change, that opens up the door for distributed computing. It opens up the door for multi-core processing. It opens up the door for really working with the data knowing that the data you have is going to be the data that is not going to change. And so that really gives you the power to try and do some adventurous things with your code. So again, at the end of the day, I feel like Elixir makes functional programming accessible to everyone. Like I said, over and over again, you're probably tired of me saying it. It made it so that even I could understand it. And I don't say that with false humility or anything like that. I really believe that because it took me so long to get to this point to where I started to understand functional programming that now I'm able to have those conversations with the smarter guys at work that do really get functional programming. And I can do it because of Elixir. So the three takeaways, oh, that was for a conference. I'm sorry, there aren't takeaways. I mean, you can kind of try to hug me, but I'm in the United States it's a little bit harder. Or hug the organizers rather. I love, I do love to have people talk to me. So if you want to follow me on Twitter, my handle is down here at the bottom. I will talk to you guys there or email or whatever and then, you know, they'll try out Elixir. So with that, my presentation's done. I'll open it up to any kind of questions that you guys have. Does anybody have, thanks a lot, Nate. Does anybody have any questions for Nate? Okay, Nate, actually I have a question for you. Sure. So for me, even I just gave a presentation just for you and even I was talking about learning Elixir and I was also talking about learning Exorcism, learning through Exorcism. For me, I just wanted to ask you, how do you end up learning those more challenging functional programming concepts better? What really helped you understand pattern matching better? What helped you understand recursion better? And those particular functional programming concepts which may not be very obvious upfront and can take people some more time to get a grasp of what do you think people can do to understand those particular concepts better? So they can get up and running with the language much faster. Yeah. So let me, let me somewhat have to make sure I understood, right? We talked about some of the simpler things but like pattern matching can get more complicated, it can lead to recursion. What's the approach to take to learn some of those things, right? Right. So interestingly enough, I actually cut out a section on recursion because I was trying to, this presentation when I do it at a conference is more, it can go into like an hour and 15 minutes. But I think when you say recursion, sort of about the folks in the audience, it almost makes me break out a cold sweat because if you've done recursion in an imperative language, I've never done it right. I'll just say that I've tried it before and I always mess it up. What helped me with Elixir, actually pattern matching and recursion went hand in hand when I was learning that. So obviously I started with a more simple pattern matching of like let me just pattern match on like a case statement. Okay, I've got that. But there was an exercise of my own exercise which needed recursion because it needed to take a list and then start like working its way through that list. And to do that in Elixir without recursion would be really ugly and really hard. And so pattern matching there was one in which, okay, well now let me go back here. Let's go, actually you know what, let's do this. Let me open that presentation. Well, I'm talking about it. And that's not on the screen, is it? All right. So we would come here. Is that possible? Yeah. Yeah. It's going to go back. I'll pass this. Yeah, that again? It would be awesome if I thought that company would be welcome on the platform. Yeah, we should look at it. That was the exercise of my own. It was one of the exercises there. Okay, cool. But what it was is, it was kind of this, right? So if we want to reverse a list. So if you have a string ABCD or a string one, two, three, what you want at the end is you want the opposite, right? You want three, two, one. And so this is an example of pattern matching with functions and doing recursion. So the way Elixir works is it takes the, it's going to match first of all in the number of parameters and then it's going to match by the exact parameters. So here we have a list. We're going to pass in one, two, three. Here we have an empty list as the first parameter and then the third option is a list. And so if we run through it really quick, if we pass in this list one, two, three, the first thing it's going to do is it's going to say, okay, well I matched the one reverse list, the one reverse function that only takes one parameter. And so what that function does is it then calls this. It calls reverse one, two, three, and then an empty list. Well that matches this function where it has head and tail. Head is just going to be the first item in the list and tail is going to be everything else. And so it's okay, well I know how to handle that. So it's kind of coming back into that function pattern matching, excuse me, the pattern matching of function parameters that we talked about is dynamic. And it's just going to keep calling this function as we walk through it until it gets all the way down to here and now what it says is, well now I have a reverse with an empty list and a list and that doesn't match the first one because it has multiple parameters but it does match this one because the empty list matches here. And so that was the beginning for me of recursion and I did all of this here in a minute. This probably took me about a day of playing with or maybe even longer, but playing with trying to figure it out and just like trying things and failing, trying things and failing and getting part of the way there and then something would mess up. And so it was really just, for me it was a lot of trial and error was how I picked up recursion, but once I got it and there's some, I can't remember the gentleman's name, but there was someone on the Elixir team that wrote a blog about how Elixir handles the list functions like map and reverse and that kind of stuff. And it was very simple. It was, I mean, he used simple language to make it so that people like me could understand. And once I read that and did some exercises, then I started understanding it. And really once I got the idea of recursion, it was one of those kinds of things that was like a snap. You're like, oh, now I can see how this would work in lots of areas. And so I guess my recommendation would be to pick a problem, maybe like reversing a list and work on that until you can understand how the recursion's working. And then once you get that, we'll start seeing the power of recursion where you can use it at other places. Sure, yeah, that's an opportunity. Yeah, absolutely. And these, hang on. Hello, yes, yeah. So now that you understood lots of stuff by using Elixir, do you feel more confident about trying Haskell again, for example? Do you think if you learn something from Elixir for like other things, or like for example to go back to Ruby or something? Yeah, absolutely, that's a great question. So, as I said at the beginning of the talk, I've got nine Pluralsight courses, only two are on Elixir. So one of my more recent ones was a functional JavaScript talk, or functional JavaScript course, and it was the real basic stuff like map and reduce. And so what I'm, my next one that I'm working on is a more advanced functional programming concepts for JavaScript developers. And what I'm finding, I'm doing a lot of research right now, and what I'm finding is I'm winding back up with the Haskell documentation, funding it up because that's kind of the, one of the sources of truth for functional programming on the internet I guess. And it does make more sense now. Now there's still plenty of things I have to go look up because Haskell uses a lot of category theory terms and mathematical terms, but it does make more sense now. So I have, once I learned the concepts with Elixir, and I was able to learn the concepts without being overwhelmed with the vocabulary I think, now I'm able to understand Haskell a little bit more. I never, I wrote one API in Ruby, actually it was Sinatra and I did it in a weekend and that was about six years ago and I haven't gone back to it since. So it's, I haven't come back to Ruby but I was never at Ruby really, but it's definitely helped me in other areas. It's helped me even in my JavaScript, writing better code I think by understanding Elixir. Right. Thanks. Yeah, absolutely. We have any questions for Nick? Yeah, Nick I have a question for you. This is Anil Tabla. So we, a lot of people come to Elixir and learn for basically concurrency, right? So we've got Beam which is well established and an implementation, earliest implementation of actual model. But there are alternatives, right? So the mainstream languages like Scala and Java, they have their own, like, actors are inbuilt in Scala and we have Aaka which is an actor modeling for Java. So when people in the mainstream already have support for actor model, where do you think that like, our lang could be used for, I mean, it still has some benefits I guess from the Beam's VM point of view, but in terms of catching up in mainstream, what do you think should happen so that Elixir catches up in all, so. Let me make sure I understood. So we're talking about different languages, Scala and some of the stuff on the JVM and some of the other languages that perhaps are a little bit more mainstream. So is the question, how do we handle, like how do we get Elixir mainstream? Or I'm sorry, am I just the actual question? Yeah, so a lot of people come to Elixir for concurrency reasons. They want to get, but they have a huge amount of concurrency, they expect a huge amount of concurrency, like internet this application. So they probably think about Elixir, one of the reasons, right? So Elixir and lang. But we have actor implementations in other mainstream languages, right? So right now, so people use Akha and they know, actors in Scala. So my question is, where does Arlang shine? I mean, where do you think does it shine that people choose? Yeah, no, that's a good question. Yeah, so Erling is not the inventor of the actor model, right? And that's clear from what you're talking about with Scala. And even in my non-JavaScript days, I was at .NET developer and there's some actor stuff at .NET as well that you can implement. Yeah, so I think where Erling really shines in a couple of ways. One, to me, it's the time that they put in. So Java's great, the other languages are great. Erling's been around since 1985 and been running in production since 1985. So there's some definite advantage there, right? Like there's, I have to imagine, I haven't done the research, but I have to imagine there's very few bugs in Erling and that there's some very well-tested patterns and practices. And that's not to say that there's not in Scala and some of these other languages. But that's, you know, it's kind of like looking at wisdom, right? You go talk to your grandparents because they're wise and they've probably gone through, like they don't understand what Instagram is and Facebook, but they understand what it means to have genuine relationships. And so you listen to them about, you know, relational advice, that kind of stuff. That's kind of where I look at Erling. The other thing too is Erling has amazing fault tolerance. One of the design principles of Erling was to let it fail. Whereas, you know, Java, not so much anymore, but Java, you know, used to have to check every single exception.net has exceptions that, you know, you can use if you want to. But that's just very exceptional, right? And so when something fails in.net or when something fails in Java or when something fails in JavaScript, it's abrupt, it's disruptive, it's painful. In my case anyway, I never accounted for it. Every exception I account for is not the one that hits. It's the one that I didn't think about. All that kind of stuff, right? Well with Erling, what their mindset is, is well we can spin up these processes that are super quick and super easy. And so if something fails, nine times out of 10 or 99 times out of 100, it's gonna be data and not code, right? Like the code's probably fine. And so let's just restart the process. Excuse me, let's restart the process and kick back off again and we'll let that go. And so you will see something fail and pick back up in Erling and run before you've even realized that it failed. And so I think that's a selling point of Erling as well in addition to the concurrency, is that it can handle that failing really well. And there's another demo that I don't have on me anymore, but Elixir takes advantage of that with their supervisors. And supervisors are processes that supervise other processes. And so their whole goal is to watch this process and if something fails, it instantly restarts it. And so to me that's really cool because you might get a weird, something happens on the computer and the process fails. Well, in my world and .NET and JavaScript land, I have to dig through all the logs and figure out why it failed and restart the process and make sure I'm even aware, usually what happens is a customer calls me and says, oh, this thing isn't working. With Elixir and Erling, by the time I know that the thing failed, it's already restarted and processing requests to get. And so I think that's another advantage over just having the ACR model. Does anyone have any questions? Cool. I guess that's about it. Thanks a lot, Nick. Yeah, it was all the way from the U.S. I really appreciate that. Can we have a round of applause? Well, thank you guys. It was probably as much, I enjoyed it as much as you guys are not more being able to talk to you this morning, my time and tonight your time. So thanks, everybody. Thank you. Thank you.