Successfully reported this slideshow.
Upcoming SlideShare
×

# Curry functions in Javascript

1,693 views

Published on

Exploring higher order functions, curry functions in JS. Creating and using curry functions

Published in: Technology, Self Improvement
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### Curry functions in Javascript

1. 1. HIGHER-ORDER FUNCTIONS- WHAT ARE THEY? They are functions ( what a surprise !) that do one or both of the following:  Take other functions as parameters  return a function For this discussion the term function will refer to “pure functions” i.e., functions that return the same value if they are called with the same arguments.
2. 2. TYPES OF FUNCTIONS function Add(x,y) { return x+y } function Add (x,y) { console.log (x+y) return x+y } function newadder(x){ return function (y){ return x+y } } function fn ( another function) { return anotherfunction() } function fnfn (another fn){ return function (x){ return anotherfn()+x } } <- a pure function <- a function with side effects <- a function that returns another function <- function that takes another function and returns a value. <-a function that takes another function and returns yet another function!!
3. 3. CURRYING Nothing to do with Indian cuisine. It is a reference to the mathematician Haskell Curry, who re-discovered Moses Schonfinkel‟s work on these types of functions. A technique of converting a function that takes multiple arguments to a chain of functions that take one argument each. For example: f (a,b)-> a+b .=> curried_f(a)(b)  curried_f is a higher order function that takes „a‟ as an argument and returns a function ( let us called it g(b) ). The function g() is called with „b‟ as its argument.  When curried_f() is called with a single argument „a‟ it returns a function g(b) where the value of „a‟ is fixed . In this case curried_f(10) will return function g(b) that returns the value 10+ „b‟. ( g(1) will be 11 , g(2) will be 12 etc.
4. 4. CURRIED FUNCTIONS EXAMPLES function Add(x,y) { return x+y } function curry_add(a){ return function(b){ return a+b } } Usage: Add10=curry_add(10) Add10(100) -> 110 <- normal add function; <“curried” version Returns a new function that takes „b‟ as an argument and returns the value of a+b <- Add10 is a new function that takes 10 as an argument and returns a function that returns 10 + whatever is passed to it. add10(b)-> 10 +b