Press "Enter" to skip to content

Currying – Part 6 of Functional Programming in JavaScript


hello what is currying currying is when

a function doesn’t take all of its

arguments upfront instead it wants you

to give it the first argument and then

the function returns another function

which you are supposed to call with the

second argument which in turn will

return a new function which you are

supposed to call with the third argument

and so on until all the arguments have

been provided and then the function at

the end of the chain will be the one

that returns the value that you actually

what does it sound confusing good the

feeling of confusion is your friend it

means you’re learning before we get to

it I’m going to incorporate some viewer

feedback into the show this one is for

you

Joshua foreground and everything have a

look at this beautiful function this

function is not curried but we’re going

to curry it up in just a minute it’s a

very simple function it takes a name a

size and element all strings and returns

fluffykins is a tiny dragon that

breathes lightning let’s clear all that

proof here is the same function in the

korean version dragon is a function that

takes a name it will in turn return

another function which takes size which

in turn will return another function

which takes an element which in turn

will return the final strength I’m going

to call it and log the output now

remember

dragon will take a name block Akins and

it will return another function which

takes a size tiny which in turn returns

a third function which takes an element

fluffykins is a tiny dragon that

breathes slightly so you see that it’s a

chain of functions and if i just remove

this last lightning call here and just

run the code it would just show that oh

it’s a function the idea with carrying

is that your function can pass through

the application and gradually receive

the arguments that it needs the function

goes through your app you sprinkle it

with some arguments here you spice it up

with an argument here and here you add

the final one and you get your output so

for instance you might have broken out

dragon here

into fluffykins dragon and then refer to

it here and you would get the same

tiny dragon and we run it again and we

get the same result now our dragon

function here is written from the start

to be curable but every functional

library worth its salt has a way of

making nan Korea Bowl functions career

proof now dragon is back in its standard

form the one we saw from the beginning

there is no currying here it’s just the

normal arguments as an example I’m gonna

use lo – vs co MP I mean stole low –

[Music]

dippity-do in stock now we can use it in

pour here we go and then we just do

hungry just overwrite the existing one

BAM and now fluffykins it’s a tiny

dragon that means I think you don’t have

to use slow – specifically for this all

libraries with any kind of functional

nature has a career option

that’s what currying is and how it works

but why is it useful postproduction

Leviosa

so what you see here has no carrying at

all it’s just normal code we’re gonna

improve it a bit with carrying let’s

walk through it we have a dragons array

we have a couple of lightning dragons

here knew me and fluffykins but we also

have a fire dragon and we also have our

time warp dragon that’s serious business

we are going to get to line eight and

nine but let’s first look at line eleven

and twelve where we filter out the

dragon the lightning dragons from the

Dragons array so we just call filter on

dragons you all recognize this from the

previous episodes filter takes a

callback which will get every item and

that item will be checked

against this has element function if so

it has element lightning and we pass in

the object and we get a true or false

and this will then be passed to

lightning dragons just let me run that

and you see that it’s fluffykins and

nume that is filtered off if you look at

has element it’s a function that takes

an element this is this string here goes

here and the filtered object or the

iterated the object that filter iterates

goods here and just compares that

against the element property of the the

object let’s try to improve this with

and curry has element and then just

remove this wrapping function just do

that Wow

so now has element is Korea ball

that means when we call has element with

lightning that means that it will return

a new function which in turn expects we

passed the the object to check if it has

a limit lightning which allows us to

pass it as the callback function to

filter directly which is priestly call

occlusion a curable function is simply a

function that takes every argument by

itself and then just returns a new

function that expects the next

dependency to the function until all

dependencies have been fulfilled and the

final value is returned so we call

Carroll functions like this we call them

with the first argument with the name

which in turn will return a new function

which we call with this size which in

turn will return a new function which we

call with the elements which will return

the final value which we output here and

it’s Karl is a large dragon that

breathes eyes and that is car people as

usual I would love to hear from you

either on Twitter at MP Jaime or in a

comment down below either just write

something nice or funny

tell me what you think could be made

better or tell me what you would like to

see me make a video about speaking of

which make sure that you don’t miss the

next episode make sure that you

subscribe to the channel or follow me on

Twitter

until next Monday stay curious [Music]

Please follow and like us: