Skip to content

Commit 98f72d5

Browse files
committed
Introduction to Scalaz
1 parent d2a6284 commit 98f72d5

File tree

3 files changed

+521
-1
lines changed

3 files changed

+521
-1
lines changed

build.sbt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,17 @@ lazy val scala_akka = (project in file("scala-akka"))
9090
)
9191

9292
val monocleVersion = "2.0.4"
93+
val scalazVersion = "7.3.2"
9394
lazy val scala_libraries = (project in file("scala-libraries"))
9495
.settings(
9596
name := "scala-libraries",
9697
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
9798
libraryDependencies ++= Seq(
9899
"com.github.julien-truffaut" %% "monocle-core" % monocleVersion,
99100
"com.github.julien-truffaut" %% "monocle-macro" % monocleVersion,
100-
"com.github.julien-truffaut" %% "monocle-law" % monocleVersion % "test"
101+
"com.github.julien-truffaut" %% "monocle-law" % monocleVersion % "test",
102+
"org.scalaz" %% "scalaz-core" % scalazVersion,
103+
"junit" % "junit" % "4.13" % Test
101104
)
102105
)
103106

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.scala.scalaz
2+
3+
import scalaz._
4+
5+
object ScalazExamples {
6+
case class UserId(id: Long)
7+
case class FullName(fname: String, lname: String)
8+
case class User(id: UserId, name: FullName)
9+
val userFullName = Lens.lensu[User, FullName](
10+
(user, name) => user.copy(name = name),
11+
_.name
12+
)
13+
val firstName = Lens.lensu[FullName, String](
14+
(fullName, firstName) => fullName.copy(fname = firstName),
15+
_.fname
16+
)
17+
18+
val lastName = Lens.lensu[FullName, String](
19+
(fullName, lastName) => fullName.copy(lname = lastName),
20+
_.lname
21+
)
22+
val userFirstName = userFullName >=> firstName
23+
val userLastName = userFullName >=> lastName
24+
25+
implicit object threadShow extends Show[Thread] {
26+
override def show(f: Thread): Cord =
27+
Cord(s"Thread id = ${f.getId}, name = ${f.getName}")
28+
}
29+
30+
case class Score(amt: Double)
31+
implicit object scoreOrdered extends Order[Score] {
32+
override def order(x: Score, y: Score): Ordering =
33+
x.amt compare y.amt match {
34+
case 1 => Ordering.GT
35+
case 0 => Ordering.EQ
36+
case -1 => Ordering.LT
37+
}
38+
}
39+
40+
//enum
41+
case class Priority(num: Int, name: String)
42+
val HIGH = Priority(3, "HIGH")
43+
val MEDIUM = Priority(2, "MEDIUM")
44+
val LOW = Priority(1, "LOW")
45+
46+
implicit object PriorityEnum extends Enum[Priority] {
47+
def order(p1: Priority, p2: Priority): Ordering =
48+
(p1.num compare p2.num) match {
49+
case -1 => Ordering.LT
50+
case 0 => Ordering.EQ
51+
case 1 => Ordering.GT
52+
}
53+
54+
def succ(s: Priority): Priority = s match {
55+
case LOW => MEDIUM
56+
case MEDIUM => HIGH
57+
case HIGH => LOW
58+
}
59+
60+
def pred(s: Priority): Priority = s match {
61+
case LOW => HIGH
62+
case MEDIUM => LOW
63+
case HIGH => MEDIUM
64+
}
65+
66+
override def max = Some(HIGH)
67+
override def min = Some(LOW)
68+
}
69+
}

0 commit comments

Comments
 (0)