Don't understand endal function

In scalaz, the function endoin is Function1Opsimplemented as follows:

def endo(implicit ev: R =:= T): Endo[T] =
  Endo.endo(t => ev(self(t)))

I am curious why the body has a Endo.endofunction, and not just taking self ... like Endo.endo(self), which behaves the same way Endo.endo(t=> ev(self(t))).

Here is my facial expression, and I do not see the difference between them. Did I miss something?

def endo[R, T](f: R => T)(implicit ev: T =:= R) = (x: R)=> ev(f(x))
def endo2[R, T](f: R => T)(implicit ev: T =:= R) = f 

Also, doesn't the first implementation add some overhead at runtime?

+5
source share
1 answer

For function Endo.endorequired A => A. The value selfis a function T => Rthat does not meet the requirement Endo.

T => R T => T, ev , , T => R T => T.

, , :

def endo(implicit ev: R =:= T): Endo[T] =
  Endo.endo(self andThen ev)

, .

+3

All Articles