-
Notifications
You must be signed in to change notification settings - Fork 52
/
chA_ScalaCheatSheet.scala
216 lines (177 loc) · 6.6 KB
/
chA_ScalaCheatSheet.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import model.Artist
object chA_ScalaCheatSheet extends App {
// defining a value
val x: Int = 2022
val y: String = "YYY"
val z = true // Boolean inferred
// defining a function
def f(a: Int): Int = {
a + 1
}
def g(a: Int, b: String): Boolean = {
a == b.length && z
}
// calling a function
assert(f(x) == 2023)
assert(g(x, y) == false)
// creating immutable collections (e.g. Lists)
val list: List[Int] = List(1, 2, 3)
val set: Set[String] = Set("Hello", "World")
// passing function by name
assert(list.map(f) == List(2, 3, 4))
// passing an anonymous function
assert(list.filter(i => i > 1) == List(2, 3)) // double-arrow syntax for anonymous functions
// passing an anonymous 2-parameter function
// higher-order functions (ch4): e.g. foldLeft
// - anonymous function with two parameters ch4
assert(list.foldLeft(2020)((sum, i) => sum + i) == 2026)
// multiple parameter lists (currying) ch4
def h(a: Int)(b: List[Int]): Boolean = {
b.contains(a)
}
val foo: List[Int] => Boolean = h(2020)
// Math:
// - Int.MinValue ch4
// - Math.max ch4
assert(Math.max(Int.MinValue, 2022) == 2022)
// case class (product type) ch4
case class Book(title: String, numberOfChapters: Int)
val grokkingFp = Book("Grokking Functional Programming", 12)
// the dot syntax ch4
val books: List[Book] = List(grokkingFp, Book("Ferdydurke", 14))
assert(books.filter(book => book.numberOfChapters > 13) == List(Book("Ferdydurke", 14)))
// the underscore syntax ch4
assert(books.filter(_.numberOfChapters > 13) == List(Book("Ferdydurke", 14)))
// missing implementation: ??? ch5
def isThisBookAnyGood(book: Book) = ???
// string interpolation ch5
assert(s"Reading ${grokkingFp.title} now!" == "Reading Grokking Functional Programming now!")
// passing multi-line functions as arguments ch5
assert(
books.map(book =>
if (book.numberOfChapters > 12) s"${book.title} is a long book"
else s"${book.title} is a short book"
) == List("Grokking Functional Programming is a short book", "Ferdydurke is a long book")
)
// type inference and empty list of ints ch5
val emptyList1 = List.empty[Int] // or:
val emptyList2: List[Int] = List.empty
// type inference and helping the compiler set the type of List ch5
val listOfDoubles1 = List[Double](1, 2, 3)
val listOfDoubles2: List[Double] = List(1, 2, 3)
// for comprehension ch5
assert((for {
i <- List(1, 2)
book <- books
} yield s"Person #$i read ${book.title}") == List(
"Person #1 read Grokking Functional Programming",
"Person #1 read Ferdydurke",
"Person #2 read Grokking Functional Programming",
"Person #2 read Ferdydurke"
))
// objects as modules, objects as "bags" for types and functions, importing
object things {
case class Thing(value: Int, description: String)
def inflate(thing: Thing): Thing = thing.copy(value = thing.value + 2030)
}
assert(things.inflate(things.Thing(3, "Just a thing")) == things.Thing(2033, "Just a thing"))
// opaque type (newtype)
object model {
opaque type BookRating = Int
object BookRating {
// creating new value
def apply(rawRating: Int): BookRating = Math.max(0, Math.min(5, rawRating))
// extension functions
extension (a: BookRating) def value: Int = a
}
}
// importing everything from an object, import wildcard
import model._
// creating and using a value of an opaque type
val rating: BookRating = BookRating(5)
// rating / 2
// Error: rating / 2 value / is not a member of model.BookRating
val i: Int = rating.value / 2
assert(i == 2)
// sum with case objects (singletons), with product types (ADTs)
enum BookProgress {
case ToRead
case Reading(currentChapter: Int)
case Finished(rating: BookRating)
}
import BookProgress._
// pattern matching
def bookProgressUpdate(book: Book, bookProgress: BookProgress): String = {
bookProgress match {
case ToRead => s"I want to read ${book.title}"
case Finished(rating) => s"I just finished ${book.title}! It's $rating/5!"
case Reading(currentChapter) =>
if (currentChapter <= book.numberOfChapters / 2) s"I have started reading ${book.title}"
else s"I am finishing reading ${book.title}"
}
}
// naming parameters in case class constructor or functions (ch7)
val b = Book(title = "Grokking Functional Programming", numberOfChapters = 12)
assert(bookProgressUpdate(
book = b,
bookProgress = Reading(currentChapter = 13)
) == "I am finishing reading Grokking Functional Programming")
// trait, bag of functions
trait BagOfFunctions {
def f(x: Int): Boolean
def g(y: Book): Int
}
// creating instance of a trait
val bagOfFunctions = new BagOfFunctions {
def f(x: Int): Boolean = x == 1
def g(y: Book): Int = y.numberOfChapters * 2
}
assert(bagOfFunctions.f(2020) == false)
// turn Unit value in Scala (if a function returns Unit it means it does some impure things inside)
val unit: Unit = ()
// Map type intro p313
val book1 = Book("Grokking Functional Programming", 12)
val book2 = Book("Ferdydurke", 14)
val progressPerBook: Map[Book, BookProgress] = Map(
book1 -> Reading(currentChapter = 13),
book2 -> ToRead
)
// writing function that pattern match. we decided to use basic syntax in the book, but here are other ways you ca implement that in Scala
assert(progressPerBook.values.filter(bookProgress =>
bookProgress match {
case ToRead => false
case Reading(_) => false
case Finished(_) => true
}
) == List.empty)
assert(progressPerBook.values.filter(_ match {
case ToRead => false
case Reading(_) => false
case Finished(_) => true
}) == List.empty)
assert(progressPerBook.values.filter {
case ToRead => false
case Reading(_) => false
case Finished(_) => true
} == List.empty)
// _ inside for comprehension
// _ as an unnamed value in for comprehension, Unit
assert((for {
_ <- List(1, 2, 3)
book <- List(Book("A", 7), Book("B", 13))
} yield book.numberOfChapters) == List(7, 13, 7, 13, 7, 13))
import scala.concurrent.duration._
// - scala finiteduration 1.second 2.seconds mention
val duration: FiniteDuration = 1.second
val durations: List[FiniteDuration] =
List(100.millis, 2.seconds, 5.minutes, 500_000.hours) // big numbers in scala 400_000 p365
// multi-line strings
"""
|Thanks
|for
|making
|it
|this
|far!
|""".stripMargin
}