Nice one thanks... But one remark, there is not truly currying, although partial application and currying seems like sharing the same concept but it's not quiet the same. Also due to THIS in JS (credits to Kyle Simpson) there is straightforward algorithm to know what THIS is, just check how function is called through 4 steps: 1. is function called with new 2. bind, call, apply 3. is function was called as 'method' of an object 4. default THIS (window, global...)
@funfunfunction7 жыл бұрын
Can you please explain why you do not consider this example currying? About this: Yeah, like I say in the video, it's not exactly rocket surgery, but in the functional example, it's just one step to figure out what it refers to.
@leepowelldev7 жыл бұрын
I tend to agree, I would place this more under partial application (through closures) as opposed to currying. Currying from my limited knowledge is "the process of taking a function with n arguments and transforming it into n functions that each take a single argument. It essentially creates a chain of partially applied functions that eventually resolves with a value." (medium.com/@kevincennis/currying-in-javascript-c66080543528) and it's this resolved value that is the important bit - a bit like a Promise in that if you called the resolved curried function at a later date you'd get the same value back.
@maksymlysenko37617 жыл бұрын
Seems like this exact example without context (only code) can be considered as currying, but contextually from my perspective 'closuring' connection object is more to partial application.
@funfunfunction7 жыл бұрын
+Lee Powell but according to your definition, how is the example not exactly that? It seems to me that it perfectly fits your description of a curried function?
@publishyours7 жыл бұрын
I may be completely wrong but here's how I see some difference between the two. Consider a function that takes 4 arguments: partial(arg1)(arg2) would return a function that takes one argument (arg3) and then return another function to take the last arg4 curried(arg1, arg2) would return a function that takes one or two arguments. I'd consider both these examples to be "partial application", but would only say that the second one is curried.
@johnloven7 жыл бұрын
I knew all of this before, but I've never had it explained to me in a way this understandable. When explaining the differences between OOP and functional programming, this example is just spot on. Great job Mattias! I guess I'll stay up and re-write my javascript http client class now using ~10 private members...
@egotrip31637 жыл бұрын
You should definitely do course on Udemy. With your vigor and unaffected sense of humour, you are looking at the gold mine! Intro is great!
@inaccessiblecardinal93526 жыл бұрын
Great video. At my work we have a JavaScript/Java stack, and a lot of the Java side is incumbered by tons of legacy crap code, bad family trees of classes..and then stuff you can't even explain with a straight face. I've worked both sides, and js patterns win... every time. I use techniques like this all the time on the front end, and it's almost always smoother sailing. It doesn't even feel like a "school book design pattern", it's just a sensible way to use the language.
@falconmick7 жыл бұрын
For part 2: I'm rather fond of currying a function that creates const funcs inside of it which I then expose via a object. Basically let's you have control over scope exposure leading to private functions and variables and also lets you combine functionality via composition and then expose via a simple interface
@rothbardfreedom7 жыл бұрын
21:17 - Only reasonable and necessary argument. Point made.
@spartanA017 жыл бұрын
I lol'd at "It's not Rocket Surgery"
@TehMapleDude7 жыл бұрын
Same xD
@KiffinGish7 жыл бұрын
Perfect background music improves programming velocity!
@TheOlian047 жыл бұрын
Like the new intro. It has some thump to it :P
@thiagovilla9707 жыл бұрын
Hey MPJ! I just came across your channel a few days ago, and I'm hooked! I've only seen about 10 of your videos (including your very first one) and, man, you've come a long way, right? I'll definitely support you on Patreon as soon as I can, not only because I really enjoy your channel from a personal standpoint, but also because I empathize with and strongly support the idea of keeping the content free for EVERYONE! I'm new to JS programming, I mean after a hiatus of 8 years. I stopped programming in 2008-2009, and am only now catching up to this marvelous new world of stuff like Node and ES6. To that, you've been a really good teacher. Thank you for that. Kudos from Brazil and keep up the good work. Btw, do you shoot at 30 fps? Ask that cuz your videos' framerate seem really smooth.
@YazanAlaboudi7 жыл бұрын
This was not rocket surgery! Thank you.
@alexschin98877 жыл бұрын
There's also this interesting thing with "this" I ran into, today. I had a chain of functions (also methods) like so: o.modify3(o.modify2(o.modify1(data))) I didn't like this as both he the name of the object (here "o") and the name of the method were extra long and I wanted to make this look nicer by putting it into a functor and map-ing over it, but we didn't have any library for that so I used the array: [data].map(o.modify1) .map(o.modify2) .map(o.modify3) and it also blew up in my face, because apparently, "this" is undefined inside the map unless you pass it as an argument to the map (but that defies the purpose, I wanted to make the code shorter and clearer). I wish classes were never added to js. Now it seems like it's the preferred way of doing things. I've seen many people trying to use the class just because it's part of the ES6 (newer === better). Functions are the way to go imo, and if you need objects, just create them with functions. :)
@man_kind7 жыл бұрын
CLIFFHANGER!!!! :) Great video MPJ!!! This is great stuff, an example that's relevant, with a topic that's useful! A++
@kl1tU57 жыл бұрын
hi! im kind of new in full javascript development (use to code in PHP), the videos of functional programming and dependency injection result very usefull for me. I have a doubt for an approach of data validation using functional programming and dependency injection, is a good practice if i pass the validator as a dependency of my function? how do you do your data validations with a functional approach?
@shivapandey047 жыл бұрын
Hello MPJ, Your videos have been so helpful, thank you. I wish you could make a video I think all the programmers face at some point. I see there is no proper guidance about steps to learn programming from beginner to expert. ( specially when you are trying to learn by yourself). For absolute beginner it could be like choose a programming language, learn basic syntax, play around with basic concepts like loop, conditional statements, functions etc. After that work on logic building. Simple programs like prime no, fibonacci series etc. But after you have passed this stages and you can build softwares at professional level. Now you feel like you know a lot, but you don't know enough sort of plateau region. How do we progress from here. What to learn now? Some open source projects seem so complex to understand even if you want to contribute and for those projects where you can contribute they are absolute mess. Also, how do you manage to improve your skills when you are working full time and freelancing. You don't have much time to work on yourself? It would be helpful, if you could help out on this one.
@jbellero7 жыл бұрын
I love the new intro MpJ. Awesome!!
@ericshawmusicchannel7 жыл бұрын
Great video, love those sped up coding sessions. They really disclose how everything comes about. AND, great choice of electro swing bits in there. Any chance we could get a playlist?
@Sergejack7 жыл бұрын
“A closure is a poor man’s object; an object is a poor man’s closure” Anyway, I hoped the video would (also ?) be about Ioc Containers.
@ChristopherOkhravi7 жыл бұрын
Went to the comment section to post exactly this quote :) I mean if closures are the FP equivalent of objects then a function returning a function ought to be the FP equivalent of OOP DI? Ps mpj the Patreon portion in the middle was absolutely hilarious :D :D :D
@suminshakya50674 жыл бұрын
@@ChristopherOkhravi Loved your design pattern videos
@shakibamoshiri25035 жыл бұрын
You are very talented for Stand-up Comedy!
@greatnile7 жыл бұрын
one more interesting benefit of doing it in the functional way is writing easier and cleaner tests with less setup/teardown
@ConstantinChirila7 жыл бұрын
Ahaha!! I love your duck faces when you are concentrated writing code :)))
@gagansrai81377 жыл бұрын
wow closures are awesome!
@atmark6667 жыл бұрын
i finally understand closure..
@Ihatetomatoes7 жыл бұрын
Very cool as always MPJ. Keep it up!
@omidkh99766 жыл бұрын
I love the way you transfer you knowledge :) keep it going :)
@viniciusalbuquerque80307 жыл бұрын
Hi, MPJ! You've put the same link for dependency injection and inversion of control in the video description. Is that right?
@funfunfunction7 жыл бұрын
Thanks!! Fixed.
@outcastant7 жыл бұрын
arghhh cliffhangerrrrrrrr
@andrewfong1017 жыл бұрын
shout out to all patrons and MPJ yay
@LenWoodward6 жыл бұрын
at 17:15 is this "currying" like I watched in one of your previous videos, or am I misunderstanding that principle?
@LenWoodward6 жыл бұрын
lol, never mind. I hit play again and you answered my question
@clarejang90887 жыл бұрын
Hi mpj! Did you check your github alarm?? I wrote a issue on your fffautomator!
@funfunfunction7 жыл бұрын
Hi, github notifications are WAY to noisy to check, ping me on twitter instead.
@clarejang90887 жыл бұрын
Fun Fun Function Than could you just check the repo issue? It's not so complex one...
@funfunfunction7 жыл бұрын
Thanks!
@electron0zero7 жыл бұрын
Loved the Intro.
@mateusz-grochowski4 жыл бұрын
Today I regret I'm not patron when FFF exist :/ miss You MPJ
@anthonygordon91707 жыл бұрын
I''m still very new to advance JS techniques like this but I'm missing something here... how would you pass the 'name' parameter to the inner function? Would it be something like ... createUser('John Doe') OR app('John Doe') ??
@funfunfunction7 жыл бұрын
+Anthony Gordon the former - createUser("John doe") i actually show this in the video, but I should probably have done it multiple times
@marshallfungai30337 жыл бұрын
You make javascript fun for sure ... "How does this work" -- #hahaha
@Neworldexport7 жыл бұрын
just great !
@NixAlaister7 жыл бұрын
Love the intro!
@Siddharthpandey277 жыл бұрын
Can you take session on call bind and apply
@roidrage4207 жыл бұрын
Did you intentionally wear the same shirt as your last dependency injection video?
@DiegoGurgelis7 жыл бұрын
So if I want to create a new user right way I can like that makeCreateUser(connection)('Name of the user'). Right?
@TheSkept1cal7 жыл бұрын
Yes!
@DiegoGurgelis7 жыл бұрын
Yes, it isn't I think the more intuitive way to use it's like mpj showed creating a const before he use the function.
@antihenti14767 жыл бұрын
is there anyway to use this dot in a constructor that have a event? something like this: function ball(x, y, radius) { this.x = x; this.y = y; this.radius = radius; document.addEventListener('click', function() { this.radius += 50; }); } var circle = new ball(50, 75, 200); ----------------------- This just creates a circle variable with x y and radius. But the problem is using this dot in a event won't work properly since javascript thinks this dot refers to the event rather than the circle object
@jedrzejsadowski68107 жыл бұрын
You can use an arrow function, because it binds its context at declaration not invocation. You could use .bind to do a similar thing, or (an ugly solution) assign this to a variable (like let self = this) and inside the handler use self.
@funfunfunction7 жыл бұрын
You can use an arrow function instead of the normal function which will lock the scope to the created object. However, since you are creating a new function and closure every time you create a new object you're throwing away the memory benefits of using the new keyword, so you might as well use a factory function, which is a lot better: function createBall(x, y, radius) { let ball = { x, y, radius } document.addEventListener('click', function() { ball.radius += 50 }) return ball } var circle = createBall(50, 75, 200) This will give you exactly the same result, but without the confusion of this and new.
@old_sport7 жыл бұрын
document.addEventListener('click', () => this.radius += 50); Check out FFFunction's video on Arrow functions and why **this** works. Oh boy I've watched too many of these videos...
@antihenti14767 жыл бұрын
Fun Fun Function Great! thanks for the support
@Locustfiretree7 жыл бұрын
Semi-colon vs no semi-colon is the new tabs vs spaces.
@HotelSeptember7 жыл бұрын
Am i missing something when it comes to closure? A function within another function OF COURSE has access to its parents function arguments. This just makes sense. Is this not possible within other languages?
@funfunfunction7 жыл бұрын
+Samuel Gregory many languages cannot declare standalone functions, let alone functions within functions.
@AngusMcIntyre7 жыл бұрын
I'm still not convinced I would want to do this, especially with a DI framework piecing large applications together for me. I feel like I lose type safety, semantics offered by good type names and decrease code legibility a little. Don't get me wrong, I use closures the whole time.
@funfunfunction7 жыл бұрын
I just want to make clear that I am ABSOLUTELY not advocating a DI framework. I think manual injection is much clearer, especially in JS. Can you elaborate why you feel that you lose type safety? There is no loss, the way I see it.
@AngusMcIntyre7 жыл бұрын
If my user utils are expecting an DatabaseConnection then it gives me more context as to what consuming code should pass in. If a function(string name) is expected then at some point I will rely on documentation and clear variable naming to get the message across. Its also easy for consuming code to pass in whatever it feels like so now I have more to check in code reviews. Clear code and decent test coverage eliminates these issues but sadly I don't have that luxury very often :D Maybe type safety was the wrong phrase and what I am actually referring to is legibility. Most of what I write is front-end C# with WPF so DI frameworks are a little more prevalent for me. Clearly in C# the method you described is used regularly. Almost every time I touch LINQ I am injecting closures. However, I'd have to give it some thought before I recommend that an application be structured around this pattern.
@funfunfunction7 жыл бұрын
+Angus McIntyre I don't quite get what you mean from your description, can you provide some example?
@AngusMcIntyre7 жыл бұрын
I went away and thought about this a lot. I even went and looked at popular .net libraries where functional composition is heavily used (e.g. LINQ , System.Reactive, TPL DataFlow, Stateless). I took a code review where a method was being called many times with the same parameter list. I asked the author to refactor it as a high order function that closed over the parameters - the code was drastically simplified. Its hard for me to give a concrete ruleset for when I'd abandon classes, but for dependencies that represent any part of the domain model (things that occur in my high level design like payment portals and databases) I'd almost always prefer passing in a named interface (IPaymentPortal, IUserStore, ISerialPortConnectionSource, etc) for clarity. However, my experimentation has warmed me to the idea for some cases. I like to think I managed to stay curious.
@MarcelRobitaille7 жыл бұрын
Is there ever a use case for Js classes? What if you need state?
@funfunfunction7 жыл бұрын
+Marcel Robitaille state can be handled just fine with closures. I'm overall not a fan of classes as a concept and generally avoid them if I can. If you are creating many objects (100000+ per second) then classes will give you better performance to a degree that I would use them, but at that point I would honestly ask myself why I'm creating that many objects. Classes are inherently an Object Oriented concept and the more functional your code is the less you need them.
@MarcelRobitaille7 жыл бұрын
How would you do state your way? Just a let at the top of the file? Somehow this seems less clean to me than with classes.
@funfunfunction7 жыл бұрын
+Marcel Robitaille you can check my video on factory functions to see an example. If you are trained in classes it will naturally seem unfamiliar, but unclean seems to be a really weird way to describe it, I find classes to be a mess, especially how they are implemented in JavaScript.
@habla_dev7 жыл бұрын
mpj can you please make a video on design patterns please =D thanks!
@funfunfunction7 жыл бұрын
+Augusto Velez you just watched one!
@slavasemenenko64276 жыл бұрын
Top
@q_rsqrt51407 жыл бұрын
Can anyone link music from this episode?
@duffman1212127 жыл бұрын
I don't actually know, but it sounds like Caravan Palace
@Totiimon7 жыл бұрын
I found amusing that you are using the same shirt as in the other dependency injection video(?)
@aboyer10137 жыл бұрын
The lamp behind him reminds me of Cartman.
@tmokenc17267 жыл бұрын
Have you noticed that your voice sounds better with x1.25 speed?
@thiagovilla9707 жыл бұрын
I think the video's default speed is already 25% faster
@lenaggar7 жыл бұрын
Rocket Surgery, LOL :D
@wBacz7 жыл бұрын
injection without classes? !!! this is crazy!!!
@Ratstail915 жыл бұрын
Nobody: Me: Wait, I'm doing this already!
@Marcosmapf5 жыл бұрын
MPJ is so erratic that sometimes I think I'm watching the video at 1.25 speed
@hallohi24145 жыл бұрын
the shirt is srodingers cat role reversed.
@MrTripi7 жыл бұрын
I still don't understand how currying reduces complexity.
@wh33lers7 жыл бұрын
I would argue for a class in a case where you have a lot of dependencies and a more complex logic within. The overhead structure of a class brings more readability to the table.
@funfunfunction7 жыл бұрын
Can you provide some reasoning and examples for your standpoint?
@wh33lers7 жыл бұрын
Maybe it is just about which you like more: constructor functions or factory functions. Sure Factory functions are simpler but also with less structure (overhead) as you also stated in your video. So making something simpler by getting rid of structure might be a good idea in some situations but not all. Overhead is not always bad if it has the right purpose.
@n1ru4l7 жыл бұрын
I once refactored a Database Class into Factory Functions. For me it was hard to mock the Database Repository Abstraction Classes for testing because they had so many methods that could be potentially called. With the functions I know exactly what parts I had to mock. Also static type checkers like flow will complain if you use an uncompleted mock class for dependency injection. However I the function approach lead to more code that I had to write for the dependency injection. In the end the quality and complexity of my tests decreased, so I think it was a good trade-in.
@wh33lers7 жыл бұрын
If I write unit-test and mock the database dependency I know exactly which methods I am going to use in that unit and will only mock those functions. I use the expects combined with willReturn create the right scenario. Maybe this might be environment dependened though.
@n1ru4l7 жыл бұрын
I don't like the "I know which methods are called"-part. I found this approach to be prefect to me. That way I do not have to mock the database, I can pass in functions that would behave like my repository functions. 🤔 Also it "fixed" all my type errors with flow.
@korkut31 Жыл бұрын
you better return a object litheral that have methods,
@jeffparent21597 жыл бұрын
To add to your conclusion, a good reason you write function generators using currying is to leverage functional composition. Think of how you would sanitize your user with a class vs using the function generators. If you want a hint....pastebin.com/7xEiLJ92
@Ozziepeck117 жыл бұрын
how is that table not coffee colored by now.
@ReuelRamos7 жыл бұрын
That sounds like cqrs pattern
@sc763997 жыл бұрын
Nooo cliff hanger!
@DeepDarkier7 жыл бұрын
You are comparing just one function, when you have many, like getUsers, editUser, deleteUser and so on, classes will end up with less code
@funfunfunction7 жыл бұрын
Not quite. AND there is a caveat as well, for both. This is what the next episode will be about, stay tuned!
@Ozziepeck117 жыл бұрын
you must spend so much on paper monthly.
@ehza7 жыл бұрын
next pewdiepie :3
@AbhishekMukherjee6 жыл бұрын
Is that you Tony Stark?
@EduardoRFS7 жыл бұрын
everyone love you show, and everyone hate your intro(and yes, i need to comment that every monday)
@neosahft7 жыл бұрын
for some reason i see code i see no semicolon..and i think brave new world.....:P
@coolworx7 жыл бұрын
Is that Shakespeare's BNW, or Huxley's?
@jimwilde7 жыл бұрын
Save the trees. Buy FFF a whiteboard.
@funfunfunction7 жыл бұрын
I strongly doubt that the amount of paper I've used so far is even close to the environmental impact of the production of a whiteboard.
@erikwiberg857 жыл бұрын
Better buy several, the way he rips everything..
@coolworx7 жыл бұрын
Heh. The same may apply to electric vehicles vs IC vehicles in relation to their carbon footprint.