diff --git a/Scala/Advanced/1-functor.scala b/Scala/Advanced/1-functor.scala new file mode 100644 index 0000000..a553d58 --- /dev/null +++ b/Scala/Advanced/1-functor.scala @@ -0,0 +1,49 @@ +package functor + +import scala.language.higherKinds + +//Functor typeclass with existential F type +trait Functor[F[_]] { + def fmap[A, B](fa: F[A])(f: A => B): F[B] +} + +object Functor { + //Summons functor instance if it is in scope + def apply[F[_]](implicit f: Functor[F]): Functor[F] = f +} + +//Option typeclass that represents optional value. Value type is covariant +sealed trait Option[+A] +//Option child that holds value. Value type is covariant +case class Some[+A](get: A) extends Option[A] +//Option child that represents nothing +case object None extends Option[Nothing] + +object Option { + //Some factory method + def some[T](value: T): Option[T] = Some(value) + + //None factory method + def none[T]: Option[T] = None + + /* + * Remember this factory methods and that + * their return type is Option and not Some or None, + * they will come in handy in second example ;) + */ + + //Functor implementation for Option + implicit val optionFunctor: Functor[Option] = new Functor[Option] { + override def fmap[A, B](fa: Option[A])(f: A => B): Option[B] = fa match { + case Some(v) => Some(f(v)) + case None => None + } + } +} + +object Main1 extends App { + println(Functor[Option].fmap(None)((a: Int) => a + 2)) + //prints None + println(Functor[Option].fmap(Some("2"))(_.toInt)) + //prints Some(2) +} diff --git a/Scala/Advanced/2-functor-syntax.scala b/Scala/Advanced/2-functor-syntax.scala new file mode 100644 index 0000000..94bd367 --- /dev/null +++ b/Scala/Advanced/2-functor-syntax.scala @@ -0,0 +1,30 @@ +package functor + +import scala.language.higherKinds + +object FunctorSyntax { + //Implicit Functor operations class + implicit class FunctorOps[F[_], A](val obj: F[A]) extends AnyVal { + def fmap[B](f: A => B)(implicit functor: Functor[F]): F[B] = + functor.fmap(obj)(f) + } +} + +object Main2 extends App { + import Option._ + import FunctorSyntax._ + + /* + * Option is created using factory method and there is reason for this. + * For instance such example wont compile: + * + * Some(2).fmap(_ + 1) + * + * The reason is that it will search for Functor[Some] + * and would not take into account Functor[Option] + * that could be fixed by making Functor typeclass Invariant like it is fixed in Cats + * but it is a little bit complicated for our simple example + */ + println(some("2").fmap(_.toDouble).fmap(a => a + 3)) + //prints Some(5.0) +} diff --git a/Scala/Advanced/README.md b/Scala/Advanced/README.md new file mode 100644 index 0000000..cf145cb --- /dev/null +++ b/Scala/Advanced/README.md @@ -0,0 +1,19 @@ +## Description +The most flexible way to implement functor in Scala is to define it as typeclass. +Good example of such behaviour is Cats and Scalaz libraries that supply developer with different typeclasses. This libraries include Functor too, but in much more complicated and mature form than in this example. + +## How to Run +First sources should be compiled to `.jar` file using `scalac` command: +> scalac *.scala -d Functor.jar + +Next the examples can be executed by their fully qualified name(either `functor.Main1` or `functor.Main2`): + +> scala -cp Functor.jar functor.Main1 + +Another way to run examples is to use build tools like: sbt, gradle or maven + +## Helpful resources +* [Functor in Cats](https://typelevel.org/cats/typeclasses/functor.html) +* [Functor in Scalaz](http://eed3si9n.com/learning-scalaz/Functor.html) +* [Cats Functor exercises](https://www.scala-exercises.org/cats/functor) +* [sbt website](https://www.scala-sbt.org/) \ No newline at end of file