 Looks like we're about ready to go. Now I cannot promise cartoon raccoons, but I can promise cartoon lemurs, and I swear they're just as good and just as fun. So just because this is a magic talk, and I enjoy a little bit of theatricality magic, we brought ourselves a magic book along. Now you might wonder what the magic book happens to be for. Well, you see, every time I turn one of these slides, the slide happens to turn. So, who exactly am I? My name's Brandon Weaver. I am an artist. I ended up becoming a programmer. How that happened was a series of unfortunate accidents, which started with getting into web design, which, yes, a little bit of HTML, CSS, it makes you more marketable. Okay. A little bit of JavaScript. You just add a little bit of animation here and there, sparkle a little bit of this. Okay, that seems insane. Now you need a back and wait a second. I know where this is going. I don't like this anymore. And the next thing I know, I'm doing systems engineering. So that is the thing. And currently, I'm doing that over here at Square, where I am an infrastructure services engineer on the platforming team, amusingly working on the same code that Jack Danger happened to mention in his talk. But that's a little bit of fun. So, as far as social networks or anything else, you can find me on Twitter, GitHub, Medium, IRC. Yes, people still use IRC. I know, right? And Ruby.social, which is one of the new mastodon instances. And you will notice that I left tags to my Twitter on the bottom of a lot of these slides, so you will be able to find it later. And I will go back over this at the end of the talk. So shall we get started then? This is Red. You see, Red is a magical lemur, and he loves to reduce things, just absolutely adores it. And he could take stacks of numbers all day long and he just loves it and reducing them into sums. And to him, it's the most amazing thing ever. So let's take a look real quick into how Reduce works. So, Reduce is taking a list of numbers like, let's say, one, two, and three, well, for this example, we'll get into more later, and reducing it into the number of six. And what this might look like is something like that, but that is a bit hard to read. And especially if you don't know what entirely it's doing. I know I certainly didn't for multiple years, and I probably still don't in some case, but we will ignore that part. So let's take a look into what it's actually doing here. Reduce starts with an accumulator, which you'll see in red and underlined there for the color blind or otherwise. And an accumulator is often an empty value. If you add anything to zero, you're going to get back that number, one plus zero, one, zero plus one, one, one, one, well, that one's easy. That means that if we have an empty list, we actually have something we can return as kind of the same defaults. Next, we have the actual list, which you'll see here in, what do you call that, kyan or more of teal? No, honestly, I'm not sure. We'll go with teal. I like teal. And that's going to be present throughout this talk as V. And then the last, and potentially most important, is how do we join the accumulator and that value together, which we'll have in green as our way of joining things together. So basically, number plus number is going to give you back a number. So it ends up looking something like this, zero plus the list of one, two, and three, or in a little bit more common parlance, something kind of like that. So let's take a look into what this is doing step by step. So we start out with an accumulator of zero. And with that, we add the value one, and that's going to give us back a new accumulator of one. So every step of reduce, the accumulator carries over and becomes the accumulator for the next step. So if we go to the next step, the accumulator is now one, and we add to that the next value in our list, which would be two. And that's going to give us back the value three. Which means our last time around, we have three plus our last value, which is three, which means we ran out of values. Which means reduce now knows it's done and it can return that accumulator, which is six. So of course, by now, this means that we have a list of many things and we reduce it into one thing using an initial, often empty accumulator value, such as zero, and a way to join two values together. Of course, this makes us masters of functional programming. We figured out everything there is to do here. There's no point really learning much of anything else. We know this, we've already got it down. Well, the problem is, except Ruby 2.4 came out with this interesting little function, which I know some of you are definitely thinking about as I did this. That function would be some. So I guess my talk's kind of pointless now, isn't it? Well, Ray's question is, reduce unnecessary? So I wouldn't say it's the end of the story because red decides to do something, he decides to ask his master, does some kill reduce? And then something magical happens. Red gets back a letter, and in that letter he found his master had replied to him and said, come visit if you wish to learn. So it looks like we're going on a little bit of an adventure to find the true powers of reduce from Red's master, Scarlet. So Red journeyed over the rolling hills through the grassy plains until he saw a castle upon the horizon, and that castle was nestled between the mountains where the clouds reached the sky. And there is where he found the great and wise master of reduce master Scarlet's. So Red told his master of what he had learned of something list together of using addition and all the amazing things he had done, but he still had that nagging question on the tip of his tongue, so he just had to ask, does some make reduce useless? I read to consider for a moment perhaps that you can do more than just something with reduce. What if we use subtraction, multiplication, division? What if we didn't use numbers at all? Perhaps instead we have an empty array and we push to add elements to it. What could one do with this, I suppose? I know just the thing for you. You'll find three masters in land of enumerable. Go out and learn from them about their functions and see if you can figure out ways to do the same with reduce. And with that Scarlet left Red with much to think on, a map to help him on his way. So with that he was off to the corners of the map to find from the masters how their functions work and how he might use them with reduce. And now a special guest star, a convenient cup of water. Thank you, you're a good friend. The first master he was to visit was the master of mapping. So he journeyed through the forest of transformation deep into the groves where the mushrooms bloomed and creeks ran a sparkling blue. There he found a path and the end of the path was a house. And outside that house he found the first master he was to meet, the master chartreuse of map. Welcome Red, I've heard so much about you and your journey from master Scarlet. Why master? Can you tell me how map works so that I may implement it with reduce? Let me show you young one, the ways of mapping. To map is to apply a function to every element of a list to get back a new list. So then chartreuse began to explain map. So we take a look such that we have a list of one, two and three. We wish to multiply every element by two. That is our function. So each element is doubled. One multiplied by two is two, two by two is four and three by two is six. Giving us back an entirely new list of two, four and six. So Red began to wonder, how would you apply this to reduce? His new list was empty and his joining function was pushed. So how might we use this? Well, you see what we start with is we push on a new element to the list for every step that we go about. But before we do that, we call a function to transform that element into what we want it to be. So taking a look at this all together, it might look something like this. But that's a lot of code and personally I find it very unsettling as well. Yes, I agree. So let's dig into it and find out what exactly this is doing. You see, we have our old familiar list, which is going in as an argument here. And for each value, it comes across as V. And then we have our accumulator, which is now an empty array. And that is going to be there as A. And next we have our joining function, which to this case is push. If you use push on an array, that means that you add a new element to that array. But before we add it, we happen to call the function that we were given with the value, effectively transform it before we do anything to it. So the idea here is that we're pushing onto our accumulator the result of calling a function on a value, which is a bit of a mouthful. I do agree, but we'll take a look at what exactly that's doing. So something like that might be called like this. Now you'll notice that instead of innumerable methods, this is called using a list. And the function, which is a block of Ruby, is still on the outside, much the same. And we still get back the result to four and six, but how exactly is that working? So let's take and break that up real quick. So we start with an accumulator that's an empty array, and we push to that the result of one applied to our function, which is basically the same thing, call or multiplied by two. Which gives us back a new list of two. And now we start with the list of two, and we push onto that the result of two multiplied by two, which gives us a list of two and four. And finally, a list of two and four, to which we push the result of three multiplied by two, which gives us back our final list of two, four, and six. So to map is to apply a function to every element to list to create a new list. To map with reduce is to apply a function before appending an element to list in the first place. With less since he learned from map, it was time to visit the next master, select, and see what there was to learn. But on his way, he found a very peculiar tree, and he was starting to get lost as things seem to go backwards, forwards, and he had no idea which way was up anymore. And on the ground, he found a hat. Now you see with the lemurs, hats meant that there was a master somewhere in the area, I mean only masters wore hats, surely. So Red picked up the hat and wondered aloud, is there a magician? Is he here? And then the tree reached down a branch and said, hello there, would you be so kind as to give me my hat? Are you a master? Well, yes I am, I'm master branch. You see, I get blame the cat for knocking it off again. It was probably trying to cherry pick and couldn't commit to it. Wait, wait, wait, wait, come back, come back, come back. It was about at this point that Red had noticed the hastily scrolled off beware tree sign. Now with the less since he learned from map, it was time to actually go to the master's select and hopefully not find any other oddities. But unfortunately for Red, the master's select was nothing but oddities. He journeyed through towns and villages looking for the master's select. But as he got closer, things kept on getting stranger and stranger until there were eventually nothing but absurdities all around him. And there at the end, he found a house and in that house he found the master of select. And when he found Indigo, the master of select, he found him to be quite eccentric. Select had him with a collection of various ruby arts and toys. But Red had noticed something peculiar in both the tree and around select. There was a cat, so he asked Indigo, is that a Cheshire cat? No, no, no, no, we're not in Wonderland. We don't call him a Cheshire cat, we call him an Esher cat here. So Red, obviously a little bit disturbed by this, decided to keep on going with what he was normally going to do. How do you find anything in this collection? Ah, why was select? You see, I can find all the rubies in the entire room like this. I just had to zoom around and there, we're done. So Red decided to ask, why is master, how may I implement select using reduce? So then Indigo decided to show him. Let me show you a magic trick. I take a number in and a function, and if the function isn't true, well poof, it's gone. Never to be seen again. So Indigo began to explain, let's take a look real quick. Select uses a function to decide which element should go into our new list. Such that we have our favorite old list, one, two, and three, we wish to only get the evening elements. That is our function. For one, it's not even, so we don't want that, we get rid of it. No. For two, it does happen to me, even so we keep this element. It's exactly the type we're looking for. And for three, no, no, no, no, no, no, no, that won't do it all. Which means that we get back simply just a list of two. So Red decided to take a look into this and see how he might implement select using reduce. So like map, we use a function, but instead we're using the function whenever we decide to push an element onto a list, we only do that if the function happens to return the truthy value. Which gives us something like this, but you might notice something odd here. We have to return the accumulator after this. And that's because if you happen to use if and that returns nil, well, reduce doesn't like nil. No one really likes nil. I don't like nil, nil gives me nightmares. But one might say also, you should use each with object. Well, each with object does not fit on slide. So here we are. So essentially what we're doing is we're pushing a value onto the list if it happens to return true when given to that function. And then in any case, we're returning that list. Which means we have a function like this, which might be called a bit like this. And this looks very similar to our map example from earlier, where we have a list of one, two, and three, and we're selecting even numbers, which gives us back a list of two. So let's take a look into how that's working real quick. We start with an empty array as our accumulator and we push onto it the value one, if it happens to be even. In our case, this function is checking if numbers even. And since it's not, we get back an empty list. In our next case, we take an empty list and we push onto it the value of two, if that happens to be even. And since it is, we get back a list containing two. Now, we start with a list of two and we push onto it the last element, three, only if that happens to be even, which means that we only get back a list of two. Which means that select is to use a function to decide which elements belong in a new list. And to select with reduce is to use a function to decide whether or not to add the element to the list in the first place. And with that, we're headed on our way to our final destination, find. But find is quite a way. I mean, just look at that map. That's a bit excessive to walk, don't you think? We're not gonna be mean to read. We're not gonna be nearly that mean. So we drew a train for him. For some reason, the train had taken on the name the Ruby on Rails and he had no idea why that was. But in any case, it was a train and it was quite comfortable. So with that, Red was on his way to the master of find. And as he journeyed along, he eventually saw the city of find violet, the master. He had constructed all these amazing things with all the research and all the things that she had found and a conspicuously placed teapot because I have horrible humor. So he left the railway and went into the town and he found an academy and on his grounds, we found a vast library stretching for what seemed like miles and miles and inside that library, the deepest of Alcove's, he found his next master, find. And inside that Alcove was violet, the master of find. And she was looking for a very specific book as she entered. Ah, so you must be Red. Oh, I've heard so much about you. Come in, come in. I'm just looking for a particular book at the moment. So you look through all the books in the entire library just to find one book. I know how silly would that be that I keep on looking for a book after I've already found it. That's precisely the use of find. Could you show me how find works, wise master? Well, of course. Find works much like select but once we find what we're looking for, we stop looking. If we don't find anything, well, we found nothing. Now there are ways to have a default in this but we won't worry about them today. So then violet began to explain find. Should we take a look? Find uses a function much like select. Find of an element happens to be in a list. Such that we have a list of one, two, and three and we wish to find the first even number. That is our function. So for each element, we check if it's even. If it's not, we keep looking. And if it is, we stop, meaning that we don't even ask three if it happens to be even and we stop iteration. Do you know what a break is, young one? So that gives us back the value of two. So red had to think about this for a while because this was a very strange thing to do with reduce, isn't it? I mean, you're returning one value here instead of an array like you would normally be. So how exactly does this work? Really, we don't even care about joining or accumulating or anything. We just care if the function happens to return true. So in this case, what we do is we break with a value if a function happens to return true, much like select. Now break for those not familiar is a keyword that allows you to break out of a loop and return a value. But even the example looks nothing like we've seen before using reduce. What is this madness? You see, we're producing a denial. We don't even care about the accumulator. You mean to tell me that we're basically using reduce like in each loop? Well, yes, essentially, there's no rule against it, though I will probably get dinged on code reviews for it. We'll ignore that. So we have a way to break out of our function of reduce with a value, but only if a function call happens to return true, which gives us a function that looks a bit like this. I mean, if we never decide to take a break, we're never gonna get anything back. I suppose in the trend of things, that's another metaphor for software development, isn't it? So much like our previous example, fine takes in a list as an argument and a function. And in this case, we return the value two. But let's take a look at what exactly that's doing. We start with a value of one, and we break only if that happens to be even. It's not, so we keep going. Next, we decide to break with the value of two. If that happens to be even, and since it is, that means that we return the value of two, and we don't even bother with three. So to find is to use a function to locate a single element to list, and then stop looking. To find with reduce, just reducing to nothing until an element matches the function, then to break out with that value, which is quite a mouthful, I could do the same as so. So with his journey now done, Red was going back to see his master. But on the way, he ran into another set of lemurs, a particularly odd set, Cerulean, Saffron, and Vermillion. Cerulean was clapping on his tambourines, behind him two other lemurs, Saffron and Vermillion, were singing along, merely. Red was naturally curious about this and decided to ask what was going on. What's with the other two lemurs back there? Ah, that would be Saffron and Vermillion. You see, there are my C extensions. They make every trip go a bit faster, but the problem is, they never listen to anything you say, unless you happen to play a fiddle. And I have not figured this out yet, but as it were, at least they do listen to that. So why the journey to see Master Scarlet? Well, that's because I'm on a journey to establish a new school of innumerable, Tally-Buy. We've heard from the Council of 2.6 that it might be coming eventually, and Grandmaster Nobu may well have ordained the code already. Would you be willing to show me what Count-Buy does? Rather Tally-Buy because I changed the slides. Tally-Buy works like a map, and that it applies a function to a value, but different in that it uses the return of value as a key to keep a Tally or counts of something. In this case, true or false. In other cases, maybe the first letter of a word or something else entirely. So then Cerulean began to explain Tally-Buy. Shall we take a look? Such that we have the elements one, two and three, and we decide whether or not they're even. One is not, so we now have one count of false. Two is, so we now have one count of true. Three is not, so now we have two counts of false and one count of true, which gives us back a hash of true, count one, and false, count two. But that doesn't look like an array. I'll need something new altogether, won't I? So instead of reducing into an array, perhaps we could reduce into a hash. And for those not familiar that zero there basically means any key it doesn't know about is going to have a default value of zero, which means we can add to it as much as we want. And then if we want to keep the same style as we did before, we might end up with something that looks like this. Whoopsie, going back. Now, that terrifies me. I want to never see that code again, so we will make it go away because I'm pretty sure it scares the rest of you too. So we'll cheat a little bit. Ruby is not technically fully a functional language, so we can get away with a few things we're not supposed to. In this case, what we're doing is we're calling a function on a value to get back a key. And then with our accumulator hash, what we're doing is we're taking that key and we're adding one to the count of that. Now we can take a look at our tally buy function, but as always, this is a bit complicated, so let's break it up. So remembering back, we first find our key, which is kind of like map, we're applying a function to a value to get back a new value. And with that, we're taking our new hash and we're adding one to the counts of that key, so it could be something like word.first or rather word zero because we're in Ruby. And all together, our function might look a little bit like this, but that is still very dense, so let's take a look into what exactly it's doing. So like much of the other functions, it takes in a list and then takes a function and with this function, it tries to decide the counts of elements. So in this case, and you will forgive me because slides are such a precious commodity of resource, we're gonna cheat a little bit. We're going to have no counts to start with and then we're going to have the value one, which is not even, so it's false, which means we add to the counts of false one, which gives us back new counts of one element of false, which means the next loop around, we start with the count of false of one and we have element two, which is true, which means that we add one to the count of true, which means we have one count of false and one count of true. And then finally, we have one count of false and one count of true and our last element happens to be false whenever the function is applied to it, which means that we get one more count of false, giving us back a final count of two and one. So tally-by is counting the elements of a list after you apply a function to them, kind of like math. The tally-by would reduce as using a hash to keep counts of values transformed by a function. After saying farewell to Cerulean, wishing him luck on his meeting with the council of 2.6, Red found himself back where it all began, back where it all started. So Red told Scarlett of all the things he had learned and seen over the time he spent in the land of innumerable and all the adventures he'd had. Just look at all the new functions I've learned to make, and I still have to ask, even after all of that, I've only re-implemented functions which already exist, haven't I? Does this mean I should use reduce for everything now or just reduce unnecessary? There yet so many fascinating lessons is out there to learn, Red, but this was but the beginning of a grand journey for you, a grand journey of knowledge. Consider with me for a moment, would you use an axe to trim a bonsai tree? Well, no, that wouldn't make much sense at all. And would you use a pair of hedge trimmers to cut a mighty redwood? No, does this mean I get pruned? Quiet you. The challenge, Red, is knowing the difference between the two and knowing when to use each one wisely. So too are the ways of reduce. It has its uses when simple functions won't do, but that doesn't quite answer your question, so let's take a look into some of the power of reduce that might be beyond what you've seen already. And there is true power here, young one, but power we must learn to use properly before we're able to use it correctly. There are parts of composition, currying, closures, transducers, category theories and more beyond your wildest imaginations. That's precisely what makes reduce so much fun to learn and use. Consider with me that we might be able to take a function that reduces a list and combines actions of selecting and mapping to where if a function returns any value except false or nil, that result is pushed on to the new array after transforming it. So too you could transform and combine any function that can be implemented in reduce into a grand art known as transducers, but that red is a story for another day. For you've learned much and you've had a long journey. There were merely first steps on your journey to knowledge my apprentice, one you will soon be walking. But again, these are lessons for another day. You've traveled far and learned much, and my time here is drawn to a close, I'm afraid. We shall talk again soon, and with that day comes, I look forward to seeing everything you've done and everything you've learned. And with that red journey back home, I had much to think on from this, realizing how little he actually knew of reduce and all the things that he spent so much time learning already and how much more there was to learn in the world. And that to him was a beautiful, beautiful thing. And all the time he had spent adventuring, there was still so much more to learn out there. So many more beautiful things to find, friends to me, people to greet, friends to laugh with, cry with, everything else it could be. And to him, that was the most valuable part of this entire journey. Perhaps this is the end of red story, or perhaps it's just the beginning of an entirely new one altogether. So to wrap up, all the illustrations in this talk were actually my own work, over about 50 illustrations, 200 hours, and, okay, 140 slides, I suppose. Good, he gives me a count, can you believe that? And you will find tested examples that will give you more information here in the repo, and I will pin this, and I will tweet this out later. And again, who I am, you can find me on various social media platforms, and I will be tweeting out various things as we go along. So just credits, because you might have noticed, I hid some strange things in this talk every now and then, and I did get permission, mind you. There were some very odd reactions whenever I asked for permission, but I did get permission nonetheless. So the foxes were a homage to why the lucky stiff. Okay, good, the slides are back, okay. Because why was my first foray into Ruby, and he may have been in a lot of yours too. And that's just to say that whimsy, silliness, and fun are still very much appreciated in the Ruby community. Otherwise it wouldn't be up here speaking, they'd probably throw me out. And then you'll notice that Matt's was hiding in one of the slides as himself. The bundler tape gun was present as itself, and DHH happened to show up as a conductor of the Ruby on Rails. Now, we have decided to have some amount of fun. You might have seen a variety of colorful lemur stickers wandering around the halls, and yes, there is actually a point. I'm not completely insane. Not yet, my doctor has told me so. So we have a scavenger hunt, and you might find the tag lemurs of RubyConf being thrown around. If you search that, you will find all the people who happened to have lemur stickers here. Now, if the people with the lemur stickers would please stand up, so that people might see who you are. Okay, take a good look around and know that these are the people with the lemur stickers you seek. Now, people with the lemur stickers please move towards the back of the room. I hope you all are fast because they're actually very fast as well. And I'm a very mean person. But I will say that that is not quite all we have because you might wonder, what is going to happen next? What is Red exactly up to, and where is he going from here? Well, you see, and if you were paying attention, you might have caught a very subtle little clue hidden in one of the slides. IJTR is not an acronym I've ever seen on a Ruby book before, but it will be in 2019, as Red's lemur will return in a book published by No Starch Press called An Illustrated Guide to Ruby. So that's all the time I have here today. There's not an angry light blinking at me, so we do have some time at the end for various questions and other stuff up here on front of the stage. But I hope you all enjoyed your experience and journey into the land of innumerable. Thank you.