Skip to content

Added Scala examples #9

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions Scala/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
target/
*.iml
25 changes: 25 additions & 0 deletions Scala/1-functor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Simple functor example
*/

case class Functor[A](x: A) {
def map[B](f: (A) => B): Functor[B] = Functor(f(x))
}

object Example {

def main(args: Array[String]): Unit = {
val functor = Functor(2)

println("Functor rules")

println("1. functor.map(x => x) = functor")
println(s"functor.map(x => x) = ${functor.map(x => x)}")
println(s"functor = $functor")

println("2. functor.map(x => f(g(x))) = functor.map(g).map(f)")
println(s"functor.map(x => f(g(x))) = ${functor.map(x => (x * 2) * 3)}")
println(s"functor.map(g).map(f) = ${functor.map(_ * 2).map(_ * 3)}")

}
}
17 changes: 17 additions & 0 deletions Scala/2-not-functor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* NOT a functor because map method returns just value, not ValueMapper
*/

case class ValueMapper[A](x: A) {
def map[B](f: (A) => B): B =
f(x)
}

object NotFunctorExample {

def main(args: Array[String]): Unit = {
val mapper = ValueMapper("Hello" -> 123)

println(mapper.map(_._1.toUpperCase))
}
}
27 changes: 27 additions & 0 deletions Scala/3-maybe.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
case class Maybe[A](x: Option[A]) {
def map[B](f: (A) => B): Maybe[B] =
Maybe(x.map(f))

def isNothing = x.isEmpty

override def toString: String =
if (isNothing) "Maybe(Nothing)"
else s"Maybe(${x.get})"
}

object Maybe {
def apply[A](x: A): Maybe[A] = Maybe(Some(x))

def empty[A]: Maybe[A] = Maybe[A](None)
}

object ExampleMaybe {

def main(args: Array[String]): Unit = {
val empty = Maybe.empty[Int]
val notEmpty = Maybe(2)

println(s"Expect Nothing, actual = ${empty.map(_ => 3)}")
println(s"Expect Maybe(3), actual = ${notEmpty.map(_ => 3)}")
}
}
4 changes: 4 additions & 0 deletions Scala/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Scala examples of Functor
Some great articles:
1. [What is a functor?](https://medium.com/@dtinth/what-is-a-functor-dcf510b098b6)
2. [What's NOT a functor](https://medium.com/@dtinth/what-s-not-a-functor-57a081131555)