bouncer
← Back

DevInsideYou · 18.1K views · 585 likes

Analysis Summary

30% Minimal Influence
mildmoderatesevere

“Be aware that the creator frames functional programming as a 'discipline' that solves software maintenance issues, which may lead you to undervalue object-oriented approaches that are equally valid in different contexts.”

Transparency Transparent
Human Detected
95%

Signals

The content exhibits clear signs of human authorship through its conversational delivery, personal references to the creator's life and community, and a non-formulaic script structure. The presence of natural speech disfluencies and specific mentions of external constraints (like paying a video editor) strongly point to a human creator.

Natural Speech Patterns The transcript contains natural filler words ('now', 'anyway', 'by the way'), self-corrections ('now that i'm thinking about it'), and conversational transitions.
Personal Anecdotes and Context The narrator mentions specific personal details, such as not being a data engineer, his Patreon supporters by name, and his relationship with his video editor.
Technical Nuance and Opinion The script expresses specific opinions on the Scala community's evolution and the trade-offs of the language, rather than generic definitions.

Worth Noting

Positive elements

  • This video provides a rare, commit-by-commit evolution of code that makes abstract concepts like Monads and Tagless-Final tangible for developers.

Be Aware

Cautionary elements

  • The framing of functional programming as an objective 'discipline' rather than one of several competing architectural philosophies.

Influence Dimensions

How are these scored?
About this analysis

Knowing about these techniques makes them visible, not powerless. The ones that work best on you are the ones that match beliefs you already hold.

This analysis is a tool for your own thinking — what you do with it is up to you.

Analyzed March 13, 2026 at 16:07 UTC Model google/gemini-3-flash-preview-20251217 Prompt Pack bouncer_influence_analyzer 2026-03-08a App Version 0.1.0
Transcript

last week i made a video about cats versus zero for season scholar developers and in that video i promised to make another version of it for beginners now in order to do that you need to really understand functional programming and so this video is also going to serve as sort of a crash course in fact we'll cover pretty much everything you need to know about functional programming now that i'm thinking about it i might as well call this video like this yeah i think it sounds good anyway let's roll the intro hey vlad here from davinci.com welcome to another video now this is mostly a scala channel however if you stumble on this video for any other reason don't worry most things are applicable to any other language and since you're new to scala allow me to welcome you to the scala community welcome scala was one of the first and it might actually be the only programming language out there that successfully managed to combine the object-oriented and functional programming worlds in a seamless way now nothing is perfect in this life everything has trade-offs one of the challenges that this even though successful mix poses is the fact that now you as a programmer are left with too many choices for building software and so since the beginning the always evolving scala community was searching for the best way to do it now since you're new to scala there will be a lot of by the ways in this video and here's the first one even though scala is a general purpose programming language it found its niche in a wherever java found this niche which is in the back end of web applications which is also what this video is concerned about and be in the world of big data which is not what this video is concerned about since i'm not a data engineer if you're serious about diving in the world of scala rather sooner than later you will discover the fact that we have a lot of tax tags to choose from some of them lean towards object-oriented programming some of them lean towards functional programming and some of them are somewhere in the middle now back-end applications tend to grow over time and functional programming offers solutions for maintaining large pieces of software and so over time the scholar community lean towards pure functional programming we will talk about purity later in the video but the point i'm trying to make is that even within this functional niche you still have choices so one of the goals for this video is to help you choose and the way we're going to go about this is by explaining the appeal of functional programming in a non-hand wavy way let's begin right after the message from our sponsors which is you the people who support me on patreon without you i wouldn't be able to pay for my video editor who allows me to spend more of my time to hang out with you doing live streams or answer your questions on discord there's many of you and only one of me so every dollar counts because eventually it adds up special thanks to fred albio and johann for being my highest tier patrons thank you now let's talk about functional programming first and foremost i already made a video about programming paradigms so you might want to check it out also i made a scala crash course which is three and a half hours long so you might want to check it out as well by the way guys the theme for this video is to be sort of like a crash course so we're gonna move pretty fast okay this video even though it is for beginners it's not for absolute beginners like i hope that you have like maybe a year of experience in some other language now what is functional programming well functional programming same as any other paradigm is a set of constraints aka a discipline that we the programmers follow to maintain potentially large pieces of software now one important thing to keep in mind is that this discipline is imposed on you the programmer now programming languages type systems tests and other tools are useful but at the end of the day they're not bulletproof and if they were would have skynet and you wouldn't have a job now every discipline has a cost and functional programming is not excluded from the statement it's only worth the price and thus valuable and large always changing applications in fact this is what software is all about right it's about change software is easier to change than hardware now what is the value of functional programming or in other words why would we constrain ourselves to follow this discipline simply put as a programmer we have many things to worry about and function programming removes some of those worries it reduces bugs and prevents regressions thus giving us more confidence when we want to change our applications which is something that happens all the time but remember it's not bulletproof now what is this constraint though what is it exactly that we need to give up now the answer to this question is assignment but it doesn't really help you does it this is also the reason why usually when fp is presented you're just bombarded with buzzwords like immutability and the fact that it somehow helps with concurrency and there's talk about recursion and lambdas and referential transparency as i said many buzzwords instead of giving it to you straight what i have for you today is a repository with 32 commits and we're going to go through all of them i used to do all of this stuff live but it turns out that typing takes a lot of time and this video would be like three hours long so we're going to simply run through 32 commits over here and this repository is also going to be on github and if this video turns out to be too long i'm going to cut it into two the goal is to focus on the question that most fb beginners have which is how to do an immutable print line and somewhere along the way we're going to define functional programming very precisely without any hand waving let's start going through this repo so git allows you to only go backwards because the pointers point backwards which is why i tagged everything like this so you can check out any tag you like and i'm also going to be pushing these tags okay so i have uh made videos about zsh and oh my zsh and all my dsh comes with a git plugin which comes with a bunch of aliases so today we're going to use a bunch of uh git aliases okay so i'm going to show you a couple so gco is good check out uh gsh is good show and get show hyphen s is like good show you know short and yeah this is pretty much all we're going to need so what i'm going to do is i'm going to check out the v1 right and i'm also gonna do g pristine uh g pristine is pretty much going to remove a bunch of stuff that is like uh laying around hold on pretty steam like this okay so it's basically get reset hard and also get clean with a bunch of flags okay so as you can see where now you know if i do log it you know the hat is over here so this is the initial commit so i'm going to open it with visual studio code and i'm going to show you what we have while it's loading we're also going to import the the build project i'm going to give you like a couple of pointers about scala but ultimately this video is not necessarily about scala uh it's not about like the build tools and anything like this but you know you're a beginner so i'll try to give you like a little bit of something so that you're not completely lost what we have over here is like is the terminal and over here is my uh file tree and as you can see i prepared two files one is called mutability and the other one is called immutability so what we're going to do is we're going to run with a typical banking account example i have two versions one is immutable version another one is immutable one and i'm pretty much going to use them just to just sort of explain to you you know the this color syntax and stuff okay in fact uh maybe i should do this and i'm gonna throw like the immutable over here and go like this and for now we're going to disable the the console like this so once the build uh tool finishes compiling uh we're also going to have a buttons over here for running and debugging but we're not interested in that at all we're using scholar 3 which has a python based indentation syntax so for example this is a class and this here is the body of the class it's indented by two spaces and there's a colon over here the syntax came in in scholar three okay so uh this is pretty much a main method and this program pretty much creates a bank account mutates it a little bit uh in fact we should probably run it so i'm going to launch sbt which is uh scholars build tool so we're going to come on there you go clean the project and we're going to run it it's going to compile it's just only two files so the compilation is going to take just a couple of seconds so we're going to run the mutable version and it presents 0 2015. so now we're going to run the immutable version it does the same thing okay so let's go through uh through the code so this is like the main method we're printing out a bunch of hyphens then we have a class called mutable bank account this is how constructors are done in scala so this field initial balance is of type integer it is not going to be accessible from the outside so in scala this is technically a private valve valves or constants okay so uh everything that is here is two things one is this is the constructor body and also this is all the members of the class which is you know functions you know methods uh writing and other fields okay we have a private variable right which means a mutable reference to an integer and in the beginning in the constructor is going to be um instantly initialize was the initial balance okay uh this is pretty much a getter for the balance right so this is just a definition this is a method without any parameter list and its default value is going to be a car and balance but this is a method right and because this variable is immutable whenever this change you know changes the the getter will give another value we have deposit and we have withdrawal uh that pretty much you know we give it the amount we're either adding it the amount to the existing current balance or we're withdrawing we we're removing it we're not worried about arrow handling or type safety or anything like this here okay over here we're creating our first mutable bank account by giving it the initial balance of zero and we are printing it out which is why we saw the zero then we're depositing 20 let's say dollars and then we're printing it out again printing out the balance right by using this getter right because it has access to the uh mutable variable every time the printout is going to be different so bringing it out and then we're always drawing five dollars and so we're left with fifteen right so over here we had zero twenty fifteen let's have a little bit more space like this okay the immutable version works pretty much the same way like most things look exactly the same so final class now this is not a private valve this is a valve which means it is accessible from the outside so we can do emittable account dot balance over here we didn't want to do this because we only wanted the getter to be visible but not the setter right only the methods inside of this class will be able to modify this current balance but on the other side you're still going to be able to see it immutability doesn't have any issues with it right so we can have this balance because it will never change right so over here we're instantiating the immutable bank account with a zero and it will be forever zero so how do we deposit or withdraw things well instead of returning unit which is something like void and java um we're going to every time we're going to change something we're actually going to create a new bank account so over here as you can see deposit creates a new bank account takes the balance of this account adds the amount that we're passing in withdraw has a minus over here right and so anytime we deposit we're actually creating a completely different bank account okay we're here withdraw completely different bank account so we kind of have like versions of the bank accounts if we stick around using those valves okay i think a debug point sneaked in somehow now this point is already very important because um immutability already seems more sensible right you kind of have this intuition that immutability is somehow better you know there's no mutable state so it's probably good if there's no mutable state then there also cannot be any shared mutable state so this is how concurrency is solved um you know usually around this stage people also talk about referential transparency but this sounds like a very fancy word so don't worry about any of that we're going to uh discover all of these things step by step and there will be um no stone left unturned right so you will know exactly what reference referential transparency is and you will know exactly why mutability makes more sense not every commit is going to um uh to be very big okay so uh let me let me uh first um see if so when i do this short thing i'm only seeing the description okay just to remind myself what it is going to be about and uh now we're actually going to see what happened there okay so as you can see this is the only thing that happened there and i'm actually going to check it out okay in fact i think what i should do is i should do this over here okay so i'm going to open the console and i believe i can do something like over here uh split terminal i'm going to use the sh okay and i know that i'm covering like some parts of this but it's not that that much over of an importance of what is written like in the in the bottom right corner so what i'm going to do is i'm going to check check out v2 so as you can see this is the only thing that changed and i simply made this change because i'm kind of like walking you through the whole the whole thing just to point out that there is always something returned because we're in scotland scala is an expression-oriented language so everything is an expression which means everything has a value and because it's a mix of functional programming and object-oriented worlds every value is also an object there are some optimizations when it comes to primitive types but none of them matter today this is just a fancy syntax for when we are not interested in specifying the name for our variable because we're not going to reference it at all right but it will become important much much later in the video why i'm pointing it out like this let's not dwell on this let's just move on so version 3 is no intermediate state okay so over here uh we have an immutable bank account but we kind of go through like intermediate states first we create like the second bank account then we create a third bank account but at the end of the day we we just wanted to create a third bank account and we kind of do like the same thing over here we're not creating intermediate states but we're going through intermediate steps okay and so all of all that this thing is showing is that um you could you know because one of the things about function programming is that usually people say oh you know it's like less code it's like you know everything is shorter it's nicer nice this is not what this is about i want i want to show you that you can do this uh both with the immutable version and the immutable version as well okay basically over here uh we're not using any intermediate states so the immutable version kind of makes sense because this is also called um fluent interfaces right because this line gives an immutable bank account on that immutable bank account you can call deposit right but what is deposit return well it returns an immutable bank account so on that immutable bank account you can call another method called withdraw and in the end of the day you're getting your third bank account but kind of have only one uh val and therefore we only you know it kind of feels like we only had only one bank account okay also usually it's one of the beginners concerned about functional programming that there is like some copying um taking place but you know most of these problems and functional programming are actually solved by a thing called persistent data structures but i'm going to talk about them today well i'm going to talk about recursion which is a common thing that functional programming tutorials talk about because ultimately function program is is not all about recursion and also especially like in real world application uh even if your entire application is written in purely functional uh style you you might not have even a single uh recursive method because all of these things are like abstracted away they're hidden behind uh you know utility methods so the immutable one kind of works the same way right so in scala we can have you know uh vowels inside of vowels we can have even classes inside of valves skull is a very regular language there are there aren't that many uh exceptions uh in terms of like syntax okay so we're creating a val bank account but there's a teeny tiny val inside of it called result uh usually it's called result or some sort of like accumulation so this is essentially a bank account then we deposit we withdraw and then at the end of the day we will return it okay so this is also one of those things that uh usually when you start out with scala people tell you don't use var var is like mutable stuff but it turns out that mutable stuff has actually nothing to do with the bars because even though this is a vowel right the immutable state is inside of this thing like typical example would be an array you can have you know val x equals you know array of one two three the array is mutable even though the reference was will always point to this array it's just that the question is what is this array the array can mutate it can change in place again the point of this whole exercise so far was to show you that it's not all about like you know the fact that the immutable code is like shorter and more pleasant to read you know it is a point but it's not like it's not what this is about okay uh let's go and see what's what's up with the next one so the version four is tap so one of the things that you learn about is uh lambdas lambdas are uh just a syntax for function literals okay so the same way as you can have a literal for a number like one two three uh you can have a literal for a function now in scala this literal is just a syntactic sugar for an anonymous class that is going to be instantiated red so it's going to turn into an object at runtime and there's a lot of like syntactic sugar around it so what i want to show you is that you can even get rid of this val result so that syntactically these two things even though one is mutable and the other one is immutable can get even closer okay so i'm going to check it out so this is v4 these two functions are present in the scala standard library in fact the next commit throws them out and uses an import okay but let me explain to you the the syntax first first of all like this is a feature in scholar three it is also possible to do this in scholar two it's just that the syntax would be slightly different and this is an extension method it's also present in other languages like c-sharp for example this extension method extends all kinds of types we're giving this type a name a which means you can stick any type into it but in this scope it is going to be known as a right and so you're going to be able to call a function called pipe or a function called tap right this one on that thing that you're calling red so you're going to be you know imagine you have something and you can do dot pipe or dot tab okay this is what the syntax allows you to do pipe is a funny thing essentially uh maybe i should give you an example so imagine that you have like two functions f and g okay and then you know g takes an a and f takes whatever you know whatever comes out of g so this is how would you you would you know how you would write this code right so you give a to g and then whatever comes out to give it to f this code will allow you to do something like uh g of a right take the result pipe the result into f okay or you could even go take the a pipe it into g and then pipe it into f okay so this is what this what this thing would allow you to do okay so as you can see it is a high order function high order functions either take other functions as parameters or they produce functions as their result or both and at the end it returns to b okay so b is over here right a little bit of generics okay so somehow we go from a to b just by calling the function okay the implementation is trivial now tab is actually what we're interested in so far right because i brought in pi because simply because it's you know usually they are presented together but we're going to use pipe only a little bit later in the video okay tab is actually very interesting because even though it looks exactly the same at the end of the day it throws out the result of b right and it just gives you that thing that you called uh called right so for example over here you know if this was like a tap g right whatever g returns which is over here would be thrown out and then a would be returned right so this pipe will still be called on the a and this is done exactly for formutable code so if you look at the bottom we're creating the multiple bank account then we tap into it this is a fancy syntax for okay bank account comes in right and now we're taking the bank account and we call deposit on it right now the result of deposit is unit right but look at this code we're throwing out this unit we don't care about this unit we return that thing that we call it on right which is this mutable bank account okay so over here this tab is still called on the bank account okay so this whole thing at the end of the day does exactly exactly what i was doing before but there is no need for this valve so this argument about immutable code like you know its last code it's not it's not that valid like most of the times yeah you can you can write very concise code however again this is not what this is really about okay i'm pretty sure that the next commit which is b5 is going to throw it out and just do an import to scholar util chaining because these two methods are so popular so common that they are you know part of the standard library okay and by the way from time to time i should actually run the code so what we can do over here is we can do you know run main and then them inside you and uh let's run let's run the immutable as you can see the result is not only only 15. uh what is the next one simple mutability challenges interesting i actually want to see what i what i did there before i presented to you uh so there will be two commits in this entire thing there are like the very crucial key points you really need to make sure that you understand them because otherwise everything else is going to be very hard to understand let me talk a little bit before i present this to you so the next commit defines functional programming it presents a model for understanding how procedures which are methods or functions are applied to arguments this model is called the substitution model for applying procedures right for procedure application so the substitution model for procedure application now this model is not how the compiler or the interpreter interprets or compiles your programs this is a model for you to think about your programs and to have certain guarantees this model should be very attractive to you because this is the simplest model of how to think about programs or pretty much any other scientific or mathematical things in fact this model is being you know was presented to you in high school right so whenever you know your teacher i always use this example whenever your teacher writes on the white board you know x equals five then no matter what happens no matter at what time you refer to x everything else on the board will still see access 5. x will never change right so again like immutability kind of like kind of like sneaks in right but we're going to define it more precisely than just saying oh okay it's never going to change let me check it out was it five or six okay we already had to five okay six okay so this is how functional programming is defined imagine you have this code and i'm going to comment out um i'm going to comment out all of this okay so this is how this program is is running in fact we don't need it as a version for now this is all that this program is doing right it prints out one and one which is a tuple okay so now imagine for a second that you have a vowel and you don't know what is on the right hand side of this vowel okay like this okay by the way this code compiles because um scala allows not only alphanumeric variable names so there is actually a definition called question mark question mark question mark and all it does is it throws a non-implemented error so we can jump in there and you're going to see that it just throws an unimplemented error okay so this is done for like prototyping code imagine that you don't know what this is which means you don't know the type of this thing okay now imagine that you wanted to create a tuple which is a data structure with without any special name right you can retrieve these values uh by calling you know underscore one and underscore two okay later if you wanted to okay this is a tuple two which is a heterogeneous collection it can uh hold two things of potentially different types in this case it's the same type okay so you're taking this a sticking it in there twice okay so now you have a tuple of ace which is why i call it t and so we're printing it out and then you know when there was one you you actually saw the top so here's the question when do these two programs behave exactly the same let's say now what is the difference between these two programs the only difference is that over here when we're constructing the tuple we are not referring to this expression but instead we're referring to its value okay remember the model is called the substitution model so we're substituting substituting the expression by its value this is also called a referential transparency right so this expression is differentially transparent if these programs behave the same okay and for simple simple values like 1 or an immutable bank account these programs will always be the same however you need to know what you're dealing with right in a mutable program you need to know what you're dealing with right if this is an array you don't know if this is a mutable bank account you don't know and here's an example right so here we're introducing side effects effects that happen on the side okay so all of these four programs are kind of the same right they all start with some something then a tuple is being created in the first version of the program the tuple has been created by referring to the expression and in the second version is by literally substitution substituting the expression with everything that is following the equal sign over here okay i'm going to remove the console so that you see the whole thing okay so literally like this copied paste it once paste it twice and a bit later we're gonna have a version of this was immutable bank account okay but for now for now it's just print lines so if we run this program now you're going to see that it actually does not behave the same what does this mean right so one program does this the other program does this this is different behavior what does this mean well it means two things first of all this expression this one well technically this one right but you know you know what i mean right this expression is not referentially transparent so referential transparency is a property of an expression and it means that the when when an expression is referentially transparent it means that the time when you refer to this expression doesn't matter right remember like if x equals five no matter if you call it in a hundred years x is still going to be five remember it's like this immutability thing right now the substitution model is a property of the entire application right so now because we have until we have this program you know excluding these print lines these print lines are just for you to to help you understand but if these print lines were not around right like these two programs right like again like forget about these print lines like real quick these two programs this entire program was not breaking the substitutional model for procedure application right so it gave you certain guarantees it allowed you to treat these expressions simply as aliases right but once we introduced this we broke the substitution model for procedure application and now if this was a huge application we would lose this ability to reason about our programs in terms of this very simple model and this is a definition of functional programming a programming is written in a functional style it's a purely functional programming if you did not break the substitution model for procedure application okay which means every expression is referentially transparent which usually means every expression is immutable there are some cabinets we'll we'll get into that okay so let's get into the next one and the next one i'm sure is going to be um by the way grhh is get reset heart rate which means that you know let's let's throw out all the changes okay so um let's do a git checkout v7 and i'm pretty sure that in v7 we're doing we're working with a mutable bank account okay now it's exactly the same thing right so we're creating something right so there's something on the right hand side of the equal sign right uh we're not doing that yet okay we're putting like two of these things into a tuple then we're doing a print one now one important thing to notice is that just because you have mutability or just because you you know you're breaking the substitution model for procedure application sometimes your program will behave just fine right so as you can see the bank accounts are not affected obviously we're seeing these print lines but at least the bank accounts are not affected and you can say oh who cares about the print lines right but sometimes it is right and this is the problem right so because sometimes with mutability you will sometimes have bugs that are very hard to reproduce because if you wanted to reproduce them you will need to talk to users and you would need to say okay well what did you do and then i said okay well i first i did this and then i did that and then did that and then you need to go to your program and you need to replicate exactly the same thing okay so here's an example right so if we if we do this right then all of a sudden not only print lines change right so this affected this this bank account and and the the second bank account was not affected okay so second second bank account is still still a one okay so now these programs don't behave the same the substitution model is broken and why is that because this expression cannot simply be treated as an alias to some value it is an alias only within some environment in fact the model that you need to apply here to to think about your programs is called the environment model for procedure application in fact these are just like the official computer science term for object orientation which is the environment model for procedure application and functional programming which is the substitution model for procedure application and i have very very old videos about this and most of the stuff comes from a wonderful book called sicp which stands for structure and interpretation of computer programs and i have a review somewhere on my channel i'm sure you can find it let me throw this out and let me demonstrate the immutable version okay so let's switch to running the immutable version so as you can see it's the same thing right so ignore the print lines right so the print lines are still messing up with the substitution model and you know the rest of the video is going to explain how to how to solve this problem okay so look at this if we enable these things the bank accounts are still not going to be affected right why because this simply creates another bank account and we're throwing it out remember this thing right so this equals sign i'm sorry this underscore here is another bank account right so if i were to give it some name uh you know my id will actually tell me well this is actually not immutable banking but we're not doing anything with it right it doesn't participate in the rest of the program which means that you know none of the stuff is is affected right like this okay so either like this or like that everything is fine okay and one important thing is that let me actually switch to the to the immutable version again it is very hard to reason about this if we go to mutability all right and we do this withdraw well actually look at this so there's like one one one one okay so over here this two string which is something that i added which you know just prints out the bank account we change it to a vowel it behaves the same we change it to a var behaves the same well it cannot be a bar okay it changes to a lazy vowel it behaves the same okay now let me change it back to def okay so this is a very interesting point right because depending on what your program is is doing you might or might not create bugs right so you have already broken the substitution model but so far it doesn't bother you but sometimes it does right so if we have this okay see we have like 0 0 1 1 right so it's already not the same but now also if you go over here and do a vowel instead of daf then stuff change so it's very very very hard to reason about programs like this okay because the substitution model is broken right but only because it's broken doesn't mean that you easily see it right all right so next one let's show v8 i believe over here i just wanted to show you the pipe because i introduced the pipe before okay and it is over here okay so this is this is going to be very no it's actually there's actually another reason why i'm introducing the pi because later we're going to learn about map and it's going to be very very similar okay so another another way like to write this would be like this right so look over here right so instead of just like doing like e dot withdraw we can do e pipe and then like this is going to be uh this is the same as okay like it's it's still it's still the same e right so this is like a little bit of a silly code but but later it's gonna it's gonna make sense okay like this all right let's uh let's have a look at the next one this one is very very important okay so remember we still need to solve the print line somehow and by the way guys don't forget to hit the like button so that more people stumble on this video really helps out the channel all right if i'm correct then this is another one of those commits that is very very important why are we having this problem with the print lines and usually the answer is because they happen too early right so they already happen and therefore you know it's very hard to to apply the substitution model for things that already they already ran they already happened you know which is why most like very very pure functional programming languages and we're going to talk about purity in just a second most of them tend to be lazy they have um they have on-demand evaluation semantics which means that this val you know this code that is inside of these curly braces is only going to be run when we're actually going to use the e okay it has different tradeoffs but like this is what it is like most programming languages allow assignment allow immutability therefore most of them are so-called eagerly evaluated programming languages or strictly value your programming language which means that if there is a vowel it's going to run immediately even if you don't use it now the question is like how do we solve this println what we need to delay it somehow right artificially delayed and the way to delete is basically to say okay well we know that when this code runs we're going to you know we're going to side effect right so let's not run this code but let's remember how to run this code okay usually when functional programming is explained the term description is used okay so let's convert this into a description of whatever happens inside of these curly braces okay and the way you do this is by saying okay well e is just going to be a function you know that you know it's not going to have any arguments right uh but when we're going to call it then you know then we're going to do the print lab and the trick is that we're never going to call it okay instead what we're going to do is we're going to so this basically delays this expression right so we're going to delay all of the expressions then we're going to figure out how to combine all of the expressions into one and so your program no matter how big and i know it's very hard to imagine imagine like a like a back-end application which like it was like thousands and thousands of lines of code all of them is are combined in such a way that the entire program is like one function call okay and then in your main method which we dramatically call the end of the world only then we're going to call this function and so until we call this function we're in this perfect world where the substitution model for procedure application applies so we can very safely reason about our code we can very safely uh refactor our code uh we don't have like weird bugs we still have some bugs right it's not bulletproof but at least we can reason about programs in the it was the substitution model for procedure application okay the next couple of commits and they're sort of like steps uh to how it's done okay so most of this stuff is done in libraries okay so what we're going to do over here is we're going to write our own library okay so it's just like over here right so it's just like another file over here so it's like in the package uh functional programming library and then over here i just kept writing everything in this file we're importing everything from this library and we're using like uh using it at the bottom okay so a function that does not contain any arguments is called a thunk okay so this is a thunk that will produce an a ignore this plus completely ignore okay so what i'm going to call this function by giving it no arguments whatsoever it is finally going to give me the a like in our example the a is actually unit because it's just going to be like some sort of print line now for convenience we're going to take this thunk and we're going to wrap it into a data structure and it's going to be called io which stands for input output right so it's for those things like okay like how do we actually send an email or how do we actually read something from the database how do we print stuff out how do we you know read you know user input in there it's just a data structure that contains one of those things right and usually it's called unsafe run because um until you run it your your entire application is save right it means that it's within the substitution model however as soon as you run it all bats are off but you're not going to run it and we're going to we're going to get to that point now this is the companion object um for for this class and if you're not familiar with scala companion object are our ways to have sort of like static methods right so we can do i o dot delay without having an instance of i o okay and that's just a helper function to uh where we can give it an a and we'll wrap this a into a thunk and it will wrap this tongue into an i o and there's a tiny trick over here this is called a by name parameter in scala right and you can think about it as if this was already a function right and so over here we're just calling it like this right so this means that um and in those circumstances is this a evaluated right it's uh you know if you give it a five this five only be evaluated at the very end of the day when somebody calls on safe run right so over here it's good this is just like syntactic sugar for what i've just shown you the value of a is not it's not known here it's just being captured in the function okay so this allows us to do like uh to go over here and whereas before we just have these curly braces now we can just say io.delay and in scala whenever a function has only one argument you can replace you know the paren which would be like in any other language right you can replace them with curly braces okay so this is a function um everything inside of these curlys is one thing and therefore we're passing this one thing to this function delay remember it's being passed by name so this print line did not happen yet okay so it goes in over here it is being stored in a lambda the lambda is being wrapped around in a data structure i o just for um just for uh for convenience okay so what happens now is that we don't have this withdrawal yet right but like what happens now is that already these two programs like these two well technically like these two right like print lines like like these print lines are just for you guys okay just to see okay but like as you can see like this program is now equivalent to this program right uh you kind of see over here like the objects are different but this is just a you know this is not this is not what this is about right obviously these are two different objects right but the program behaves exactly the same right because what are those objects well those objects are descriptions for for running computations okay so we can go over here right and write a little bit of code and say okay well i'm just a description for some for some program okay so and as you can see we just have two tuples each with with a description description description description description and there we go this is it like this is how this problem is solved it's just that now you kind of have like other problems it's like okay how do you how do you work with these things okay and this is how we're going to move on because now we cannot do this pipe right because now it's going to say well withdraw is not a member of this io thingy right and this is how we're going to go to a function called map which is going to do what pipe used to do but for these like nested things these rap things right so map will know how to go like inside of this i o and convert this value sort of like without running it it will describe how to run it right but like none of this stuff is going to run just yet right which means when we're going to call map over here it is going to produce just another i o just another description and same as with the immutable bank account we're not doing anything with it right and therefore it will it will stay uh it will stay fine okay let's see let's see what is happening in the next commit let's check out v10 v10 now basically has this this line which is just a comment right but this is separation between between the immutable pro part of the of the uh of the application right and i'm saying immutable i mean like between the part where the substitution model for procedure application is not broken and the other part okay in functional programming languages uh you you will never even have this thing for example in haskell right in haskell the main method does not return unit it returns io of unit okay and so you will construct all of your descriptions somehow and you will just give it to the runtime and it will run it so your program like 100 of your program is uh within the substitution model okay um we didn't get there just yet we're going to we're going to have conversations about this in in in just a bit this is where we let things blow up right so this is where we break the substitution model by actually calling unsafe run this is now unsafe right and usually you would have like only one call to unsafe run but over here we kind of like for now at this stage we have like what like six programs right because i wanted to show you that that all of them behave exactly the same like as you can see they all say producing one producing or producing or producing one okay now where are actually our bank accounts right because before we were printing out the bank accounts okay and i believe this happens in v11 okay we're actually surrounded this was a print line and now you see that all of these programs behave you know all of them are identical okay they first do a print line or producing one and then they're actually giving you the immutable bank account so i want to reiterate a little bit so what we did is we had side effects and we converted them into effects pure effects right so this is also the reason why io is sometimes called an effect type right but it's just a data structure just to wrap around the thunk okay let's go back let's see what i did what i did next okay and by the way we're gonna we're gonna start moving like faster because you know we have 32 commits to go through but we're already at 12. yeah so the next one is the map because we somehow need to withdraw right like how can we withdraw like the only the only way we can withdraw right now is by calling unsafe run which we which we don't want to do because like we want we want to do it here somehow and we cannot call unsafe around here because we want to be in the safe world of the substitution model okay so uh what we're going to do is we're going to define a helper method on io right that will in an immutable way call and safe run for us right but it's not going to call it right it's going to just create another io that will call it once we run the entire application okay so if we go over here it's pretty much exactly what you know what we need to do so unsafe run for example it would you know actually get the bank account right so the a is the bank account right then we will apply the transform transformation which is going to be the withdrawal something right and that's how we're going to produce the result okay so compare this with the immutable bank account right so if we go to the immutable bank account okay so something like a deposit it's a very it's the same pattern right there's some parameter goes in you know and then the first thing that we do is we're creating another data structure right another immutable bank account same thing is happening over here we have io we have a function it has some parameter the first thing that we're doing is we're creating another io okay and thus everything that is happening here is not happening yet it's just a description right because remember like this is just synthetic sugar for creating io and you know giving it a thunk okay so all of this is inside of inside of that right so this is just like a helper this code does not run yet okay so we're just giving it a function you know we have an io of a we want an io of b so we give it a function how to go from a to b okay it creates an io it calls on safe run to get you know it describes how to call on safe run to get the a out it sticks it into that this function over here right this is how we get b out and there we go we have i o of b okay and so now over here what we can do is we can map right but as you can see our programs are not affected right the programs are still the same why again because this is just another i o okay and we're not touching this other io right this is an io of unit but we're not doing anything with it so it doesn't affect our program at all it's exactly the same as what happened to a remutable bank account right so over here you know right it just creates another immutable bank account but we're not doing anything we wouldn't care about it it's a new bank account like whatever okay so let's see what happened in in 13. this is this is an important one this is what i was saying before in a in a programming language like haskell um this runtime that knows how to run this description is built into the language in scholar we don't have that and in fact this is one of the biggest reasons why i'm making this video is because we have competing libraries that try to do that what these libraries do is they create a they create a trade usually called like fp app or something right so i put it like in the fp library it looks like this in fact like there's some there's a lot of like mechanical stuff that i would need to explain to you about how this works but essentially it's just a it's just like a template class where you implement the run function which you know you give it an io of any and all it will do is it will call unsafe run for you okay so essentially what happened over here is that i removed this like daf the whole thing now became an object and now i do like extends fb app okay and now somewhere at the bottom i'm going to say well run is going to be this one delay right so we're giving it like one right so technically like this line is not correct anymore because uh because this is still like one description okay so we're giving this description to the runtime and it calls this one function right like our entire application became this one function right over here this is our entire application and uh the runtime simply runs it i also put uh some green color around it like this okay and uh this is why the whole thing uh looks green okay but now like imagine that this library is not you know it's just on our class path right we haven't written it okay and therefore our entire program is purely functional this is what it means to appear the function right the substitution model is not broken anywhere we can reason about it in a beautiful way okay so it doesn't really matter how exactly this stuff works because again there are some mechanical things like app is a very special trait which uses some delayed initialization techniques um so it doesn't it needs to be like an abstract class if it's a trade you know then it's not going to work you know but but all of these things are like just you know some very specific scholar things they they have nothing to do with uh was what this is all about right so just a template we implement run and it just calls unsafe run right so the unsafe code is not in your application it's in that library so you don't need to worry about your program is 100 purely functional which does not mean that you cannot have bugs right none of this stuff is bulletproof let's see what i did next because i believe i believe we're going to start talking about mod ads uh this one is just uh like usually you would have like a distinction between like your program right and and like this part that actually does you know the extends app and you know and run okay so um this whole thing now became an object right it kind of like ends over here right you can do like and program right and like it has a value which is our you know one single io delay uh was like this is like basically the our program and uh you know we're just doing like this one call over here like we don't even have this one called what we call unsafe run the library does it for us okay but this is pretty much like the pattern right so you have like your main is that it's going to look like this okay this is just a reflector okay so the next one is a path to modis right mods really big word and functional programming a monot is just a mechanism to chain these descriptions right so the same as before we couldn't call pipe we needed map now we're going to realize that map is not enough we need a slightly bigger boat to compose these okay so what what we don't want is we don't want to have like one io you know one description and then the description to be gigantic and the second one is that what we don't want is we don't want to call and save run okay even though it's within i o and everything is kind of like fine we really don't want to have that because this means that inside like everything that is marked currently marks like everything inside of these curly braces within this even though tiny piece we cannot apply the substitution model and we don't want that in fact some really advanced cases actually require you to do this but you know we're not going to do any any of this stuff over here okay so um one thing that you always hear about functional program programming is that you can create like very very small things and then combine them this is the opposite right we have like one very very big thing so what we want to have is we want to split these things into many like small ios and find a way how to um how to combine them right how to chain them how to compose them this is like the word that is uh you know usually used okay we didn't check out 15 okay so let me see what i did there so the first one like it's pretty much a refactoring right it's a refactoring to um to just explain right so like this is like the old value now and this is like the new value okay so it's still an io still delay it's exactly the same thing as before right over here right um but now we try to use first of all we have like these valves right just to remind you that everything has a value and then we try to remove unsafe run right and we successfully removed unsafe run so instead of unsafe run right so instead of you know for example this one right like uh print line and we give it t e which is a tuple of ease and we're getting the first value out and we're calling it safe run instead what we do is we're getting the first value sort of like pipe remember like pipe print line we know the pipe doesn't work but we know that we have maps we tried with map and we successfully removed unsafe run however our program does not behave the same as it was before so we see that map is somehow not enough okay because what is happening over here is that e is some sort of i o right it's an i o mutable account okay when you call map well you're converting this io into io of unit okay well great now this underscore is io but you're not doing anything with it right so map calls unsafe run but we need to call and say fran again right to call this description right we need to call in safe run and we need to tell it that after it runs it also needs to do the next thing which is like this print line and then do the same thing with the next one and then do the same thing with the next one okay uh let me see if we're doing this already in the next one uh well not like this right like this no okay we're not okay so the first thing that we're that that we're doing is we're going to try to chain them right to connect them with map so i believe what i did was um is i tried i took like i believe these three yeah so as you can see t1 map print line right and then i tried to like take these three out i put them like before here and i wanted to like experiment with it a little bit okay so if we check it out okay so i commented it out and i said okay so let's try to use map let's see if map is enough and it looks like like it's it's almost enough right so te1 says okay producing one and then immutable bank account because of this map print line great okay then we do map bring line these underscores great this works but then we do map te2 and here we're lost again right because like remember we cannot do this right like we cannot do this right we're not allowed to call on safe run okay so and then we're kind of lost we're we're in this we're in this description world again right so we're like over here again override easy valve to string which is a string equals description right so we need to call like unsafe run again like this is the trick we need to call in safe run twice somehow okay i'm pretty sure that the the next one you know uh the version 17 is just to explain the ad expansion over here so the sprint line is basically okay the bank account comes in and we do a print line of the bank account right so as you can see they're all the same okay now but they're not all the same right because like print line is something that is already happening right it gives you like a unit right but over here like this is an io of unit right and so we need to continue right let's see the version 18. so the first one is okay let's let's let's level the plane field let's make them all equal okay so we can't make them all like we can either go like down right so we can call like unsafe run okay now they're all like sort of like executed right print lines is executed and safe running okay but we actually want to go the other way around okay we actually want to want to delay the print lines okay so we check out 18 we're going to delay the print lines okay and now all we need to do is we need to create another combinator instead of map we're going to call it flat map okay because now what is happening is that we have io of io so we need a map that will then flatten the ios right how will it do well it will call and say run again right so if we go to v19 right you're going to see that we're calling flat map now right so everything is io here right and flat map does exactly what we need and flat map looks exactly like map right see the only difference is that like this thing is an io right before it was a b which in our his was like a unit most of the time but now this is an io and the implementation is almost the same right so first we're getting the a out but then when we call the function we don't get the b we get the i o of b and so we call on safe run again and this is how we get our b okay and so again we went from i of a to i o b but this time was a different function and you know hand wavy way to explain monas is that monas is like that thing that has a flat map pedantically there are also laws and stuff like this i haven't had playlists about mona so if you want you can check it out okay but for now a monitor is something that has a flatmap let's continue it's like i believe from here everything is fast 20 is the one that okay so i did it i did a kind of like a big jump over here okay so now we can see how we use like flat map lab map lab map platinum flat map uh scholar has a construct called four comprehensions which allows us to um basically maintain this code like almost as is okay it's just that these valves will go away these equal signs are going to look like this okay and over here remember this is going to be like delay print line right and this thing over here right it's going to have a four in scholar two that will be also in curly braces in scholar three it's not required and then over here in scholar two it will be closing curly brace and then yield in our case what we're yielding is a unit right because this was an io of unit okay it's called three we don't need this curly brace okay so i'm gonna do thro throw out what i just did and check out we v20 and that's exactly what i just explained okay so instead of like doing it yourself flat my flap my flat map and just for comprehension technically it's not like precisely the same because these are nested flat maps what we had before was like chained flat maps okay and this is something has to do with this magnetic laws right this is the associativity law it says that they should be the same like whether you nest things or whether you chain things it should behave exactly the same it's also why it's very important to test laws when you create your own instances of modes but usually you don't do this because usually libraries do this for you okay now there's some style there's still some discrepancy right we still have like a map over here remember we didn't we didn't remove it but as you can see as you can see it already works okay so i believe the next steps are going to be to do you know flat map and then delay the print line and so on let me just check out v21 yeah so this is like the next step right so we're going to flat map and then we're going to have a b arrow to the right i o delay print line b okay and the next one is going to be um an introduction of our own console that is going to do this whole delaying for us because what we want to do is we want to have add expansion right we want to what we want to have is we want to have flat map and then our own you know i believe i call it fpconsole.println because over here we cannot we cannot use that expansion the next one is going to be 22 right which has this yeah it's pretty much exactly what i said and if p console is just a teeny tiny object it has print line every line okay print line has a delayed you know by name parameter a and all it does is io delay the regular print line so this is the regular scala this is the print line that we have been doing all along it's just that uh i cannot do it like this because otherwise it would like refer to itself so i need to do a console.println and the readline is pretty much exactly the same it's like the opposite it just delays you know the read line from the standard library therefore it gives you an io of string okay and there we go okay and the next one pretty much like rewrites you know the rest and it also shows you that because we're dealing with descriptions we can store a description in a valve you know with these hyphens and we have this forward comprehension which says okay we'll do hyphens first you know then take a bank account flat map to print line and do the hyphens now but now we also have like flat map inside of a full comprehension so we don't really need this right so i'm going to rewrite one of them uh by hand and then i'm going to check out the next commit and it's going to do this for for the other ones right so basically we could say okay well get the bank account from the from the e which is a description of how to get a bank account okay and in the next step i just do fb console print line b okay because like this thing essentially is a flatmap right so it's going to look like this it's literally the same code as you can see behaves exactly the same okay so let me do this oops v24 i'm going to check out v24 there you go this is exactly what i just said okay get the first bank account print it out get the second thing again print that okay right it's exactly the same program right so this is like a huge full comprehension and by the way when you're when you're dealing with um with a real functional programming code basis very often you would see like this huge functional programming uh this huge four comprehension i really recommend you not to do this right because now you're in the functional world you can extract every little thing into its own valley into its own depth please do this okay let me see the next one so basically we're going to instead of having this program that does like all of these things we're going to go back to the original program that we have written remember the one that starts with a bank account like twenty dollars uh uh withdrawals like five i believe right it's just written in this functional style okay and this is for the mutable and for the immutable one okay so now like now like this is our program okay as you can see like at the end of the day this program looks pretty much the same as it always look like it's just that now it's inside of the full comprehension okay so if we go to like check out the one as you can see it was well maybe not v1 hold on uh maybe like um v3 [Music] maybe v4 yeah like this one see it was like print stuff out create this backup you know bank account was like zero and then deposit twenty withdraw was zero five and then another another print you know print out the balance and println 50. okay so this is this is now how it looks like in a functional world okay println 50 create the the um uh create a mutable account was it was zero deposit 20 withdrawal five print line the balance you know and ended on the print line okay and in an immutable world looks exactly the same it's just that over here you don't even need to do these delays because everything is immutable anyway okay so inside of a full comprehension you can also do equal sign like this and go like go like that okay like this now in a mutable world you could do the same thing and it would actually not not like most of the times you will not break anything but there's a super subtle difference and for like you know 100 purity's sake you should not do this and i'm going to show you why um a couple of comments from them maybe even in the next one so what the next one uh shows you is that uh we're going to remove this thing and we're going to run our program still ourselves right by calling like unsafe run and what we're going to do is we're going to show that over here we're calculating the description right we're always calculating the description during the calculation we should not initialize an immutable state okay we should also not mutate anything during the initialization okay so over here when this class has been created uh there is some mutable state that has been created this current balance is a var okay so if you were to put a print line somewhere over here well let me check out v26 okay so this it puts a print line over there okay and now this demonstrates the substitution again okay so i'm going to comment this out i'm going to yank until the end okay and i'm going to put it like see as you can see like there's no delay here anymore there's only like equal signs okay so if you have this program right notice that over here we're not running it okay so if we do like unsafe run now we're running it okay but we're not running it right we're just saying okay just give me the description the thing is that like uh objects in scholar are uh lazy which means that as long as like you don't touch them like even though this is a val it's not going to be evaluated this is exactly why i'm accessing it i'm forcing the evaluation okay but this is just a description right like this is just i o of units so nothing should happen however if you are not careful with the immutable state even though you have io you're going to run into issues right because again this is a substitution model right so imagine that there is this vowel and instead of using it over here right like this right if you do it like this right so sort of like the opposite of substitution right so instead of using the value we're actually using the the reference right so now as you can see we're sending rockets to space even though when we have we didn't even run the program yet it just said during the creation of the description we accidentally send rockets to space okay like this okay so be careful right like avoid mutable state like a plate so the next one is going to be like let's forget about bank accounts let's let's do a functional programming hello world okay get checkout 27. and i believe it's actually in a different file so this is the functional programming hello world okay so it um we need to switch to running it we're going to run it only once right like like this mean okay so it just says okay what's your name my name is blab hello black okay so this is the functional programming hello world this is how it looks like first we're calculating the description right so this is the io of unit by using full comprehension print line print line read line print line print line and then we're giving this program to run okay this is a functional programming hello world now i believe the next one requires a lot of explanation so this is the path to tagless file there is a technique called tagless final originally it was created to solve the so-called expression problem right it was basically how can we add behavior or add new data types to our program without recompiling the existing ones okay this was the premise of the expression problem it's called the expression problem because the original example was um a data structure which represented like mathematical expressions you know like adding two things together subtracting the things together and so now in the scala world we really we really adopted this um uh pattern uh it's called tagless final i have an entire playlist about it you might want to check it out however we applied it to a very very very small problem um well it was it was big for us but we applied it to like a very very narrow problem okay we did not care about the solution to the expression problem well kinda did right it's all related okay so i already mentioned this this um this problem right scala is not a purely functional programming language therefore this effect type io is not built into the standard library as it as it is in haskell and so we have competing libraries to uh to provide these effect types and the sky community is very small a lot of projects are being maintained by like one guy or like a very small group of people there is okay there are always like occasional community contributions but it's not something that you can like you know rely on as a company and so what happened over time is that we kind of took tegla's final as a mechanism to abstract away which of these libraries we take and therefore it's sort of like defensive programming if one of the libraries stops being maintained we can very easily switch to another one because the way it works is that we abstract everything that we need from these effect types and only in the main method we say which of these effect types we need this pattern kind of dominated the functional programming ecosystem within scala for the last couple of years and now it's slowly fading away it's still debatable it's still very negotiable like which way is better to develop applications in my opinion it's just not worth the trouble i've been using it for a couple of years and i concluded that it's not worth the trouble so it's uh much easier to use an io type directly um and we're gonna get to this okay so let me see uh what i did in the in the first one right so this one 28. if you look at this over here like what are we doing okay so we are calculating the description of the pro of a program but specifically for this program like which parts of io do we need right and it turns out well we need that part that knows how to print stuff we need to know we need to have that part that knows how to read stuff and we also need that part that does the flat map right which is like the monad part what we did is we created type classes which is a pattern that now i'm realizing takes a little bit of time to explain so let's run through the connection real quick every program well not every program like every program of any like relatively decent size requires two kinds of polymorphism one is called parametric polymorphism think of something like a list parametric polymorphism means that your lists can have many shapes but independently of which shape they will be in they won't change their behavior right think about list of strings list of ins list of dogs no matter which of these lists you have the method size on the list behaves exactly the same right this is called parametric polymorphic independent of which which shape the behavior is still the same the other kind of polymorphism is called ad-hoc polymorphism and it works the opposite way right if the shape is different then the behavior is different think of this typical you know animal dog example right so you have animal the animal can eat or something and now you have a dog which is a slightly different shape of of an animal right and it can eat in a different way right because the type is different the shape of the type is different it behaves different now functional programming languages they don't have subtypes and so the pattern that they use is called type classes and functional in in functional programming languages like haskell uh this is built in into the language there's like a keyword type class uh in scala we mimic type classes uh we mimic them with implicits in scholar three it's not very visible because we kind of like moved away from implicits and we also gave like specific keywords for uh for something like type classes but also not directly we still don't have a we still don't have a keyword for type class and in scholar 3 syntactically you won't even see the difference in usage right in the declaration there will be but in use that you won't see okay so what the next thing is you know what the next commit is explaining is that in an object-oriented way to look at things io is a mona however in a functional programming way i o has a moment okay so the next thing i'm going to show you requires a lot of like scala mechanics okay so at the bottom we have two traits uh actually three traits right there's also one for the console actually i jumped like too far i think i wanted to jump to 28 first so we have like the functor and we have the moderate and monet extends the factor okay and basically like we in a hand wave away we said okay monitors everything it has a flat map okay so this is how mona looks like well it pretty much has a flat map right but there's no like i o in sight also there's like no thing inside that you're going to call flat map on and the way this works is that like whatever extends this trade is going to uh have flatmap as an extension method okay so we're working with like parametric types over here okay so we're saying okay so there will be a monad for some type f which is of a higher client right so there will be something inside of this app like f could be a list f could be an option but in our case it's going to be i o okay and because of this we're going to be able to call platinum and so like the definition of map and flatmap it stayed exactly the same it's just that instead of calling unsafe run so instead of calling it like this on saferon you know we're calling iota and saferun and io comes from this like extension thing syntax okay and so effectively io becomes a simple data structure okay but there's a type class called monad somewhere which extends the functor which isn't limiter and over here there is an instance of this type class so given an instance of the tab class if it isn't scoped this call compile will actually inject it for us there will be extensions extension methods on i o which are like map and flatmap right so i'm actually going to show you this so if you look at v28 right so the functional programming library cl like file changed right was everything that i just explained but nothing else changed right so this is what i meant like syntactically like the on on the usage side you wouldn't even notice a difference right so for example over here you know the full comprehension still works because it still looks like as if i o simply had a method called flatmap okay so as a user you don't even see whether type classes were used or whether subtype polymorphism was used but on the declaration side there is a little bit of like scholarly mechanics okay so what this allows us to do which i haven't done yet let me check out 29 but i actually don't want you to see this file yet okay a function programming library okay so checking out 29 and in 29 we also have another type class for console right so it pretty much looks exactly like this object it's just that the methods are abstract and it's not hard coded to io it's hardcoded to some f right and so the result is like an f of unit instead of iof unit here is f of b instead of i o of b this is kind of hard to explain like this is another instance right so now it's an instance of a different type class it's a it's an instance of a fee console of io okay this is it's a very subtle thing by the way these instances they need to hard code for which type right and this is how you can do uh composition how you can favor composition over inheritance in scholar three right um basically you say okay this instance is going to have like exactly these things okay so basically like this export thing is doing exactly this right like what just happened is exactly the same code right it just said i didn't need to do that i could just say import everything that is inside of this object export everything okay exactly the same code okay so now that we have these pieces in scope we can say we can write our program in such a way that it doesn't depend on io but it depends only on these pieces and the trick is that these pieces are in a separate library right it's going to be the library that provides these type classes okay so in our main method the program is now like there's no i o inside okay if you look for io there's there's no i o in this file okay so it's going to be a program for some very abstract f and assuming that there is an instance of the monotype class for this app and there is an instance of the fp console type class for this f which is going to be io then somehow we're going to do something and we're going to produce an alpha unit right and so the program stays exactly the same well because the methods and those type classes are called exactly the same okay but now there is no i o inside and therefore at the very end over here type inference help us out but essentially the compiler inserts i over here this is the only place where we mention i o and therefore we can switch between those libraries assuming that we have some other libraries that bring in the instances of those type classes okay and so what happened in skull is that we had a library called moniks which was a you know which had a first mover advantage it came in with uh i believe it's like uh io dot monics i believe you tell your harmonix uh eval task i don't have it in the class pass but it would compile with like certain things it would compile okay then after this we had a library called cat's effect right would work exactly the same way right and all these libraries they come with like a similar looking trade thingy here you know and cat's effect is a little bit confusing because cats effect has io it has instances for the type classes for io but cat's effect is also that library that defines those type classes right that moniks implements and that the next library that i'm going to introduce you to is also going to implement okay and the last one is called zeo right so this would be like zero dot uh i believe it's called io or maybe it's called task i don't remember that's not the point one other thing that this tecla spinal technique uh provides is uh what is called uh tracking in particular in this case we're tracking the effect type so it's called effect tracking okay so before when this was io you couldn't kind of like know what the program is doing what is it what is it describing it could be a description of sending rockets to space or it could be a description of simply printing stuff out you would only know this once you know after you looked at the implementation in this case the implementation is just a couple of lines so it's not a big deal but imagine if this was like a huge method okay with this you kind of see what it has access to you see okay like it's going to be some abstract app but all it will be able to do is whatever mona you know allows it to do and whatever the console allows it to do so it kind of gives you like this um a little bit of a false confidence about about your code and in practice it turned out not to be that valuable and by the way this is just my opinion right because what happened in practice is that most of the time like all of your apps would require a monad uh and also a capability to uh throw or handle errors right in cat's effect it's called monitor throw okay and so most of the times like at least this effect tracking portion was not was not valuable enough to justify uh rewriting the code like this um and i should actually show you a couple of examples i believe okay so let's actually move on we're almost done right we're 20 29 and there are only 32 okay so i believe the next one shows just a couple of syntactic things over here so if you notice uh we're not actually using the m anywhere right but the compiler knows that it's there because of this using thing okay and so uh this one we do use and there's a way to save a couple of key strokes right and the way this works is that by uh by splitting these parameter lists okay so we if we check out the v30 you're going to see that now that these parameter lists are split in this one you can have you can mention the type classes that you're not going to need to reference by name but in this one you're actually going to require the names okay and we also have another syntax called context bounds where we basically can say okay well f is not a mono but f has a moment right and the way it works is that we have like a column over here and we do like monitor over here it's a little bit shorter because we don't need to mention this f okay so i remove this and i check out um 31 and this is exactly how it looks like okay so usually like in most cases you would see like okay it needs a monot and you know it needs an fb console and so on and so on uh however like in this particular case because we actually need the name it's more convenient to do it like this okay another way to do this would be um to like remove this and to have like a valve over here fpc and he would say well could you please summon the instance of the fp console fp console for f can you please summon this and summon is just a function in this calisthenic library and it looks like this i'm going to call it find or something like this right so find some a right and it's going to be like a using of this a um like this right and it will return this a right how's it going to do this whole like this okay so this thing works exactly the same way right so i can put find over here right it's going to work exactly the same thing right fpc is like fb console of f right so basically the compiler will inject this a and we'll just give it back so if we go back to summon if we jump in there you're going to see that let's see i can't find it but this is how it looks like okay go back to my code we're pretty much at the end the very last uh commit is going to show um how these libraries are actually used okay so in in the build file um i went and i went over here and i added zeo and i added cats effect monics is not published yet zeo interrupt for cats has not published yet in fact maybe it actually is published uh already uh let's actually check this right now because like some of this stuff is very new because we're i'm using like a cutting edge version of scala right so it's like the latest color 3 version 02 just came out and um let's see so zeo interrupt cats like as you can see this one just came out three days ago and they recently switched to an epoch kind of like release so you have like major minor patch over here but over here you have like the first two stands for zero two and the second two stats for cats effect two and we need two three four zero two and cats effect three which is not published yet but i'm sure it will be in just a couple of days this is also like one of those things that like uh tagless final technically protects you from your effect library not being maintained anymore however you might also run into this situation where you know the interrupt library is not maintained like as fast at least okay it's not exactly the same thing but you know it's getting there now like in the main method um i have like a couple of versions uh over here right so this is like our version it's called you can define packages inside of other packages okay so this is still our version still looks exactly the same okay and this is our main you know and stuff this is the zeo version you're going to see that it looks pretty much the same same way just the import is different uh the console is like somewhere else the method is called slightly differently you know print line print line redline blah blah blah the main looks pretty much the same it's just that we're extending that thing from the library inside of ours this is the cat's effect 3 version you know we import cat's effect star there's a slightly different way to obtain the console but however the you know saying goes you know printer line println redline blah blah right so this is now it's kind of hard to read but essentially it's io unit slightly different trait you know name slightly differently but behaves exactly the same you know io simple and this is a mix like this is a tagless final approach but we cannot inject like any other effect type because the libraries are not published yet so the only effect type that we're going to inject is going to be still cats effect okay so as you can see we need like more imports we need cats because it brings the monotype class we need cat's effect which brings the console type class we need the syntax so that we can do the dot flat map and in the end of the day the program looks pretty much the same right so yeah these are all the versions um this entire repository is going to be available on github and you can check it out at your own pace let me finish this video by saying a couple of words now which one should you choose uh when cal is called we have uh basically either three options right either monics gets effect or zeo or you can go with tagless final and then at the end of the world you can decide which of the effect types you're going to choose okay now moniks has the first mover advantage it's been around since 2014 15 maybe even 13. um um it's a very good library in fact we still use it at work uh the issue with it is that the bus factor is very scary okay but it's maintained by a very small uh part of the scholar community uh in fact recently the main maintainer uh had some personal issues in his life uh so it wasn't maintained for a couple of months um or maybe like even half a year luckily he came back so it's still still alive it's not that or anything like this um cat's effect is a battle tested library uh especially cats effect 2 because very recently gets effect 3 came out and you know only the time will tell how it behaves you cannot go wrong with cat's effect 3. zeo is probably like the youngest one of them however the youngest also means a couple of years um just a couple of weeks ago co2 came out and again only the time will tell how good it is uh zu is slightly different from the other two in how it handles errors and also uh it has an optional way to do dependency injection differently but you don't you don't need to use it tagless final it's kind of fading away at least in my opinion because in my opinion uh it's not really it's not really worth it because like the big the really big fear was that you know some of these libraries are going to die not going to be maintained anymore um i'm betting on zeo it doesn't look like it's going to disappear anytime soon uh you know in the previous video that i made was for season scholar developers and over there i pretty much said that you know zeus is winning uh but it's not like you know it was not an easy fight and it's not an easy decision to make whether to use z or cat's effect or tagless final however i would still choose zeo because taglis final to me personally is just not worth it however you are a beginner and as a beginner this decision is much easier zeo is much easier to learn it's more beginner friendly even if you just look at what we just did you know the hello world even just a hello world uh in uh zeel uh which is uh over here uh is the most pleasant one out of out of the three okay so it's more pleasant than the cat's effect because uh you have to obtain the console in some weird way and it's more pleasant than tyler's final because you need like extra imports you need to understand like type classes and all this relatively heavy scala syntax and intrinsic details uh but yeah but this is mostly uh just a personal preference like in the previous video for example i said let you know if you already have a big code page code based integrals final you're not going to gain too much you know from rewriting it to a concrete effect type like xeo for example but if you're starting fresh i would start with you all right now as already mentioned this repo is going to be on github and also if this was too fast for you i have videos about all of these things i have a playlist about uh tagless final all of them are here on youtube free playlist so i have a playlist about taylor's final i have a playlist about zeo i even have a series about git i have videos about you know zsh where i use like all of these alias i have videos about all of these kinds of things right so don't forget to subscribe even you know hit the bell if you want to be annoyed by the notifications and you know if you stuck around for so long you obviously like the video so please hit the like button all right i hope you enjoyed this video i see you in the next one and for now as always it's been black from damincei.com don't forget to like this video if you did subscribe if you're serious about improving the developer inside you and if you have the means consider support me on github sponsors or patreon and sometimes even watch my videos before everyone else and most importantly take care

Video description

FP in Scala in one video: immutability, lambdas, monads, tagless-final (TF), effect types and effect type libraries like monix, cats-effect and ZIO. It's a crash so we will move fast and won't dive into recursion this time since it's rarely used in production applications. This is a follow-up to this video, which is for experienced Scala devs: https://youtu.be/RaQnGCGmWI4 Watch my videos before everyone else! https://www.youtube.com/channel/UCSBUwLT9zXhUalKfJrc2q2A/join The code is here: https://github.com/devinsideyou/fp Table of Contents: 00:00:00 Intro 00:03:09 What is FP? 00:04:38 Project Setup 00:07:59 v01 - Initial commit - mutable and immutable bank account 00:11:43 v02 - val _ 00:13:17 v03 - no intermediate state 00:16:22 v04 - tap 00:20:21 v05 - scala.util.chaining 00:20:51 v06 - IMPORTANT | simple mutability challenges 00:27:47 v07 - simple mutability challenges for bank accounts 00:32:17 v08 - pipe 00:33:13 v09 - IMPORTANT | descriptions of programs 00:41:03 v10 - solving the println problem 00:42:20 v11 - getting closer to solving the BankAccount problem 00:42:57 v12 - solving the BankAccount problem 00:45:44 v13 - IMPORTANT | FPApp 00:48:20 v14 - Refactoring 00:49:08 v15 - IMPORTANT | path to Monads - Part 1 00:52:04 v16 - path to Monads - Part 2 00:53:30 v17 - path to Monads - Part 3 00:53:56 v18 - path to Monads - Part 4 00:54:47 v19 - path to Monads - Part 5 00:55:43 v20 - path to Monads - Part 6 00:57:26 v21 - path to Monads - Part 7 00:57:36 v22 - path to Monads - Part 8 00:58:31 v23 - path to Monads - Part 9 00:59:27 v24 - path to Monads - Part 10 01:00:00 v25 - path to Monads - Part 11 01:01:58 v26 - mutable state must be delayed 01:04:15 v27 - fp hello world 01:05:04 v28 - IMPORTANT | path to Tagless-Final - Part 1 01:12:44 v29 - path to Tagless-Final - Part 2 01:17:56 v30 - path to Tagless-Final - Part 3 01:18:36 v31 - path to Tagless-Final - Part 4 01:20:18 v33 - lib-examples 01:23:18 - TF vs monix | cats-effect | ZIO Website: https://devinsideyou.com Patreon: https://patreon.devinsideyou.com Discord: https://discord.devinsideyou.com Telegram: https://telegram.devinsideyou.com Twitter: https://twitter.devinsideyou.com GitHub: https://github.devinsideyou.com LBRY: https://lbry.devinsideyou.com Install scripts: https://install.devinsideyou.com Giter8 templates: https://g8.devinsideyou.com Spotify concentration playlist: https://code.radio.devinsideyou.com Cheers and Happy Coding! #scala #functionalprogramming #monad

© 2026 GrayBeam Technology Privacy v0.1.0 · ac93850 · 2026-04-03 22:43 UTC