Currying - Part 6 of Functional Programming in JavaScript

Fun Fun Function · Beginner ·☁️ DevOps & Cloud ·10y ago

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 Higher-order functions - Part 1 of Functional Programming in JavaScript
Higher-order functions - Part 1 of Functional Programming in JavaScript
Fun Fun Function
2 Map - Part 2 of Functional Programming in JavaScript
Map - Part 2 of Functional Programming in JavaScript
Fun Fun Function
3 Reduce basics - Part 3 of Functional Programming in JavaScript
Reduce basics - Part 3 of Functional Programming in JavaScript
Fun Fun Function
4 Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Fun Fun Function
5 Reduce Advanced - Part 4 of Functional Programming in JavaScript
Reduce Advanced - Part 4 of Functional Programming in JavaScript
Fun Fun Function
6 Closures - Part 5 of Functional Programming in JavaScript
Closures - Part 5 of Functional Programming in JavaScript
Fun Fun Function
7 Too many tools and frameworks!
Too many tools and frameworks!
Fun Fun Function
Currying - Part 6 of Functional Programming in JavaScript
Currying - Part 6 of Functional Programming in JavaScript
Fun Fun Function
9 Recursion - Part 7 of Functional Programming in JavaScript
Recursion - Part 7 of Functional Programming in JavaScript
Fun Fun Function
10 Promises - Part 8 of Functional Programming in JavaScript
Promises - Part 8 of Functional Programming in JavaScript
Fun Fun Function
11 Staying relevant as a programmer
Staying relevant as a programmer
Fun Fun Function
12 Factory Functions in JavaScript
Factory Functions in JavaScript
Fun Fun Function
13 Composition over Inheritance
Composition over Inheritance
Fun Fun Function
14 Software needs to be better - FunFunFunction #1
Software needs to be better - FunFunFunction #1
Fun Fun Function
15 Unit testing: How to get your team started - FunFunFunction #2
Unit testing: How to get your team started - FunFunFunction #2
Fun Fun Function
16 Straight-line code over functions - FunFunFunction #3
Straight-line code over functions - FunFunFunction #3
Fun Fun Function
17 Clojure - FunFunFunction #5
Clojure - FunFunFunction #5
Fun Fun Function
18 The growth stages of a programmer - FunFunFunction #6
The growth stages of a programmer - FunFunFunction #6
Fun Fun Function
19 5 tips to quickly understand a new code base - FunFunFunction #7
5 tips to quickly understand a new code base - FunFunFunction #7
Fun Fun Function
20 Semicolons cannot save you! - FunFunFunction #9
Semicolons cannot save you! - FunFunFunction #9
Fun Fun Function
21 Functors - FunFunFunction #10
Functors - FunFunFunction #10
Fun Fun Function
22 Functors: I was WRONG! - FunFunFunction #11
Functors: I was WRONG! - FunFunFunction #11
Fun Fun Function
23 Questions and Answers - FunFunFunction #12
Questions and Answers - FunFunFunction #12
Fun Fun Function
24 Streams - FunFunFunction #13
Streams - FunFunFunction #13
Fun Fun Function
25 Prototypes in JavaScript - FunFunFunction #16
Prototypes in JavaScript - FunFunFunction #16
Fun Fun Function
26 Fast or Flexible? - FunFunFunction #17
Fast or Flexible? - FunFunFunction #17
Fun Fun Function
27 Coders are herd animals - FunFunFunction #18
Coders are herd animals - FunFunFunction #18
Fun Fun Function
28 Weekend Kubernetes Shenanigans - FunFunFunction #19
Weekend Kubernetes Shenanigans - FunFunFunction #19
Fun Fun Function
29 Monad - FunFunFunction #21
Monad - FunFunFunction #21
Fun Fun Function
30 Moar Weekend Shenanigans - FunFunFunction #23
Moar Weekend Shenanigans - FunFunFunction #23
Fun Fun Function
31 Questions and Answers - FunFunFunction #24
Questions and Answers - FunFunFunction #24
Fun Fun Function
32 Losing motivation - FunFunFunction #25
Losing motivation - FunFunFunction #25
Fun Fun Function
33 LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
Fun Fun Function
34 Fast code is NOT important - FunFunFunction #27
Fast code is NOT important - FunFunFunction #27
Fun Fun Function
35 Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Fun Fun Function
36 Writing unit tests for personal projects? - FunFunFunction #29
Writing unit tests for personal projects? - FunFunFunction #29
Fun Fun Function
37 Let's Code a Pomodoro Button - FunFunFunction #30
Let's Code a Pomodoro Button - FunFunFunction #30
Fun Fun Function
38 What editor do you use? - FunFunFunction #31
What editor do you use? - FunFunFunction #31
Fun Fun Function
39 Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Fun Fun Function
40 Is Programming Art? - MPJ's Musings - FunFunFunction #33
Is Programming Art? - MPJ's Musings - FunFunFunction #33
Fun Fun Function
41 Generators in JavaScript - What, Why and How - FunFunFunction #34
Generators in JavaScript - What, Why and How - FunFunFunction #34
Fun Fun Function
42 Haskell Basics - FunFunFunction #35
Haskell Basics - FunFunFunction #35
Fun Fun Function
43 Haskell - Baby's first functions - FunFunFunction #36
Haskell - Baby's first functions - FunFunFunction #36
Fun Fun Function
44 Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Fun Fun Function
45 How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
Fun Fun Function
46 Haskell lists - FunFunFunction #39
Haskell lists - FunFunFunction #39
Fun Fun Function
47 var, let and const - What, why and how - ES6 JavaScript Features
var, let and const - What, why and how - ES6 JavaScript Features
Fun Fun Function
48 Why are some programming languages popular? - MPJ's Musings  - FunFunFunction #41
Why are some programming languages popular? - MPJ's Musings - FunFunFunction #41
Fun Fun Function
49 Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Fun Fun Function
50 bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
Fun Fun Function
51 Examples of this and bind - Object Creation in JavaScript P2 -  FunFunFunction #44
Examples of this and bind - Object Creation in JavaScript P2 - FunFunFunction #44
Fun Fun Function
52 Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Fun Fun Function
53 Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Fun Fun Function
54 Cellular Automata - Pair Programming - FunFunFunction #49
Cellular Automata - Pair Programming - FunFunFunction #49
Fun Fun Function
55 The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
Fun Fun Function
56 __proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
__proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
Fun Fun Function
57 Unity game pair programming - Let's code - FunFunFunction #53
Unity game pair programming - Let's code - FunFunFunction #53
Fun Fun Function
58 Throw out your tools - MPJ's Musings - FunFunFunction #54
Throw out your tools - MPJ's Musings - FunFunFunction #54
Fun Fun Function
59 Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Fun Fun Function
60 Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Fun Fun Function

Related Reads

Up next
AWS, Azure, GCP: The One Thing Every Business Gets Wrong
AI Daily
Watch →