From efe29cc831ad29910cbae77e19e4c63d318b2167 Mon Sep 17 00:00:00 2001 From: Zhenia Trochun Date: Sun, 10 Jun 2018 16:44:48 +0300 Subject: [PATCH] Added Scala examples --- Scala/.gitignore | 3 +++ Scala/1-functor.scala | 25 +++++++++++++++++++++++++ Scala/2-not-functor.scala | 17 +++++++++++++++++ Scala/3-maybe.scala | 27 +++++++++++++++++++++++++++ Scala/README.md | 4 ++++ 5 files changed, 76 insertions(+) create mode 100644 Scala/.gitignore create mode 100644 Scala/1-functor.scala create mode 100644 Scala/2-not-functor.scala create mode 100644 Scala/3-maybe.scala create mode 100644 Scala/README.md diff --git a/Scala/.gitignore b/Scala/.gitignore new file mode 100644 index 0000000..b4a5277 --- /dev/null +++ b/Scala/.gitignore @@ -0,0 +1,3 @@ +.idea/ +target/ +*.iml diff --git a/Scala/1-functor.scala b/Scala/1-functor.scala new file mode 100644 index 0000000..b960d1f --- /dev/null +++ b/Scala/1-functor.scala @@ -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)}") + + } +} diff --git a/Scala/2-not-functor.scala b/Scala/2-not-functor.scala new file mode 100644 index 0000000..79b9c8a --- /dev/null +++ b/Scala/2-not-functor.scala @@ -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)) + } +} diff --git a/Scala/3-maybe.scala b/Scala/3-maybe.scala new file mode 100644 index 0000000..de54c76 --- /dev/null +++ b/Scala/3-maybe.scala @@ -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)}") + } +} diff --git a/Scala/README.md b/Scala/README.md new file mode 100644 index 0000000..828373b --- /dev/null +++ b/Scala/README.md @@ -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) \ No newline at end of file