Evaluate curried function

blue-sky

This curried function accepts a function of two arguments and converts it into a function of 1 argument :

def hotcurry[A, B, C](f: (A, B) => C): A => (B => C) =
  a => b => f(a, b)                             //> hotcurry: [A, B, C](f: (A, B) => C)A => (B => C)

def f(a : Int , b : Int) = a + b          //> f: (a: Int, b: Int)Int

hotcurry(f)                               //> res0: Int => (Int => Int) = <function1>

How can the return function res0: Int => (Int => Int) = <function1> be evaulated ?

I've tried fu(2) //> res0: Int => Int = <function1>

What is a good use case for this function ?

dhg

Just pass the parameters one at a time:

val g: Int => (Int => Int) = (a: Int) => (b: Int) => a + b
g(3)(4)
// res4: Int = 7

g takes an Int, and returns a function Int => Int. That function then takes an Int and returns an Int. So you pass a parameter to the first function, and then pass one to the function that's returned.

In your case, just do:

val g = hotcurry(f)
g(3)(4)

You can use it if you have a two parameter function that you want to use later as a one parameter function, packing one parameter in ahead of time. For instance, map takes a parameter Int => Int, so you could do something like:

val add = hotcurry(f)
val add3 = add(3)
Vector(1, 2, 3).map(add3)
// Vector(4, 5, 6)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generic curried map function

From Dev

Get arity of curried function

From Dev

scala curried function with yield

From Dev

Curried function in scala

From Dev

Scala: curried function in foreach?

From Dev

Invoking curried function

From Dev

Scala: Pass curried function as parameter

From Dev

Type inference in TypeScript for curried function

From Dev

Implementing a curried map function in Swift

From Dev

Scala: Pass curried function as parameter

From Dev

Apply a list of parameters to a curried function

From Dev

Calling a curried function that uses an implicit

From Dev

Trouble exporting curried generic function

From Dev

Cannot run Curried Function in REPL

From Dev

Functional Programming: Calling a Curried Function

From Dev

Haskell: How to recognize a curried function?

From Dev

How to define function accepting curried function parameter?

From Dev

How to define function accepting curried function parameter?

From Dev

Looking inside a curried function in R (reverse currying?)

From Dev

How to used named parameters with a curried function in scala

From Dev

Is there a fully applied curried function thunk in Haskell?

From Dev

Reference method from different class as curried function

From Dev

Uncurry a curried function of n parameters in javascript

From Dev

Looking inside a curried function in R (reverse currying?)

From Dev

How to enable a curried function to be inherited and then partially applied

From Dev

How to preload a Ramda curried function with item of an Array?

From Dev

Tuppled function versus curried function performance in SML/NJ

From Dev

Evaluate Matlab symbolic function

From Dev

Understanding the evaluate function in CasperJS

Related Related

HotTag

Archive