Currying - Part 6 of Functional Programming in JavaScript
Skills:
JavaScript Fundamentals90%
Key Takeaways
Explains currying in JavaScript as part of functional programming
Full Transcript
hello what is currying currying is when a function doesn't take all of its arguments up front 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 uh function at the end of the chain will be the one that Returns the value that you actually want 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 an element all strings and returns another string let me call it and run it fluffkin is a tiny dragon that breathes lightning let's clear all thatof here is the same function in the curry version dragon is a function 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 string I'm going to call it and log the output now remember Dragon will take a name fluffy kins and it will return another function which takes a size tiny which in turn returns a third function which takes an element lightning let's run that again fluin is a tiny dragon that breathes lightning so you see that it's a chain of functions and if I just remove this last lightning call here and just ran 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 and you get your output so for instance you might have broken out Dragon here into fluffin dragon and then refer to it here and you would get the same output or maybe into three parts tiny [Music] dragon and we run it again and we get the same result now our Dragon function here is written from the start to be carriable but every functional Library worth its salt has a way of making non-curable functions curable poof now dragon is back in its standard form the one we saw from the beginning the there is no currying here it's just the normal arguments as an example I'm going to use low Dash we just go mpm install low [Music] Dash install and now we can use it import from no dash there we go and then we just do Curry dragon we just overwrite the existing one bam and now fluffy kins is a tiny dragon that breathes lighting you don't have to use low Dash specifically for this all libraries with any kind of functional Nature has a curry function that's what currying is and how it works but why is it useful post production Leviosa so what you see here has no currying at all it's just normal code we're going to improve it a bit with carrying let's walk through it we have a dragons array uh we have a couple of lightning dragons here numi and fluffy kins but we also have a fire dragon and we also have a Time War Dragon that's serious business we are going to get to line 8 and nine but let's first look at line 11 and 12 where we filter out the dragon the lightning dragons from the dragon array so we just call filter on dragons you all recognize this from the previous episodes filter takes a call back which will get every item and that item will be checked against this H element function if so it 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'll see that it's fluffy kins and new me that is filtered out 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 iterate the object that filter iterates goes here and just Compares that against the element property of the the object let's try to improve this with carrying let's import load out and Curry has element and then we just remove this wrapping function just do that wow so now has element is carriable that means when we call has element with lightning that means that it will return a new function which in turn expects to be passed the uh the object to check if it has element lightning which allows us to pass it as the uh callback function to filter directly which is pretty Sleek conclusion a carriable 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 carriable 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 with the size which in turn will return a new function which we call with the element which will return the final value which we output here and it's car is a large dragon that breathes ice and that is Cur people as usual I would love to hear from you either on Twitter at mpj me 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
Original Description
💖 Support the show by becoming a Patreon
https://www.patreon.com/funfunfunction
A short video explaining the concept of curring, using JavaScript. This is part of a series, where are learning functional programming using JavaScript.
Currying is when a function, instead of taking all arguments at one time, takes the first one and returns a new function that takes the second one and returns a new function which takes the third one, and so forth, until all arguments have been fulfilled.
Curry function in lodash:
https://lodash.com/docs#curry
Playlist of full series
https://www.youtube.com/playlist?list=PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84
You want to follow me on Twitter and Quora:
https://twitter.com/mpjme
http://www.quora.com/Mattias-Petter-Johansson
💛 Follow on Twitch
We record the show live Mondays 7 AM PT
https://twitch.tv/funfunfunction
💛 Fun Fun Forum
Private discussion forum with other viewers in between shows. https://www.funfunforum.com. Available to patron members, become one at https://www.patreon.com/funfunfunction
💛 mpj on Twitter
https://twitter.com/mpjme
💛 CircleCI (Show sponsor)
Robust and sleek Docker-based Continuous Integration as a service. I used CircleCI prior to them becoming a sponsor and I love that their free tier is powerful enough for small personal projects, even if they are private. Use this link when you sign up to let them know you came from here:
https://circleci.funfunfunction.com
💛 Quokka (Show sponsor)
Wonder how MPJ evaluates JavaScript inline his editor. Quokka is the answer - use this link when you buy to let them know you came from here:
http://quokka.funfunfunction.com
💛 FUN FUN FUNCTION
Since 2015, Fun Fun Function (FFF) is one of the longest running weekly YouTube shows on programming 🏅 thanks to its consistency and quality reaching 200,000+ developers.
🤦♂️ The Failing Together concept is what makes FFF unique. Most coding content out there focus on step-by-step tutorials. We think tutorials are too far
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Fun Fun Function · Fun Fun Function · 8 of 60
1
2
3
4
5
6
7
▶
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Higher-order functions - Part 1 of Functional Programming in JavaScript
Fun Fun Function
Map - Part 2 of Functional Programming in JavaScript
Fun Fun Function
Reduce basics - Part 3 of Functional Programming in JavaScript
Fun Fun Function
Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Fun Fun Function
Reduce Advanced - Part 4 of Functional Programming in JavaScript
Fun Fun Function
Closures - Part 5 of Functional Programming in JavaScript
Fun Fun Function
Too many tools and frameworks!
Fun Fun Function
Currying - Part 6 of Functional Programming in JavaScript
Fun Fun Function
Recursion - Part 7 of Functional Programming in JavaScript
Fun Fun Function
Promises - Part 8 of Functional Programming in JavaScript
Fun Fun Function
Staying relevant as a programmer
Fun Fun Function
Factory Functions in JavaScript
Fun Fun Function
Composition over Inheritance
Fun Fun Function
Software needs to be better - FunFunFunction #1
Fun Fun Function
Unit testing: How to get your team started - FunFunFunction #2
Fun Fun Function
Straight-line code over functions - FunFunFunction #3
Fun Fun Function
Clojure - FunFunFunction #5
Fun Fun Function
The growth stages of a programmer - FunFunFunction #6
Fun Fun Function
5 tips to quickly understand a new code base - FunFunFunction #7
Fun Fun Function
Semicolons cannot save you! - FunFunFunction #9
Fun Fun Function
Functors - FunFunFunction #10
Fun Fun Function
Functors: I was WRONG! - FunFunFunction #11
Fun Fun Function
Questions and Answers - FunFunFunction #12
Fun Fun Function
Streams - FunFunFunction #13
Fun Fun Function
Prototypes in JavaScript - FunFunFunction #16
Fun Fun Function
Fast or Flexible? - FunFunFunction #17
Fun Fun Function
Coders are herd animals - FunFunFunction #18
Fun Fun Function
Weekend Kubernetes Shenanigans - FunFunFunction #19
Fun Fun Function
Monad - FunFunFunction #21
Fun Fun Function
Moar Weekend Shenanigans - FunFunFunction #23
Fun Fun Function
Questions and Answers - FunFunFunction #24
Fun Fun Function
Losing motivation - FunFunFunction #25
Fun Fun Function
LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
Fun Fun Function
Fast code is NOT important - FunFunFunction #27
Fun Fun Function
Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Fun Fun Function
Writing unit tests for personal projects? - FunFunFunction #29
Fun Fun Function
Let's Code a Pomodoro Button - FunFunFunction #30
Fun Fun Function
What editor do you use? - FunFunFunction #31
Fun Fun Function
Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Fun Fun Function
Is Programming Art? - MPJ's Musings - FunFunFunction #33
Fun Fun Function
Generators in JavaScript - What, Why and How - FunFunFunction #34
Fun Fun Function
Haskell Basics - FunFunFunction #35
Fun Fun Function
Haskell - Baby's first functions - FunFunFunction #36
Fun Fun Function
Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Fun Fun Function
How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
Fun Fun Function
Haskell lists - FunFunFunction #39
Fun Fun Function
var, let and const - What, why and how - ES6 JavaScript Features
Fun Fun Function
Why are some programming languages popular? - MPJ's Musings - FunFunFunction #41
Fun Fun Function
Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Fun Fun Function
bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
Fun Fun Function
Examples of this and bind - Object Creation in JavaScript P2 - FunFunFunction #44
Fun Fun Function
Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Fun Fun Function
Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Fun Fun Function
Cellular Automata - Pair Programming - FunFunFunction #49
Fun Fun Function
The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
Fun Fun Function
__proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
Fun Fun Function
Unity game pair programming - Let's code - FunFunFunction #53
Fun Fun Function
Throw out your tools - MPJ's Musings - FunFunFunction #54
Fun Fun Function
Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Fun Fun Function
Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Fun Fun Function
More on: JavaScript Fundamentals
View skill →Related Reads
📰
📰
📰
📰
Harbor HTTPS Setup Using a Self-Signed Certificate
Medium · DevOps
Debug a Legacy Frontend-Backend Deployment With One Trace ID
Dev.to · Odd_Background_328
Four inspectable workflows for context, research, orchestration, and evaluation
Dev.to · Zain Dana Harper
Scaling Globally: Best Practices in Cloud CI/CD
Medium · DevOps
🎓
Tutor Explanation
DeepCamp AI