Question

(invalid)

What is the best way to partially substitute arguments in a curried function for Units:

trait Expr[A] { def apply : A }

type Reaction[A] = A => Unit
type TypedReactor[A] = Expr[A] => Reaction[A] // aka Expr[A] => A => Unit
type FlatReactor = () => () => Unit

def flatten[A](e: Expr[A], r: TypedReactor[A]): FlatReactor = ???

So a curried Function1 needs to be transformed left-to-right to a curried Function0.

The following works, but looks pretty awkward:

def flatten[A](e: Expr[A], r: TypedReactor[A]): FlatReactor = () => { 
  val unc = r(e); val eval = e.apply; () => unc(eval)
}

EDIT

Sorry, there was a mistake. The flatten function actually looks like this:

def flatten[A](e: Expr[A], r: Reaction[A]): FlatReactor = () => { 
  val eval = e.apply; () => r(eval)
}

So I don't think it can be any more simplified than this.

Was it helpful?

Solution

Just substituting your various vals directly into a single expression gives:

def flatten[A](e: Expr[A], r: TypedReactor[A]): FlatReactor =
  () => () => r(e)(e.apply)

Which looks fine to me?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top