Dear Functional Bros | Prime Reacts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
nice okay we're in we're so in we're so in let's go we're so in dear functional Bros at least what how many of you type one in the chat if you're a functional bro do you identify as a functional bro right now type one in the chat go go one in the chats one in the chats one in the chats we got some ones we got we definitely got some ones in here in fact YouTube YouTube I'm Gonna Let You Go type one in the chat YouTube You're Going to be a part of this video I'm going to let you be a part of this video just this one time okay I want to see it man see YouTube here here's a little deal people here's a little deal okay I'm going to tell you what YouTube is always like why don't you guys let us be on the chat more often let me tell you something do you see this right here 770 concurrent viewers 148 likes you wonder why you're not going to be on the big screen now you know okay now you know I see you I see you I hear you yeah and I love you oh functional programming is awesome but functional programmers are bad marketers oh that is so good dude you just dude people are in shambles right now that is so good I mean it's so true though the most annoying ones are just like it's called like dude just know you know for a fact that your programming language looks funny and it's hard for people to understand right away you just see the thing you only say that because you know here's the thing is that you only say that because you just haven't done it it's just like okay yeah I haven't done [Music] functions to be a thing and like I know you can construct it but goodness gracious please the debug ability of those things are so effing annoying okay because they really lie on some spectrum between the math of category Theory Lambda calculus and their specific mapping to different language implementations but still I do understand the beauty of it the concept of a function is so pure you have this elegant thing that takes an input and gives you an output [Music] and no matter what the same input always gives you the same output the key thing that ensures that it gives you the same answer always for Infinity is that there's no side effects there's no [Music] State no thing inside of the function that isn't directly derived by the input so beautiful once you introduce State the outcome of the function can vary based upon that [Music] state I understand why people love pure functions right this is it pick pick claps pick is clap and pick is in for this pick's like yes yes you tell them the glories you tell them you tell them about [Music] categories honestly most programming paradigms are really just different takes on how you manage State yeah it's all about state so let's talk about what functional programming is why it kind of sucks but why adding a little bit your code is awesome I like that intro I like that intro it's a good [Music] one functional programming takes one extreme side of the state opinion it's about writing programs based upon One Core concept there shall be no State you have inputs which are trans pick is that true there shall be no State there shall be no State isn't that like isn't scan isn't scan inherently I forget what it's called fold left isn't fold left inherently naughty I think scan is in rxjs I can't remember but it just you get the previous you get the previous you get your input is based on the previous one as well formed into outputs in order to get stuff done you compose and connect functions to other functions hooking up their inputs and outputs together into one big long chain that achieves the goal of the program but you don't oh damn he passed a function in out of the function that function just had a function that function had a baby always not fun to debug have objects or structures in memory that you mutate to track what's going on in this absolute pure world where you want to write functions with no State this also means that you're not allowed to mutate variables so this means if you were to set a value you cannot change it again this means conventional iteration Loops like for and while are impossible if you think about a for Loop or while loop it requires you to mutate some State at least some variable that's tracking how many times you've iterated so how do you do a loop in a pure functional way the answer with functional programming is usually well more functions when all you have is a hammer this is the part that we want to repeat over and over again so we'll just pull that part out to a function so now we need to replicate the three parts of the for Loop the key thing that the for Loop does is increment the index variable so that the body gets rerun with each index so we need an equivalent of incrementing index and rerunning our body inside of process receipt since we can't mutate index you can imagine us creating a new variable next index which contains the next index we want to process but now we want to repeat the section again so we'll need to call Process receipt again so now if you were to call Process receipt let's say with an index of two it's recursion it would print the mergent from index 2 then recall process receipt with index 3 then four Etc recur called it I knew I was a genius I knew as was a genius but he forgot his base case his base case the base case where the hell is the base case but as it is now it'll keep calling the process receipt forever until it goes beyond the size of the receip array causing an air to be thrown yeah and this is where the second part of the for Loop comes into play the condition yes we only want to execute the body if the condition is met otherwise we do nothing and move on so we'll move that condition down into our function I hate how this base case was done I'm not going to lie to you I hate how this base case was done because it's the inverse of the B case but I understand he's doing it for the for Loop okay I understand it's like it's for the for Loop but I hate how it's done okay I just want I just want to throw that out there where we only do anything if our index is still in range you can see when we run the program it works like before it increases the index beyond the size of the array and we still call our function but in this case it fails our condition and we do nothing Yep this breaks the recursive Loop and all our functions return recursion is just a fancier form of iteration and finally we have our last part of the for loop it's a fair take recursion is just a fancier form of of iteration Fair which is where we set our starting point this just represents our first call into our recursive Loop so now we've completely converted our Loop to be done in the more functional way we can also remove any unneeded variables to make our code more concise if we want it okay this is pretty simple and just logs each item in the loop is this where we're about to get into higher order functions here is this where we're about to take this bad boy and create a 4 each are we creating a 4 each is this pre-at have I preat this with life experience but what if we wanted to do something useful like finding all the receipts that belong to a particular user and theyed damn well we can take the user ID as a third parameter and then check if the user matches this now onlys the receipt of the merchant if the user ID matches but instead we want this function to return the list of matching receipts so that it can be displayed on fil we return the results of the matching at each recursive call we want to make an array with the current receipt at the front and the results from the deeper calls attached to the end this way we can get back everything in order but we only want to append the current item if it has the right user ID otherwise is we just want to pass through the previous result so in order to do this we'll actually get the results of the deeper calls first and then if the user ID matches we'll return the current results all right here we go we got this we got this so in order to do this we'll actually get the results of the deeper calls first and then if the user ID matches we'll return the current results appended to the later results this is right where we froze let's go let's go okay we're back in we're so back in we're so back just because my computer breaks just because every now I don't know what happened but this has happened twice now otherwise just forward on the later results is and lastly if we process the entire list we'll just return empty results since there's no more results to be had so now this function just returns all the receipts for a given user ID and we'll give it a better name thing is you can imagine this pattern happening over and over again whenever you wanted to find stuff in an array yep the only difference between these functions is the condition in the if statement yeah we could replace all three functions your first problems you're using JavaScript okay just throwing that out there with just one if we instead passed in a function that returned whether or not an item should be included let's pull out the condition and move it to its own function and instead of taking the user ID as a parameter we'll pass in that function instead we can call it condition or if you want to be a classy fellow you can call it predicate then we just call that function where the condition used to be and that function tells us whether or not to include the item then we can pass our condition the user ID equals function into our filter and if look at this receipt receipt user idid I see a bug I I currently see a bug okay I see a bug of course we can now rename our function to be a generic filter now we can run our beautiful program with our filter and user ID equals function and wait we have a problem our user ID equals function takes two parameters okay code review success okay at least I'm paying attention Okay pre-at we're paying attention we're paying attention here people pre-at preat user ID to check again but our only checks against the receipt itself oh oh my goodness we're we're we're about to get higher ordered aren't we we're about to get ourselves higher ordered functions returning functions I cannot wait we don't know the user ID to check against y we could try passing in another value that we could relay into the function but what if we need more than one like in the case we needed to check if the total was between a range if only there was a way to so instead we're going to get a bit fancy here instead of having our user ID equals function take both a receipt and a user ID we're going to have it take only the user ID that want to compare against and it will construct and return a new function that takes a receipt and let's go gosh I just [ __ ] genius okay I saw it coming like a mile away this is great this is great times right here clearly clearly definitely not pre-at did not pre-at this one okay did not I did not pre-at this one it's just we're just here okay we're just here we're ready we're ready okay by the way there's somebody in here that's saying why is bro Yapp and just get to the point this is the point yo dog this is the point yeah I'm a mod trust me it's pre-at guy doesn't even know the Oracle legit okay that is rude don't call me Oracle turns whether it's from the user ID we then pass in that new function that's been created into our filter this is a little weird but what's happening is that the user ID is like a secret extra parameter which is bound to the function yeah and this function remembers that value and can use it the name for this pattern is called currying instead of handling the entire operation at once it returns a function that handles the next part of the operation the later part has the previous state bound to it that new on the-fly created function is this called curring isn't this a higher ordering I got I'm getting so I'm getting so used to the the old o camels that I'm having a hard time visualizing this are you sure this is curring I don't believe you guys pick give us the details is this curring yeah that's curring is it I thought curring was actually having them as arguments into the function I thought that was binding right because because in the old in the old o camo world if you have a function that takes in two parameters and you call it with one parameter you get back that function that has the parameter preall onto it but the function still gets two parameters yeah curring equals partial application exactly this ain't partial application there's no partially applying here I don't think this is technically curring I think you have to use bind right like if if you're doing the if you're doing the old uh the old the old javascripts right JS and you had a function right a function of Fu that took an A and B and you wanted to you wanted to like Curry that son of a that son of a [ __ ] you'd go food. bind what is it all one and there we go bar now has been partially applied right so now I just go bar you know seven and we get the answer what is bind I'm sorry dog if you don't know what he kind of just implemented binding in this video I don't know I don't that's the same thing I don't know if it's the same thing I'm having a hard time believing you it's the same thing cuz one's using a closure to pass objects the other one's not using a closure people still use bind of course they still use bind man invents language to create more language I just want I just want to hear this thing okay words words words words words I know I don't I don't believe it I'm not believing things is what gets called when filter needs to check a condition can I get banned you can Warren you can get banned you can come straight over to my house and I'll ban you so good baby this is what's a bit funny to me because functional programming is supposed to be so different than object oriented programming but by binding data like this we're creating an instance of a function with some bound data to work with yep instead of creating an instance of a data object with it down functions currying like this is a bit of an older way of doing this you can automate this in JavaScript and many other languages by using bind I'm not saying it's pre-at won't go into bind fully here because really the way we want to do this today is to use Lambda functions this is where we'll move our equals check right into an inline function now this is finally where things start to get kind of nice I'm filtering by user now or I can filter by Merchant just like this or do a more complex check on the total here we might want to get just the filtered Merchant names instead of the entire object if we devise a new function based upon our Loop that doesn't filter based upon a condition but instead takes a function that gives you the desired results for each element we insert that new transformed element into the array instead we'll call this function map because it Maps each element to a different thing damn then we can chain these together so first I by the way I'm so offended by the use of concat I just want you to know that this is just like offensive to me in in JavaScript okay this is all right I'm a functional bro this is just I feel so upset about this we get our list of receipts find the ones with a total between 14 and 20 and then just say for each give us the merchant name so now we get an array of the merchant strings instead of the full receipt objects at the end iterators and if we only wanted to take the first I just want monads and iterators that's all I want I never mind we're not going items we could write a function called take take would give us up to the count elements from the array love take we can chain that on as well so now you can see that we basically created a data pipeline here where we have an input State we filter it down to what we want map the types of the values to what we want and then take the first five to display I I love take by the way our code clearly declares what we want to happen and all the complexity how this is done is behind the scenes in our fundamental functions so we've just derived these functional style fundamentals are in fact I love take so much ay fun fun little thing fun fun little thing here people look at that little little take right here take little Skip and take here huh little Skip and take we're doing a little we're doing ourselves a little bit of a skip little bit of a take okay if you can't handle it you can't handle it okay but these are also built directly into JavaScript so let's take a look at how to use those to create data pipelines but first what's our goal with these receipts we have this amazing new startup idea that solves a uniquely American problem in an American Way in the US I love it I yes come on let's go let's go American problem American way what are we solving when you're in a restaurant you need to fill a tip manually by writing the tip in total this is effectively a mandatory fee on your meal enforced by social shaming your card is initially charged for the total dude some dude D people I recently went to a keios that was self- automated and it asked for a tip that shit's wild okay you're telling me that I'm supposed to tip myself and then give you that tip I'm starting I dude it's criminal I'm starting to get upset I tip just because I I I the only reason why I tip is because I worked a lot of shitty restaurant jobs I don't feel good about it without the tip but several days later the updated total with your hand scribbl tip will show up on your credit card the problem here is that no one is really going to check whether their tip was accurately reported when their transaction was settled so in theory a company or disgruntled service staff could simply enter a bigger number taking more of your money are you worried about losing your hard-earned money to restaurant credit card fraud black Sky Financial finds these small family-owned mom and pop restaurants and sues them for everything they have the proceeds from the destruction of people's dreams are then equitably distributed to you the user of the app based upon how many receipts you've submitted sign up today and start submitting receipts and know that you're helping prevent credit card fraud in your community one lawsuit at a time so yeah I I this is so good this is the greatest this is honestly the greatest thing I've ever seen in my entire lifetime the real American way you think Egyptians sued people a lot they Su dead people by the way just so you know they sue dead people but to pull this off we basically need to grab each receipt look up the transaction in the users's connected credit card accounts filter out the transactions that haven't yet settled or whose totals match correctly sort them by the most egregious discrepancy and then take the top 10 then we display them in a dashboard so our army of lawyers can get to work fighting the good fight let's go here's that code written in a procedural way let's change this to the fun fun approach in modern JavaScript all the methods that we need are already built in and are also methods directly on arrays for each reite we eventually want this result object that contains all of the relevant information I love that sort sorts the array in place but also Returns the array so that you think that you got a different one I love that and now we have two sorted which gives you a new one and we have sorted the receipt the final transaction and the cency so we can just map our receipts to that but nothing's better than reverse I can tell you that I've seen so many people get owned by reverse even Theo recently got owned by reverse oh man he was talking about a bug he had in ping like gosh weeks ago or uh years ago two years ago a year and a half ago got owned by reverse reverse is such a good one I love the fact I love the fact that they exist and they just shoot people in the foot all the time they're so good they're so good we can use the built-in map meth to transform the array into our list of objects the map method you're not a real front-end developer if you haven't spent 30 minutes getting owned by reverse okay makes a Lambda function for Transforming Our receipts into those result objects this condition is skipping over the transactions that aren't yet settled or don't have a difference between the receipt total and the final total on the transaction so we can just change this to a filter okay we'll need to inverse the condition since it now describes what to keep instead of what to ignore this code basically just ensur but this also this also assumes that the that you have the original Total entered in how do you know you have the original Total entered in okay I don't even know if black Sky Financial could be real what are they using Chad chippity to analyze the receipts to ensure that the receipts are correctly done is the mod Pop Shop even using chat chippity I don't even know okay there's questions about this startup all of a sudden I don't know if I believe it anymore is that the list is sorted from biggest to smallest so we can replace this with a sort on our filter list the sort takes a function that tells the sort function how two elements should be ordered it gives you two items and ask for a negative number if a is less than b and a positive if a is greater than you typically get by subtracting B from a but since we want things ordered big first we'll flip this and subtract a from B pre-at pre-at that's pre-at right there people that right there is pre-at did you see that I called the future I looked into the future and saw it and called it okay mind in the game people you got to get your mind in the game when you're watching these videos okay if you're not pre-at it by watching it live you're not in the game you're not in the game and then we only want Count number of elements which we can get with the slice method which we were already doing here so we'll just add that into our pipeline nice and voila after we make the formatting a bit more pretty all of that is written only in 12 nothing is better than than debugging these type of don't you love debugging these type of statements isn't this just like the funnest thing ever don't you just love it like it's just so much fun you get a go in there and hope you get it right you know it's not that bad uh yes it is bad cuz you have to Ed a coded to bug it properly you have to like break these into multiple statements erase the return create a bunch of const along the way which part is actually failing it's not fun they're not like iterators see iterators iterators are different than this iterators pull out a value all the way through so you can actually see a value go through and it's a little bit easier to break point whereas these aren't iterators they don't pull a value all the way through they pull everything at once so you do all the maps at once then you do all the filters at once and then you do sort and then you do the slice and so it just comes out so effing annoying Lines Just functional programming lets you rewrite things into a beautiful pipeline for your data and honestly most of programming jobs are really just taking data from Source a doing some transformation filtering and sorting and then putting it in place B A lot of the time we're just sophisticated plumbers if you'd like to try out some functional programming like this I put some experiments on my website accessible through my patreon I give you some procedural code and ask you to change it so it doesn't use any for Loops or while Loops if you're curious check them out at codes. oh we even blurred it out de functional Bros sometimes I feel like you focus on the complexity and mathematical Elegance of functional programming instead of explaining why it's awesome and help others see how useful parts of it can be maybe we need a special fpro signal to let others know that you're also a secret fpro then you can talk about fpro things like mons but if you don't get the signal we can play a Gill and talk about how nice it is to delete for Loops peer functional programming give me who's my f boys where's my f boys at I feel like there's they describe us with having hasal brains where's my f boys at you know I'm not going to lie to you f booys has this particular sound that sounds like another type of f boy F boys represent F boys oh man something weird about that one where you cannot mutate any state or have any side effects it's kind of awful yeah the is is it's fake computers fundamentally have state I mean literally the whole Von noyman architecture is where you have one giant state in memory and then little instructions which mutate that state to get stuff done input is non-deterministic and any output is forcing some side effect pure functional programming is still mostly academic but what has been happening is the best parts of functional programming have been extracted and injected into our most popular languages like always balance is key and adopting some of the principles like reducing State have been all my homies hate comprehensions okay all the homies hate comprehensions just saying it just saying it when someone tries to go like two three deep on comprehensions I just want to throat punch them shit's annoying when you have when you when you go double deep on a comprehension okay I just want to I just I just hate it okay I just hate it I want you to know that now te loves comprehension of course te does of course he does te TJ loves every TJ just loves everything okay he's like the most positive and kind person ever and I absolutely hate him for it and extracted and injected into our most popular languages like always balance is key and adopting some of the princip like reducing State and formulating data pipelines in your programs where it makes sense can bring huge benefit but without all the crazy restrictiveness and I'd expect more of these patterns to become more common massively parallel systems don't rely on using one big state of memory the more cores you throw at something the more tracking your state of memory and El Square becomes a bottleneck you know the best part is you have all these functions set up and everything looks really great and at the very bottom there's a hashmap with a mutex on it like we all know we all know what's going to happen Okay you think oh I'm a functional program I'm just so good at functional programming yeah yeah yeah I hear you nice mutex hashmap okay nice nice so who knows maybe pure functional programming will have a new renissance every time I by the way I've been trying to experiment using a uh using a multi-rod single consumer on uh in Rust and piping that into a hashmap instead of the old Arc mutex it's been pretty good it's been pretty Prett good I've been happy about that I've been trying to get really good on on the old on the old like I'm trying to step up my game you know what I mean I've been trying to step up my my rust game I've read through the rust atomics and locks book feeling a little bit more confident trying to just step it up I'm still stuck on Tokyo not going to lie to you guys I'm still stuck on Tokyo I'm still stuck doing multi-threading controlling how many things run at once via like a like a context semaphor that I pass around I really wish I was better but I'm not I'm not better okay I'm not better I wish I was better but I'm not better makes me it makes me sad but I really do like just having a semaphor what's that semaphor well it's for helping me make only eight to 12 requests at a time so that I don't get a nice nasty slack message from my coworker asking why do I have a 100 [ __ ] queries going at once to their Dev only database that MacBook mini is about to get pounded if I don't use that semaphor okay that MacBook mini about to go to Pound Town the name poundtown MacBook mini that's what they call me okay a jet
Info
Channel: ThePrimeTime
Views: 196,670
Rating: undefined out of 5
Keywords: programming, software engineer, software engineering, developer, web design, web development, programmer humor
Id: fBKJEj-b86o
Channel Id: undefined
Length: 26min 2sec (1562 seconds)
Published: Tue Jan 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.