Function Functors Definition Classic List Threaded 5 messages Open this post in threaded view
|

Function Functors Definition

 In the following code snippet, I define a Function1 Functor:implicit def Function1Functor[R]: Functor[({type l[a]=(R) => a})#l] = new Functor[({type l[a]=(R) => a})#l] { def fmap[A, B](r: R => A, f: A => B) = r andThen f }1. But why is that signature ({type l[a]=(R) => a})#l like that? how to interpret that?2. I could now randomly pick any type and qualify that as a Functor. For example.,      case class SomeRandomType[A] (param: A) // this does not know about the fact that it is going to be a Functor soon     val someRandomTypeFunctor = new Functor[SomeRandomType] {       def map[A, B](fa: SomeRandomType[A])(f: A => B): SomeRandomType[B] =           SomeRandomType(f(fa.param))     } So this to me is a Typeclass definition. Is that correct? -- You received this message because you are subscribed to the Google Groups "scala-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout.
Open this post in threaded view
|

Re: Function Functors Definition

Open this post in threaded view
|

Re: Function Functors Definition

 In reply to this post by Joe San Op woensdag 19 april 2017 09:16:59 UTC+2 schreef Joe San:In the following code snippet, I define a Function1 Functor:implicit def Function1Functor[R]: Functor[({type l[a]=(R) => a})#l] = new Functor[({type l[a]=(R) => a})#l] { def fmap[A, B](r: R => A, f: A => B) = r andThen f }1. But why is that signature ({type l[a]=(R) => a})#l like that? how to interpret that?Functor is defined like this (more or less): trait Functor[F[_]].  This means that it accepts a type constructor of arity 1.Function1 is defined like this: trait Function1[-A,+B]. So Function1 is a type constructor of arity 2.If you write a Functor for List (arity 1) you can just say Functor[List] and you're done. But Functor[Function1] is impossible.So to create a Functor for a Function1 you have to supply 1 type argument to Function1 and leave 1 parameter open, or in other words partially apply the Function1 type constructor.Scala doesn't have special syntax for partially applying type constructors, so you have to do it with ({ type Lambda[x] = Function1[SomeType,x] })#Lambda.How this works is that you create a structural type that contains a type alias called Lambda which is a type constructor of arity 1. Then you use the # operator on that structural type to extract the Lambda type constructor; this is called type projection. 2. I could now randomly pick any type and qualify that as a Functor. For example.,      case class SomeRandomType[A] (param: A) // this does not know about the fact that it is going to be a Functor soon     val someRandomTypeFunctor = new Functor[SomeRandomType] {       def map[A, B](fa: SomeRandomType[A])(f: A => B): SomeRandomType[B] =           SomeRandomType(f(fa.param))     } So this to me is a Typeclass definition. Is that correct? -- You received this message because you are subscribed to the Google Groups "scala-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout.