Skip to content
Browse files

Currying Article

  • Loading branch information...
1 parent 147fe8e commit 48dbead22fd272b696105d975d0969c4c186c89e @Rafase282 Rafase282 committed
Showing with 45 additions and 0 deletions.
  1. +45 −0 Currying.md
View
45 Currying.md
@@ -0,0 +1,45 @@
+# Currying
+It is the act of taking a function with more than one argument and converting it into an equivalent function that takes a single argument. This is based on returning partially applied functions.
+
+**Original**
+
+```js
+function add (verb, a, b) {
+ return "The " + verb + " of " + a + ' and ' + b + ' is ' + (a + b)
+ }
+
+ add('sum', 5, '6')
+ //=> 'The sum of 5 and 6 is 11'
+```
+
+**Curried Version**
+
+```js
+function addCurried (verb) {
+ return function (a) {
+ return function (b) {
+ return "The " + verb + " of " + a + ' and ' + b + ' is ' + (a + b)
+ }
+ }
+ }
+
+ addCurried('total')(6)(5)
+ //=> 'The total of 6 and 5 is 11'
+```
+
+Currying by hand would be an incredible effort, but its close relationship with partial application means that if you have left partial application, you can derive currying. Or if you have currying, you can derive left partial application.
+
+Here's a function that curries any function with two arguments:
+
+```js
+ function curryTwo (fn) {
+ return function (x) {
+ return callFirst(fn, x)
+ }
+ }
+
+ function add2 (a, b) { return a + b }
+
+ curryTwo(add2)(5)(6)
+ //=> 11
+```

0 comments on commit 48dbead

Please sign in to comment.
Something went wrong with that request. Please try again.