Skip to content

Commit 4d9d44c

Browse files
committed
Merge branch 'master' of github.com:HowProgrammingWorks/Functor
2 parents 53b1142 + 40e4917 commit 4d9d44c

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

Scala/Advanced/1-functor.scala

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package functor
2+
3+
import scala.language.higherKinds
4+
5+
//Functor typeclass with existential F type
6+
trait Functor[F[_]] {
7+
def fmap[A, B](fa: F[A])(f: A => B): F[B]
8+
}
9+
10+
object Functor {
11+
//Summons functor instance if it is in scope
12+
def apply[F[_]](implicit f: Functor[F]): Functor[F] = f
13+
}
14+
15+
//Option typeclass that represents optional value. Value type is covariant
16+
sealed trait Option[+A]
17+
//Option child that holds value. Value type is covariant
18+
case class Some[+A](get: A) extends Option[A]
19+
//Option child that represents nothing
20+
case object None extends Option[Nothing]
21+
22+
object Option {
23+
//Some factory method
24+
def some[T](value: T): Option[T] = Some(value)
25+
26+
//None factory method
27+
def none[T]: Option[T] = None
28+
29+
/*
30+
* Remember this factory methods and that
31+
* their return type is Option and not Some or None,
32+
* they will come in handy in second example ;)
33+
*/
34+
35+
//Functor implementation for Option
36+
implicit val optionFunctor: Functor[Option] = new Functor[Option] {
37+
override def fmap[A, B](fa: Option[A])(f: A => B): Option[B] = fa match {
38+
case Some(v) => Some(f(v))
39+
case None => None
40+
}
41+
}
42+
}
43+
44+
object Main1 extends App {
45+
println(Functor[Option].fmap(None)((a: Int) => a + 2))
46+
//prints None
47+
println(Functor[Option].fmap(Some("2"))(_.toInt))
48+
//prints Some(2)
49+
}

Scala/Advanced/2-functor-syntax.scala

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package functor
2+
3+
import scala.language.higherKinds
4+
5+
object FunctorSyntax {
6+
//Implicit Functor operations class
7+
implicit class FunctorOps[F[_], A](val obj: F[A]) extends AnyVal {
8+
def fmap[B](f: A => B)(implicit functor: Functor[F]): F[B] =
9+
functor.fmap(obj)(f)
10+
}
11+
}
12+
13+
object Main2 extends App {
14+
import Option._
15+
import FunctorSyntax._
16+
17+
/*
18+
* Option is created using factory method and there is reason for this.
19+
* For instance such example wont compile:
20+
*
21+
* Some(2).fmap(_ + 1)
22+
*
23+
* The reason is that it will search for Functor[Some]
24+
* and would not take into account Functor[Option]
25+
* that could be fixed by making Functor typeclass Invariant like it is fixed in Cats
26+
* but it is a little bit complicated for our simple example
27+
*/
28+
println(some("2").fmap(_.toDouble).fmap(a => a + 3))
29+
//prints Some(5.0)
30+
}

Scala/Advanced/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Description
2+
The most flexible way to implement functor in Scala is to define it as typeclass.
3+
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.
4+
5+
## How to Run
6+
First sources should be compiled to `.jar` file using `scalac` command:
7+
> scalac *.scala -d Functor.jar
8+
9+
Next the examples can be executed by their fully qualified name(either `functor.Main1` or `functor.Main2`):
10+
11+
> scala -cp Functor.jar functor.Main1
12+
13+
Another way to run examples is to use build tools like: sbt, gradle or maven
14+
15+
## Helpful resources
16+
* [Functor in Cats](https://typelevel.org/cats/typeclasses/functor.html)
17+
* [Functor in Scalaz](http://eed3si9n.com/learning-scalaz/Functor.html)
18+
* [Cats Functor exercises](https://www.scala-exercises.org/cats/functor)
19+
* [sbt website](https://www.scala-sbt.org/)

0 commit comments

Comments
 (0)