From d458b0433bdd300249c69e5b65c73174e20aa13c Mon Sep 17 00:00:00 2001 From: Oleh Dubynskiy Date: Sat, 8 Dec 2018 22:57:33 +0200 Subject: [PATCH 1/3] add Scala functor example --- Scala/Maybe1.scala | 43 +++++++++++++++++++++++++++++++++++++++++++ Scala/Maybe2.scala | 19 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 Scala/Maybe1.scala create mode 100644 Scala/Maybe2.scala diff --git a/Scala/Maybe1.scala b/Scala/Maybe1.scala new file mode 100644 index 0000000..59e9dc4 --- /dev/null +++ b/Scala/Maybe1.scala @@ -0,0 +1,43 @@ +package maybe + +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 +} + +//Maybe typeclass that represents optional value. Value type is covariant +sealed trait Maybe[+T] +//Maybe child that holds value. Value type is covariant +case class Just[+T](value: T) extends Maybe[T] +//Maybe child that represents nothing +case object None extends Maybe[Nothing] + +object Maybe { + //Just factory method + def just[T](value: T): Maybe[T] = Just(value) + + //None factory method + def none[T]: Maybe[T] = None + + //Functor implementation for Maybe + implicit val maybeFunctor: Functor[Maybe] = new Functor[Maybe] { + override def fmap[A, B](fa: Maybe[A])(f: A => B): Maybe[B] = fa match { + case Just(v) => Just(f(v)) + case None => None + } + } +} + +object Main extends App { + println(Functor[Maybe].fmap(None)((a: Int) => a + 2)) + //prints None + println(Functor[Maybe].fmap(Just("2"))(_.toInt)) + //prints Just(2) +} diff --git a/Scala/Maybe2.scala b/Scala/Maybe2.scala new file mode 100644 index 0000000..8da398d --- /dev/null +++ b/Scala/Maybe2.scala @@ -0,0 +1,19 @@ +package maybe + +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 Maybe2 extends App { + import Maybe._ + import FunctorSyntax._ + + print(just("2").fmap(_.toDouble).fmap(a => a + 3)) + //prints Just(5.0) +} From 6a646f0a94a919f405ce916a7541eb03da77750a Mon Sep 17 00:00:00 2001 From: Oleh Dubynskiy Date: Mon, 10 Dec 2018 16:46:14 +0200 Subject: [PATCH 2/3] Refactor Names and add README --- Scala/Advanced/1-functor.scala | 49 +++++++++++++++++++++++++++ Scala/Advanced/2-functor-syntax.scala | 30 ++++++++++++++++ Scala/Advanced/README.md | 19 +++++++++++ Scala/Maybe1.scala | 43 ----------------------- Scala/Maybe2.scala | 19 ----------- 5 files changed, 98 insertions(+), 62 deletions(-) create mode 100644 Scala/Advanced/1-functor.scala create mode 100644 Scala/Advanced/2-functor-syntax.scala create mode 100644 Scala/Advanced/README.md delete mode 100644 Scala/Maybe1.scala delete mode 100644 Scala/Maybe2.scala 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..29b5077 --- /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 + */ + print(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 diff --git a/Scala/Maybe1.scala b/Scala/Maybe1.scala deleted file mode 100644 index 59e9dc4..0000000 --- a/Scala/Maybe1.scala +++ /dev/null @@ -1,43 +0,0 @@ -package maybe - -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 -} - -//Maybe typeclass that represents optional value. Value type is covariant -sealed trait Maybe[+T] -//Maybe child that holds value. Value type is covariant -case class Just[+T](value: T) extends Maybe[T] -//Maybe child that represents nothing -case object None extends Maybe[Nothing] - -object Maybe { - //Just factory method - def just[T](value: T): Maybe[T] = Just(value) - - //None factory method - def none[T]: Maybe[T] = None - - //Functor implementation for Maybe - implicit val maybeFunctor: Functor[Maybe] = new Functor[Maybe] { - override def fmap[A, B](fa: Maybe[A])(f: A => B): Maybe[B] = fa match { - case Just(v) => Just(f(v)) - case None => None - } - } -} - -object Main extends App { - println(Functor[Maybe].fmap(None)((a: Int) => a + 2)) - //prints None - println(Functor[Maybe].fmap(Just("2"))(_.toInt)) - //prints Just(2) -} diff --git a/Scala/Maybe2.scala b/Scala/Maybe2.scala deleted file mode 100644 index 8da398d..0000000 --- a/Scala/Maybe2.scala +++ /dev/null @@ -1,19 +0,0 @@ -package maybe - -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 Maybe2 extends App { - import Maybe._ - import FunctorSyntax._ - - print(just("2").fmap(_.toDouble).fmap(a => a + 3)) - //prints Just(5.0) -} From fc66ac44ac782839dbf606362d92a6dd798ecfa9 Mon Sep 17 00:00:00 2001 From: Oleh Dubynskiy Date: Mon, 10 Dec 2018 17:15:27 +0200 Subject: [PATCH 3/3] change print to println --- Scala/Advanced/2-functor-syntax.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Scala/Advanced/2-functor-syntax.scala b/Scala/Advanced/2-functor-syntax.scala index 29b5077..94bd367 100644 --- a/Scala/Advanced/2-functor-syntax.scala +++ b/Scala/Advanced/2-functor-syntax.scala @@ -25,6 +25,6 @@ object Main2 extends App { * 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 */ - print(some("2").fmap(_.toDouble).fmap(a => a + 3)) + println(some("2").fmap(_.toDouble).fmap(a => a + 3)) //prints Some(5.0) }