JavaScript Function Currying

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay now we want to look at function currying this is a concept as long as you understand the basics of how a function works and how a return statement inside of a function work you'll be absolutely fine with this I put two videos on the basics of functions on the return statement the links will be down inside the comments if you take a look there so function Korean what is that well relies on the fact that in JavaScript functions our first class objects and that means just like strings numbers Julianne's objects arrays other data types you can pass a function to another function and you can have a function that returns a function from it so in the same way that if I was writing a function say function X and I was going to pass something to that function some variables may be passed and put inside this variable a that a could contain a string a number a boolean and array an object any data type you want including another function so there's function X here's function y and when we call function X to make it run we can't actually pass the function y to it and then inside of function box we can make that function right so this line right here this will execute function Y so this function right now is not doing anything close it out log Y inside of here just to see that's going to happen also our log inside that function ox is running right here the common condition in the box so let's just see that right there we go X and then Y both of them being right now they call it function X so this calls function X the passing function y into this and then saying that a carry we've got function X is currently running and then we're saying another thing that you passed in run Matt it's a function this is going to make function one line and that's why we get both of these comments here okay so that's the fact that functions are first class objects so we can pass them to functions we can also return them from functions so we could return the number of one two three because the term strain a with the term in boolean we can return whatever this was or if we declared a new thing inside of here we can return a variable which contains a function so down here I said let B be the result of running the function X a is going to be passed back here and we can till B to run so B is the results from functioning which just happens to be Y because Y it is passed into it so this right here is the same as running Commission wide so there we go X 1 Y so X ran inside of X Y does run and then this returned the function of Y to the variable B and then we're running it so that's why we get the XY Y all right now function currying I'm going to move this down and just comment this block of code I to leave it in there so that it's in the gist later on for you guys okay and then inside here function currying what is creating well it's similar to what we are doing here we've got a function and it's able to run that function it's able to return a function inside of it so I'm going to create a function called greet and I'm going to pass string to that function so if I was to call leads with hi inside of here I meant to do the console dot log so we're doing function greet here it's being called right here hi is the message and also evolve message yeah there's the string no no real surprises here now what I want to do is let's say a bit of situation where I want to be able to call this reading and I want to be able to pass in a salutation in multiple languages so maybe Swedish English Spanish and German I want to be able to call for different versions of this function and I want to be able to call the person's name so we could do something like this and put the person's name and pass in the two parameters like this and read of it but let's say I want to set up these functions so I want to make a German one an English one a Spanish Juan and Swedish one because at this point in my code I don't know the names I don't know how many names there are I don't know how many times it's going to be used and I don't want to necessarily write for different functions one for English one for German one for Swedish one for Spanish I don't want to do that I don't want to create for functions just so somebody else has the four functions and they have to pick from that necessarily I want to try and keep my code as minimal as possible so the message that I'm passing in is actually going to be used here I'm creating another function inside of this function inside here named this is going to do the name of the person that I'm saying whatever message to but because of the way these are nested my function on the inside is going to be able to create a closure and access this variable right here so I can say console dot log message name I don't have to pick these both here say I don't know what you're going to be I'll say that's English equal greet hi so this is going to take the word hi put it inside of here the word hi is going to be used inside seated rather high and then something else it's being returned is this function just like down here with returning a what was passed in we're returning but this is a function I'm returning a function that didn't exist anywhere else this function comes back and is put into here so inside of English this is the resultant function I can go inside here and save tom now when I run this code from this page greet is going to be run the string hi is going to be play inside of here and then a reference to it is going to be kept and this function right here with the variable name is going to be returned and put into here English has now become a function and what I call it Tom is the name this will be the original high message that I sent in so it's kind of like having this inner function that says all right name that's effectively what we're doing with these two lines of code we run Matt hi Tom I'm saying I'm gonna use the message hi that's my English message that's going to be saved as part of this if I say Center greet a I say let's just an old FFD box sent over the end that's going to be hola Jorge Club okay I have now created just move this down below here I've created four functions from this greet is use to create this function and the value that was passed in these four values are going to be saved inside the function that's returned so English fence today is on your Android those are now four brand new functions which will accept a single string so I can say Swinscoe and this thing guys the name Aspen oh say hi to Carlos and touch yes okay safe rum not no hi Tom Jenga for the Carlos and Todd Mateus so we have four brand new functions that we have created here with the greet so English Spence guys can watch those have become functions which will be waiting for this grammar so when I pass in those names they get used and the message parameter that was the original one right here that we passed in from these four arms and what's function kerning it's creating a brand new function by returning it from an outer function the returned element becomes the inner function that you can use and the powerful thing about it is you get this saved variable disclosure which saves this original value for you to use at a later time so if you don't know what both the values are or if you need to create a whole bunch of functions that have similar functionality this is a great way to kind of manufacture those functions for you so that you can use them at a later time for
Info
Channel: Steve Griffith - Prof3ssorSt3v3
Views: 9,150
Rating: 4.9727893 out of 5
Keywords: JavaScript, functions, web development, MAD9014, function currying, currying, functions as objects, first-class objects, first class objects
Id: F_N97iovVbQ
Channel Id: undefined
Length: 11min 41sec (701 seconds)
Published: Sat Jul 22 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.