 Okay then. So hello everybody. My name is, oh that's not on, that's not my name. My name is Tejas. I am a partner at Nelenzel Software. I was supposed to wear my company's t-shirt but I forgot. We are an employee-owned cooperative that works out of Bangalore. We've been putting code into production in Ruby, Closure, a little bit of Node.js for about 18 months and we're looking for new people and if you guys are interested, send me an email. But more interestingly, we have a list of books over here that we think are really really cool and if you buy any one of these books and send us your invoice from wherever, we will send you a gift voucher on Flipkart. So I'm not here really to talk about this but I wanted to speak about something else before we actually get into today's topic and that is about online abuse. I don't know how many people have been following what's been going on. In the last few days, Sirius Pony has put out, well a blog post, Sirius Pony is known, well her real name is Kathy Sierra and I don't know how many people are kind of familiar with online abuse but when most of us think of online abuse, we really think it's something like this where some guy is on the internet saying, oh my god, you're so stupid. Well it's really not, it's actually a much more serious topic than that. Targets of online abuse have been getting death threats against their children, their families have had to move. It's a very very serious problem and I ask all of you to read these two articles. Sirius Pony about what happened to her with a troll slash hacker called Weave and Adria Richards recently put out a blog post as well just today morning explaining what kind of abuse she faced in the fallout of Bicon 2014 I'd like to say. So anyway I'm gonna start my talk on monads with this sort of quote which is pseudo attributed to Richard Feynman that is if you think you understand monads you don't understand monads. Feynman of course said it about quantum physics but it's mostly true as well. In fact I'm pretty much gonna say that this talk is a lie. I mean like I myself am not gonna claim to be an expert of any of this. I'm really gonna just speak about sort of my experience with the type of things monads do and in fact my own understanding of how things work changed from the start of this slide start of creating my slides till the end of this. So you'll need to bear with me if I'm a bit rough around the edges. This talk is highly inaccurate and will make a mathematician cry. I mean like any mathematicians in the house anyone who actually knew category theory. Okay good so I don't have to worry about this too much but yeah I'm trying to avoid any sort of mathematics at all. What I'm really trying to do is to kind of get you to say oh yeah I've used this hack sort of in other languages because like monads may be very very useful in some languages especially like Haskell and well it's useful to a lesser extent in other languages. We don't often find ourselves having to we don't often find ourselves like in dire need of this and so I want you to what I'm trying to do is kind of explain what you've done in other languages which is something you can accomplish with a monad in maybe a different language that you didn't use and overall just to kind of generally demystify the entire well my thoughts about it. So let's get started. Well monads are just monos in the category of endofunctors. I mean it's pretty simple it's right there. Write it on an exam you'll get like a 7 out of 8 but yeah you I've heard various various different explanations for for monads down from like you know the programmable semicolons to just in general they kind of hide a lot of plumbing and a lot of confusing code away from you and let's you kind of concentrate on your pure business logic and fun fact you can say monad in almost any sentence and people think you're really smart. Like the one exception to this is with my dad when I tried this he's like okay I'm the other city but why haven't you taken the dog for a walk. Dad you are just way so people though you're actually not mad at me. Yeah so I'm going to this is the analogy that kind of stuck with me and so this is the one I'm going to kind of propagate a little bit forward and that is that the concept of a monad is for me very kind of closely related to this concept of a box. Okay so for me this is a value and that is a monad. A monad is basically the box that wraps around a certain value which gives us some interesting properties that we will talk about. Now again I'm going to call out this is highly inaccurate. I've heard monads compared to many different things in the past including a burrito and my new favorite but keep in mind that I'm just using this as an analogy. So let's get back to it. So this was what we have described our monad. So we're saying that a monad is just a box. So let's see what what can we do with this box. So a monad expects you to define two different functions on it and both of these are kind of simple functions. The names of these functions both come from Haskell. Well they come from category theory but let's say let's let's say they're more important than Haskell. So a monad defines two functions return simply takes a value wraps it in a nice box and gives it back to you. Bind takes a box and a function and it basically somehow figures out how to get the value from inside the box because it's the bind function it's built with the monad it knows how to reach inside that box and get your present out and process that value to your function and it takes the f and it takes the f of value and it basically returns that. Now it's common I mean it is very important as and like you saw yesterday with Haskell and how types are related in in Haskell. It's very important that the function also returns a box. So the function returns another monad. So we're going to see this kind of in pictures. So you can see over here value is sorry return basically takes a value wraps it in a box and returns it. Bind takes takes a monad takes a function and returns another box value and that function itself takes a value and returns a box value. So you can kind of see over here that return and our function actually have the same signature and that's going to come in a little bit important later. So kind of just keep that in in your mind. So okay let's get to some very basic math as I said there was no math in the stock but I also told you that I'm a liar so like you need to reconcile that. Can someone tell me what's the answer to this? Yeah there you go. So like I know Svanand said seven but it's not. It's actually three or seven. Yeah so how do we kind of build how can we kind of model this in our system? This is a value and that is a monad. I'm sure this trick is one that you've kind of used in every project that you've kind of built where your state of your computation can be in multiple states because it's non deterministic and so you're going to keep every possible value of that computation in an array vector list. I'm going to use all three of these interchangeably please forgive me and you're going to keep all of these in a particular list and please forgive me again. I'm going to use something that's Ruby like in describing my in the code examples that I have over here. It's not Ruby so like anyone who knows Ruby it's I do differ a little bit from Ruby but in general this is how I'm going to pass a lambda to to any function. So I had I had an area over here one two three I'm going to say dot map and that is a and that's the function so x x to x plus one gives me that increments everything by one so one two three became two three four now please don't tell me that's a block and not a lambda if I'm yes that's Ruby isms okay without further ado let's actually get started implementing all our functions return yeah this was the this was the simplest one to implement right this just takes a value sticks it in a box that's pretty simple I have a function called return takes a value x returns x in literally a box that that works now let's look at some of the functions that we have to actually implement so first off square root right so square root was what was sort of interesting and in our problem that was the one that is actually injecting non-determinism into our problem right so when I take the square root I want to return both the positive and the negative value so I have a function over here called square root takes one argument called x and it basically says returns plus root x and minus root x right again nothing really really difficult over there and you see that square root of four returns two and minus two in an array increment is very similar except it's not even as complex right if you have x x plus five can only be one value so I don't even need to worry about like multiple different values in the list I'm just going to say x returns x plus five in a box now you notice over here that this has clearly followed our function signature where the input of this function is not a moon is not the monad the input of this function is just the raw value but the output of the of all these functions have been monads I mean our boxes so let's kind of just put this all together okay so I have x is equal to m return of four and x dot something where I pass it the square root function and I want minus two and two can anyone guess what this is supposed to be what the question marks are what no so like let's let's first try with map because that's the one function everyone knows when I say x dot map p is square root of p I get a box with a box with two and minus two so now that's not really what I want let's kind of make this into a map cat okay now again this function doesn't exist in ruby but yeah let's pretend it does and so I'll say map cat so I say x dot map cat square root of p and I get two and minus two right this is clearly our bind function right our bind was supposed to take a value pass it and get a function and get like all the and get like the new values in a box right so you can see over here quite clearly our bind function for the for this is called map cat yeah let's let's actually just put this all together and you'll kind of see where this becomes interesting so this is what my code has kind of reduced you I'm gonna say return for sorry I'm gonna say return for map cat over square root of p map cat over increment and I'm gonna get my and then you can see the output I get is three or seven so over here congratulations you've just invented the list monad the list monad is no more complex than this and in fact when you look at all of and what I'm trying to get at is when you look at all of your monads you would have typically done a lot of these tricks in production in fact I want to ask how many people have done this in production where you have like multiple values so you just keep map catting over it I know that's one two three yeah okay so that's that's quite a lot of that's quite a lot of people so you can see that you've already invented the list monad yourself you just needed a name for it so let's introduce oh yeah the other thing you'll notice is this is like turtles all the way down like the minute you wanted to introduce this non deterministic slash monad kind of behavior to it you needed to have all your functions implement well be monad aware so for example your increment five really there was no reason that in theory that it needed to know about the fact that it needed to output a list but because you've kind of taken this approach where of using map cat you need to include it all the way down so good thing or bad thing that's left to you to sort of decide but I just wanted to kind of call it call this out so let's do a little bit of a of a self-imposed constraint at this point so till now all the functions have been able to return like as many elements as they want let's say that every function has to return either zero or one elements you can't return to you can only return one and for the case of simplicity let's just model positive integers over here I'm not gonna worry about negative numbers so return stays exactly the same yeah there return because it's allowed to return zero or one element return had a takes a value it returns pretty much the similar looking box similarly increment mostly stays the same and this works map cat let's look at square square root now I no longer have to worry about positive and negative values of of the square root but let's say I'm going to impose my my error checking over here so square root of a negative number is I really it's proportional to I but we but we have said explicitly we don't want to handle negative numbers over here we're only dealing with positive integers so what I'm saying is that the square root function if x is less than zero return an empty empty list and if square root is and if it's greater than zero return the square root so you can see over here that this also sort of works so you say square root of 4 is 2 and the square root of minus 1 is an empty list meaning that you basically have no values so the very similar concept to what you just did in the list monad but it's marginally different with this self-imposed constraint so you kind of see that there is a that between each of these steps of map cat you have some data that you're passing in between each step right so in this particular case let's actually describe this in English yeah if I was describing this in English what I would say is that we're passing a list to each step maybe this list has just one element and maybe this list has none yeah anyone disagree with that no that's pretty much what this I've said in English so by philosophy of having to be explained to you in simple English it is therefore done this is the maybe monad this is the this is the idea and concept behind the maybe monad to use the more formal definition is that a maybe a maybe value can either be none or it could be just a value this is particularly again this in Ruby and in closure and in other languages you have like nil which for example you can call some methods on or in other languages you have the null pattern which is you can take an object and keep calling different functions on it and it'll continuously return an empty object but keeping but in Haskell this is very very interesting because Haskell has no concept of nil the some object has to be there and so for Haskell none is basically none is sort is the type of something not being there and so this is incredibly important in Haskell but you might have done something a little bit sneaky to get around and solve this in another way Rails for example implements this as well in in the form of try again I know that this is not the best Ruby but so for example if you try is if x not equal to nil return f of x else return nil and you can see try sort of being used over here but I say 4.try x plus 5 I get 9 when I say nil.try x plus 5 I get nil so even though you've kind of been dealing with nil with what are a null object you don't need to check for nil inside this function you can kind of delegate it to one one place which is which which basically helps your code read read a lot better because you don't have to continuously check for null all over the place and oh again a quick show of hands how many people use either null object pattern or I've done something like this pushing nil checks somewhere deep inside their codebase I know I have well well less than the non deterministic but you would think I thought this would be the one that most people did so let's let let's start over like this is sort of the monad explanation you deserve but it's not not necessarily the best one I actually want to actually want to start over I've seen a lot of great talks and I've seen some of them approach monads from this kind of metaphoric way and some of them approaching it from the from the other side which is really from from sort of category theory and I want to see if I can actually do both so so let's get started with these three which are called the monad laws there's left identity which is not really that interesting I mean when you think about it is that if you take take a value convert it to a box with ember turn and then bind it to a function that's the same as just calling that function with the value inside I already kind of talked to you about this earlier so this shouldn't be anything new this shouldn't be very new the right identity is also the same that you take a monad and you bind it to return remember I told you over there return is the same signature as all these functions you should just get the monad back seem seems kind of interesting both of these more return is basically here as good as an identity so that's not exciting at all this is the one that's sort of a little bit more exciting that if you bind two things in sequence so you have a monad you first bind it to a function f and then bind it to a function g that should be the same as binding it to a lambda or an anonymous function which first calls f and then binds that on to g this one is going to kind of become important a little bit later and I'm going to you'll need to kind of suspend disbelief I on why this is important to the little little bit later till now we've kind of been going ahead and storing different values inside inside your box and that's where that's how we've seen monads used thus far but let's actually take like a step into the functional programming world and remember that computations themselves are values functions are values functions are first first class objects so there's no reason we can't store some of this inside inside the box as well and to kind of introduce this I'm going to use this the state monad so this line is sort of inaccurate that the rest of the world is state machine it's kind of is kind of not but I'll I'll kind of you can kind of see for yourself a little bit later so this time instead of actually holding just a primitive value or a series of primitive values inside inside your box this time you're actually going to hold a lambda a function inside your box and what that function signature is is it takes a state does some magic computation returns a value as well as a new state so all most language like different languages having functional purity and whatnot this state and this state are not obviously the same object but this state will be that the old state modified in some way you might as in to use closure parlance you might a sock something onto it which means you this old state might be a map and you might associate the new values onto it it might be a list it could be any data structure that you want but you're basically going to in some way make some modifications and return it as the as a new state so let's do something really simple let's actually try to build a stack the the functions is what I'm going to go first and I'm going to start with push pop and I'm going to make some other really some function some other functions that will help me sort of build a simple calculator okay so just just as a reminder this is how things are called I'll I finally need a function which takes which takes a state return new value and a new state let's start with the push function yeah so push push always needs to take a value because you always want to push some some value onto your stack so this is going to return a lambda yeah the lambda accepts a state and I'm going to say new state is equal to state dot push value and then I'm going to return value and the new state maybe in theory I didn't actually need to return the value I could have replaced this with nil and just return this as just the new state which is most interesting but let's say I'm just going to return both just because I need to return something you yeah next for pop I do something very very very similar I'm going to say value is equal to state dot pop and I'm going to return value and state presumably state is actually changed in between please ignore the quiet mutation that I didn't tell anyone is there and I'm going to introduce one more function which is called double top so double top I'm going to pop off the top value of the of the of the stack I'm going to I'm going to double it and I'm going to push that onto the stack as well so I'm going to double I'm just basically taking the top value of the stack doubling it pushing it back onto the stack we'll now get to the return function in this it's slightly more complex so again my input is going to accept a raw value x and it needs to return a lambda whose parameters the state so I'm going to accept x return lambda of state and then when you execute this this particular function I'm going to get x and state okay let me let me take a step back because people it looks like it's a bit confusing for people the intent of this monad what I'm kind of trying to do is to take a set of lambdas each of which takes a state and and another and and returns like a new state and I want to wrap them one inside the other so I'm going to kind of try to chain chain all of these together so that the outer function has some state does does some change passes passes that state to the next function inside it makes some change do something pass it to the state inside it etc etc finally the final guy will return some value in some state and then this entire thing goes back in reverse anyone who is kind of done rails might so or anyone is done familiar with different object or enterprise something like the proxy pattern that you might be looking at again wildly inaccurate please don't quote me on this yeah so so let's look at this magic function which will kind of bind everything together well sorry pardon the pun so so this is this is my this is my function and remember bind also returns a function that the same box so it needs to return a function that takes state so bind will take a monad and the function this is the same as every every function we've written so far what is going to do is it's going to call the monad the monad with the state right finally instantiate that value that gives me the new value and the new temporary state and then gonna get a temporary function by calling f with the value and then I'm going to return and I'm gonna finally call that with the new state so I'm gonna take some state get some state intermediate and then finally call my last function with with that state with that state value now okay I understand that this is a little bit of code to kind of understand just at a glance so kind of pretend that it works for now this should kind of look pretty familiar to you by now is very similar to how the list monad was working where you basically have return and then you continuously say dot bind passing in something dot passing in something something something something something so after all this right we've we've managed to get this I'm sure nobody has done this in production but this kind of brings me to the interesting part of this talk which is why why why are we doing all of this and I'm going to put a like a quote which I love functional programming it takes smart people would otherwise be competing with me and turns them into unemployable crazy people so the answer is why why are we doing this there must be some reason to do this remember this pesky associativity clause that we spoke about earlier this is suddenly going to become very very interesting this m return for a push a etc etc becomes this now it may look like just that the brackets have changed the parentheses have changed colors in reality this is actually changed things quite a bit you can actually see that what what's going on is that I'm doing like this entire thing this dot bind is now on this push a yeah what what's happening over here is that your stack has actually been implicit well it looks almost as if your stack has been implicitly passed between all these objects between all these between all these functions you don't you don't ever explicitly see the stack anywhere over here you're basically saying that you know dot bind lambda a push a lambda b push b and so on and so forth you don't actually see that where the stack is actually being passed over here it looks almost as if it's just happening by magic so this is where I need to kind of leave ruby for a little bit and I'm going to jump into imaginary language where this is not working code let us pretend that we could turn this into this right this is the this is sort of the power of of of monads where like it looks almost as if this is procedural code where a is is set to the return value of for b is set to push a nowhere in between this am I saying that there is some state or some stack that's being passed between each of these functions the the compiler interpreter or whatever has kind of done a way to to kind of implicitly push push the stack between each of these functions and it and it sort of just works in coming back to the list right we had m return of four square root of b and increment five again I can change this into this form where I'm going to say four square root of a increment b increment 5b and again change it into this right so this might not look so interesting in the ruby world right or well rather it is actually may not look so interesting but let me call out a couple of things for you in this code where have you specified that any of these things are returning multiple values right this has completely abstracted away the map cats the multiple values and all these kind of things for you right that's that's number one number two is this looks very much like assignments and so it looks very much like procedural code but the thing you have to realize is it's actually not and why is it not it's not because this is a funk is is an object is like really this it is just the same thing like rewritten in a in a different style and the reality of this is that a is never explicitly assigned to you're never assigning to the value a is the binding inside of a lambda is the binding inside of an anonymous function and because of that like two things like if this was the nil object if this was for example the the maybe monad if you say b is equal to square root of a where a was minus 1 it actually has short circuited that computation because if you if you realize how it works that lambda is just never executed so even though in your code it looks like you were assigned a value on to be in reality it just hasn't happened just because that lambda has never executed and that is sort of the real true power of sort of these monads which is this last step between turning a set of lambdas into this nice computed form so let's look at a few languages that actually make use of this power right right for the beginning some languages that actually do kind of have this already closure so this is an example from closure net they're also explaining the state monad over here but what they're doing is a state is a vector containing every function that we've called thus far so so it's like I I'm gonna take I'm gonna take a like some some numbers I'm gonna call increment increment double it and something and not only should I get my value I should also get the list of functions that I've called kind of as sort of hidden state that was passed into every single function so the functions themselves are pretty simple that's they're not too interesting so I have an increment function which takes x and a state I'm gonna increment x and I'm going to well conge is how you append to a to a to a vector list in closure don't worry about it so I'm gonna say conge state increment similarly for double I'm gonna say you know into two and I'm gonna add the state again double double here that is increment sorry so this is the final application of it you can see that this syntax actually very very closely resembles what I just wrote in my sort of Ruby sort of pseudo code implementation over there where I'm gonna say a is increment x b is double a which again keep in mind that there's some implicit secret stack that will pass from there to there I'm gonna decrement b and I'm gonna decrement again and I'm gonna find the return D and you'll see that after I execute all these after I execute all these functions so do things to 7 7 double it 14 sorry 7 incremented 8 double it 16 decrement 15 decrement 14 so after running all of these things I'm going to get 14 and then a list containing every single function that was called right this is sort of you can see that closure this is actually part of core dot algorithms dot dot monads I think core dot monads one of the two but you can see that support for this in closure is kind of built-in through closure as a macro libraries and let's see actually how it works like like I'm not gonna be convinced unless I see what what it actually does you'll actually see that the the results are actually pretty shockingly similar to what to what like we built over there a lot of this is closureism so like you may or may not necessarily want to do it but this is the first this is the return this is the return function has a different name and closure but that's what is but that's the return function it takes an argument V and which returns a lambda which takes state and returns V and S which is the value and the state nothing different from what we just described in our semi Ruby semi pseudo code kind of thing before and of course there's the bind bind function bind again is very very similar to what we just described that's going to take a monad and a function it returns another function which takes a state calls the monad with the old state gets two values value and intermediate state and calls the function with the value and then with the state you can actually very very clearly see like how this can help translate into this where you're sort of passing a state and closure you can implement your own monads as well using this def monad if you say def monad and mention your what your new name as well as just define these two functions you can also build up your own monad to do something else interesting as well you can very clearly see in Haskell why this looks much more why this looks appealing as well I have in my case I have a state where like never mind what it does but you can see that it returns the state and state again in a pair and you can see that over here I say x x bound to increment y bound to increment z bound to increment finally return z you can actually very clearly see how this sort of works while x will get the first value y will get the first value z will get the first value in that tuple you can see that how logic logically that second value is sort of implicitly passed to each of these increment functions the first time you might be incrementing one second time two third time three so that's sort of is why this is especially appealing in Haskell because otherwise maintaining any type of state is a is a huge exercise in manually like manually keeping track of it and passing it to every single function that you have this becomes much more much much much more appealing because you're able to yeah yes that's true that's true yeah yeah I mean you're right in that Haskell is lazy while Ruby is not yeah so as you point out there are some minor differences in the intent as as well with Haskell being slightly lazy but but for me that's not entirely the laziness versus non laziness not entirely it's sort of our talk anyway I'll come back to this question like a little bit like in another five minutes or so again Haskell you can kind of see how this state kind of moves through everything so now that we've kind of yeah so now that we've kind of spoken about different ways to hold computation to hold values let's just kind of take this to this logical conclusion but I'm gonna take the whole planet and stick it in this little box over here and this is basically how you want to look at IOMONAD so before I actually speak about what IOMONAD is which I'm really not going to speak about it at all let me kind of tell let me kind of explain why on earth I need this randint of 100 is non deterministic if I want a random number between one and a hundred it's non deterministic how can I actually make this deterministic well return 4 yeah okay fine that may work in comics but that's not what you actually want to do randint of 100 where you're passing it a seed value that is deterministic right does everyone familiar with how these seed numbers work through the random number yeah that is actually a completely deterministic function and by deterministic and well pure what I mean is you pass it the same seed and the same upper bounds you get back the same value every time right but this this first form actually poses a little bit of a problem in a functional programming language well a pure functional programming language like Haskell because in Haskell if I call randint of 100 I should actually get the same random number every single time that is the definition of functional purity so how can Haskell turn the first form into the second form the answer is actually pretty much the same as an extension of your state monad your state is basically the entire planet the entire world is your entire computer and what each of these functions do is it takes it it will do some computation return your new random value and it will kind of send on the world after it's made these changes to the next step in your competition now again taking the world and turning it to the world after IO can actually be modeled as a completely pure function in this case all I need to do is say my last seed was 42 my next seed is 43 right and if and if you're and if like what are the function honors that great everything sort of works again I'm sure somebody will tell me this is wildly inaccurate it probably is but anyway this is how this is how I I am very much viewing this until someone choose me a better explanation similarly puts for example puts is also easy to imagine your entire world state has an object which is called your output buffer every time I wanted to put I'm not gonna actually write to the to the actual terminal I'm just gonna write into this output in add to this list called my output buffer that hey I write these lines whenever you have the time and someone outside the your your function some sort of impure core that's running around Haskell will will kind of say okay fine this guy wanted to this guy wanted to write to the output buffer yeah let's let's paste this in over here similarly gets gets had the same problem and it has a very similar solution instead of just calling gets blindly Haskell kind of maps that to gets of one which will maybe return some string the user entered the first time gets of two which is maybe what the user entered the second time but using this Haskell kind of convinces itself that it's it's being pure while in reality while it's also interacting with the entire world around it again I know I can see that you're like I want to tell me that I'm totally wrong I know I was I thought you probably like yeah yeah okay fine never mind but sure anyway so like yeah and that's actually the end of my talk like if anyone has any questions you like I guess we have time for maybe I have two three minutes so I guess I have one question that's my email that's my Twitter please feel to feel free to reach out to me anyway to just tell me how wrong I am just like that's fine does anyone actually have any questions no no one okay then I will thank thank you guys very much you've been awesome