-
Notifications
You must be signed in to change notification settings - Fork 17
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ | ||
print(some("2").fmap(_.toDouble).fmap(a => a + 3)) | ||
//prints Some(5.0) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.