Skip to content

add Scala functor example #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Scala/Advanced/1-functor.scala
Original file line number Diff line number Diff line change
@@ -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)
}
30 changes: 30 additions & 0 deletions Scala/Advanced/2-functor-syntax.scala
Original file line number Diff line number Diff line change
@@ -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)
}
19 changes: 19 additions & 0 deletions Scala/Advanced/README.md
Original file line number Diff line number Diff line change
@@ -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/)