tonilopezmr / tonilopezmr.github.io

My web portfolio.
https://tonilopezmr-github-io.vercel.app
Other
4 stars 1 forks source link

Currying #6

Open tonilopezmr opened 6 years ago

tonilopezmr commented 6 years ago

Currying is the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument.

For example a normal function:

// (Int, int) -> Int
fun sum(a: Int, b: Int): Int = a + b

sum(2, 2) //4

could be rewritten like this:

//Int -> (Int -> Int)
fun sum(a: Int): (Int) -> Int {
  return { b: Int -> a + b }
}

val twoPlus = sum(2)
twoPlus(2) // 4
sum(2)(2) // 4

This is a two-arity function #5.

Conclusion

Currying transform a function that takes multiple arguments and returned it as a chain of functions

Resources

Curriying