Skip to content

Commit 6fb54ce

Browse files
committed
Update scalafmt-core to 2.5.3
1 parent 7f32e93 commit 6fb54ce

19 files changed

+382
-294
lines changed

.scalafmt.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version=2.4.2
1+
version=2.5.3
22
style = defaultWithAlign
33
maxColumn = 100
44

src/main/scala/fpinscalalib/ErrorHandlingSection.scala

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ object ErrorHandlingSection
5858
* Let's try it out:
5959
*/
6060
def optionMapAssert(res0: (Option[Employee]) => Option[String]): Unit = {
61-
def lookupByName(name: String): Option[Employee] = name match {
62-
case "Joe" => Some(Employee("Joe", "Finances", Some("Julie")))
63-
case "Mary" => Some(Employee("Mary", "IT", None))
64-
case "Izumi" => Some(Employee("Izumi", "IT", Some("Mary")))
65-
case _ => None
66-
}
61+
def lookupByName(name: String): Option[Employee] =
62+
name match {
63+
case "Joe" => Some(Employee("Joe", "Finances", Some("Julie")))
64+
case "Mary" => Some(Employee("Mary", "IT", None))
65+
case "Izumi" => Some(Employee("Izumi", "IT", Some("Mary")))
66+
case _ => None
67+
}
6768

6869
/*
6970
We can look for our employees, and try to obtain their departments. We will assume that we won't find any errors,
@@ -227,10 +228,11 @@ object ErrorHandlingSection
227228
val list1 = List("1", "2", "3")
228229
val list2 = List("I", "II", "III", "IV")
229230

230-
def parseInt(a: String): Option[Int] = Try(a.toInt) match {
231-
case Success(r) => Some(r)
232-
case _ => None
233-
}
231+
def parseInt(a: String): Option[Int] =
232+
Try(a.toInt) match {
233+
case Success(r) => Some(r)
234+
case _ => None
235+
}
234236

235237
traverse(list1)(i => parseInt(i)) shouldBe res0
236238
traverse(list2)(i => parseInt(i)) shouldBe res1
@@ -257,12 +259,13 @@ object ErrorHandlingSection
257259
* `Either` type to obtain the department of each employee:
258260
*/
259261
def eitherMapAssert(res0: (Either[String, Employee]) => Either[String, String]): Unit = {
260-
def lookupByNameViaEither(name: String): Either[String, Employee] = name match {
261-
case "Joe" => Right(Employee("Joe", "Finances", Some("Julie")))
262-
case "Mary" => Right(Employee("Mary", "IT", None))
263-
case "Izumi" => Right(Employee("Izumi", "IT", Some("Mary")))
264-
case _ => Left("Employee not found")
265-
}
262+
def lookupByNameViaEither(name: String): Either[String, Employee] =
263+
name match {
264+
case "Joe" => Right(Employee("Joe", "Finances", Some("Julie")))
265+
case "Mary" => Right(Employee("Mary", "IT", None))
266+
case "Izumi" => Right(Employee("Izumi", "IT", Some("Mary")))
267+
case _ => Left("Employee not found")
268+
}
266269

267270
def getDepartment: (Either[String, Employee]) => Either[String, String] = res0
268271

@@ -348,9 +351,15 @@ object ErrorHandlingSection
348351
def employeesShareDepartment(employeeA: Employee, employeeB: Employee) =
349352
employeeA.department == employeeB.department
350353

351-
lookupByNameViaEither("Joe").map2(lookupByNameViaEither("Mary"))(employeesShareDepartment) shouldBe res0
352-
lookupByNameViaEither("Mary").map2(lookupByNameViaEither("Izumi"))(employeesShareDepartment) shouldBe res1
353-
lookupByNameViaEither("Foo").map2(lookupByNameViaEither("Izumi"))(employeesShareDepartment) shouldBe res2
354+
lookupByNameViaEither("Joe").map2(lookupByNameViaEither("Mary"))(
355+
employeesShareDepartment
356+
) shouldBe res0
357+
lookupByNameViaEither("Mary").map2(lookupByNameViaEither("Izumi"))(
358+
employeesShareDepartment
359+
) shouldBe res1
360+
lookupByNameViaEither("Foo").map2(lookupByNameViaEither("Izumi"))(
361+
employeesShareDepartment
362+
) shouldBe res2
354363
}
355364

356365
/**

src/main/scala/fpinscalalib/FPinScalaLibrary.scala

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ object FPinScalaLibrary extends Library {
2828

2929
override def color = Some("#E22D34")
3030

31-
override def sections = scala.collection.immutable.List(
32-
GettingStartedWithFPSection,
33-
FunctionalDataStructuresSection,
34-
ErrorHandlingSection,
35-
StrictnessAndLazinessSection,
36-
FunctionalStateSection,
37-
FunctionalParallelismSection,
38-
PropertyBasedTestingSection,
39-
ParserCombinatorsSection
40-
)
31+
override def sections =
32+
scala.collection.immutable.List(
33+
GettingStartedWithFPSection,
34+
FunctionalDataStructuresSection,
35+
ErrorHandlingSection,
36+
StrictnessAndLazinessSection,
37+
FunctionalStateSection,
38+
FunctionalParallelismSection,
39+
PropertyBasedTestingSection,
40+
ParserCombinatorsSection
41+
)
4142

4243
override def logoPath = "fp_in_scala"
4344
}

src/main/scala/fpinscalalib/FunctionalDataStructuresSection.scala

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,11 @@ object FunctionalDataStructuresSection
8383
* `setHead` follows a similar principle. Let's take a look at how it works:
8484
*/
8585
def listSetHeadAssert(res0: List[Int], res1: List[String]) = {
86-
def setHead[A](l: List[A], h: A): List[A] = l match {
87-
case Nil => sys.error("setHead on empty list")
88-
case Cons(_, t) => Cons(h, t)
89-
}
86+
def setHead[A](l: List[A], h: A): List[A] =
87+
l match {
88+
case Nil => sys.error("setHead on empty list")
89+
case Cons(_, t) => Cons(h, t)
90+
}
9091
setHead(List(1, 2, 3), 3) shouldBe res0
9192
setHead(List("a", "b"), "c") shouldBe res1
9293
}
@@ -246,7 +247,7 @@ object FunctionalDataStructuresSection
246247
*
247248
* In fact, we can write `foldLeft` in terms of `foldRight`, and the other way around:
248249
*
249-
* {{{ 
250+
* {{{
250251
* def foldRightViaFoldLeft[A,B](l: List[A], z: B)(f: (A,B) => B): B =
251252
* foldLeft(reverse(l), z)((b,a) => f(a,b))
252253
*
@@ -430,10 +431,11 @@ object FunctionalDataStructuresSection
430431
* Let's try to implement a function `size` to count the number of nodes (leaves and branches) in a tree:
431432
*/
432433
def treeSizeAssert(res0: Int, res1: Int): Unit = {
433-
def size[A](t: Tree[A]): Int = t match {
434-
case Leaf(_) => res0
435-
case Branch(l, r) => res1 + size(l) + size(r)
436-
}
434+
def size[A](t: Tree[A]): Int =
435+
t match {
436+
case Leaf(_) => res0
437+
case Branch(l, r) => res1 + size(l) + size(r)
438+
}
437439

438440
def t = Branch(Branch(Leaf(1), Leaf(2)), Leaf(3))
439441
size(t) shouldBe 5
@@ -458,10 +460,11 @@ object FunctionalDataStructuresSection
458460
* tree to any leaf.
459461
*/
460462
def treeDepthAssert(res0: Int, res1: Int): Unit = {
461-
def depth[A](t: Tree[A]): Int = t match {
462-
case Leaf(_) => res0
463-
case Branch(l, r) => res1 + (depth(l) max depth(r))
464-
}
463+
def depth[A](t: Tree[A]): Int =
464+
t match {
465+
case Leaf(_) => res0
466+
case Branch(l, r) => res1 + (depth(l) max depth(r))
467+
}
465468
def t = Branch(Branch(Leaf(1), Leaf(2)), Leaf(3))
466469
depth(t) shouldBe 2
467470
}
@@ -473,10 +476,11 @@ object FunctionalDataStructuresSection
473476
* in a tree with a given function. Let's try it out in the following exercise:
474477
*/
475478
def treeMapAssert(res0: Branch[Int]): Unit = {
476-
def map[A, B](t: Tree[A])(f: A => B): Tree[B] = t match {
477-
case Leaf(a) => Leaf(f(a))
478-
case Branch(l, r) => Branch(map(l)(f), map(r)(f))
479-
}
479+
def map[A, B](t: Tree[A])(f: A => B): Tree[B] =
480+
t match {
481+
case Leaf(a) => Leaf(f(a))
482+
case Branch(l, r) => Branch(map(l)(f), map(r)(f))
483+
}
480484

481485
def t = Branch(Branch(Leaf(1), Leaf(2)), Leaf(3))
482486
Tree.map(t)(_ * 2) shouldBe res0

src/main/scala/fpinscalalib/GettingStartedWithFPSection.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ object GettingStartedWithFPSection
7676

7777
isSorted(Array(1, 3, 5, 7), (x: Int, y: Int) => x < y) shouldBe res0
7878
isSorted(Array(7, 5, 1, 3), (x: Int, y: Int) => x > y) shouldBe res1
79-
isSorted(Array("Scala", "Exercises"), (x: String, y: String) => x.length < y.length) shouldBe res2
79+
isSorted(
80+
Array("Scala", "Exercises"),
81+
(x: String, y: String) => x.length < y.length
82+
) shouldBe res2
8083
}
8184

8285
/**

src/main/scala/fpinscalalib/StrictnessAndLazinessSection.scala

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ object StrictnessAndLazinessSection
5353
* `Stream` to a `List` (which will force its evaluation):
5454
*/
5555
def streamToListAssert(res0: List[Int]): Unit = {
56-
def toList[A](s: Stream[A]): List[A] = s match {
57-
case Cons(h, t) => h() :: toList(t())
58-
case _ => List()
59-
}
56+
def toList[A](s: Stream[A]): List[A] =
57+
s match {
58+
case Cons(h, t) => h() :: toList(t())
59+
case _ => List()
60+
}
6061

6162
val s = Stream(1, 2, 3)
6263
toList(s) shouldBe res0
@@ -79,11 +80,12 @@ object StrictnessAndLazinessSection
7980
* }}}
8081
*/
8182
def streamTakeAssert(res0: Int): Unit = {
82-
def take[A](s: Stream[A], n: Int): Stream[A] = s match {
83-
case Cons(h, t) if n > 0 => cons[A](h(), t().take(n - res0))
84-
case Cons(h, _) if n == 0 => cons[A](h(), Stream.empty)
85-
case _ => Stream.empty
86-
}
83+
def take[A](s: Stream[A], n: Int): Stream[A] =
84+
s match {
85+
case Cons(h, t) if n > 0 => cons[A](h(), t().take(n - res0))
86+
case Cons(h, _) if n == 0 => cons[A](h(), Stream.empty)
87+
case _ => Stream.empty
88+
}
8789

8890
take(Stream(1, 2, 3), 2).toList shouldBe List(1, 2)
8991
}
@@ -92,10 +94,11 @@ object StrictnessAndLazinessSection
9294
* `drop` is similar to `take`, but skips the first `n` elements of a `Stream` instead:
9395
*/
9496
def streamDropAssert(res0: Int): Unit = {
95-
def drop[A](s: Stream[A], n: Int): Stream[A] = s match {
96-
case Cons(_, t) if n > 0 => t().drop(n - res0)
97-
case _ => s
98-
}
97+
def drop[A](s: Stream[A], n: Int): Stream[A] =
98+
s match {
99+
case Cons(_, t) if n > 0 => t().drop(n - res0)
100+
case _ => s
101+
}
99102

100103
drop(Stream(1, 2, 3, 4, 5), 2).toList shouldBe List(3, 4, 5)
101104
}
@@ -106,10 +109,11 @@ object StrictnessAndLazinessSection
106109
* We can also implement `takeWhile` to return all starting elements of a `Stream` that match a given predicate:
107110
*/
108111
def streamTakeWhileAssert(res0: List[Int], res1: List[Int]): Unit = {
109-
def takeWhile[A](s: Stream[A], f: A => Boolean): Stream[A] = s match {
110-
case Cons(h, t) if f(h()) => cons(h(), t() takeWhile f)
111-
case _ => Stream.empty
112-
}
112+
def takeWhile[A](s: Stream[A], f: A => Boolean): Stream[A] =
113+
s match {
114+
case Cons(h, t) if f(h()) => cons(h(), t() takeWhile f)
115+
case _ => Stream.empty
116+
}
113117

114118
takeWhile(Stream(1, 2, 3, 4, 5), (x: Int) => x < 3).toList shouldBe res0
115119
takeWhile(Stream(1, 2, 3, 4, 5), (x: Int) => x < 0).toList shouldBe res1
@@ -206,9 +210,9 @@ object StrictnessAndLazinessSection
206210
// Apply map to the second element:
207211
val step3 = cons(12, res2.map(_ + 10)).filter(_ % 2 == 0).toList
208212
// Apply filter to the second element. Produce the first element of the result:
209-
val step4 = 12 :: Stream(3, 4).map(_ + 10).filter(_ % 2 == 0).toList
210-
val step5 = 12 :: cons(res3, res4.map(_ + 10)).filter(_ % 2 == 0).toList
211-
val step6 = 12 :: Stream(4).map(_ + 10).filter(_ % 2 == 0).toList
213+
val step4 = 12 :: Stream(3, 4).map(_ + 10).filter(_ % 2 == 0).toList
214+
val step5 = 12 :: cons(res3, res4.map(_ + 10)).filter(_ % 2 == 0).toList
215+
val step6 = 12 :: Stream(4).map(_ + 10).filter(_ % 2 == 0).toList
212216
val step7 = 12 :: cons(res5, Stream[Int]().map(_ + 10)).filter(_ % 2 == 0).toList
213217
// Apply filter to the fourth element and produce the final element of the result.
214218
val step8 = 12 :: 14 :: Stream[Int]().map(_ + 10).filter(_ % 2 == 0).toList
@@ -242,7 +246,7 @@ object StrictnessAndLazinessSection
242246
*/
243247
def streamOnesAssert(res0: List[Int], res1: Boolean, res2: Boolean, res3: Boolean): Unit = {
244248
ones.take(5).toList shouldBe res0
245-
ones.exists(_ % 2 != 0) shouldBe res1
249+
ones.exists(_ % 2 != 0) shouldBe res1
246250
ones.map(_ + 1).exists(_ % 2 == 0) shouldBe res2
247251
ones.forAll(_ != 1) shouldBe res3
248252
}

src/main/scala/fpinscalalib/customlib/errorhandling/EitherHelper.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,5 @@ object Either {
9090
9191
It's also possible to use `Either[List[E],_]` directly to accumulate errors, using different implementations of
9292
helper functions like `map2` and `sequence`.
93-
*/
93+
*/
9494
}

src/main/scala/fpinscalalib/customlib/errorhandling/ExampleHelper.scala

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@ object ExampleHelper {
2323
val mary = Employee("Mary", "IT", None)
2424
val izumi = Employee("Izumi", "IT", Some("Jaime"))
2525

26-
def lookupByName(name: String): Option[Employee] = name match {
27-
case "Joe" => Some(joe)
28-
case "Mary" => Some(mary)
29-
case "Izumi" => Some(izumi)
30-
case _ => None
31-
}
26+
def lookupByName(name: String): Option[Employee] =
27+
name match {
28+
case "Joe" => Some(joe)
29+
case "Mary" => Some(mary)
30+
case "Izumi" => Some(izumi)
31+
case _ => None
32+
}
3233

33-
def lookupByNameViaEither(name: String): Either[String, Employee] = name match {
34-
case "Joe" => Right(joe)
35-
case "Mary" => Right(mary)
36-
case "Izumi" => Right(izumi)
37-
case _ => Left("Employee not found")
38-
}
34+
def lookupByNameViaEither(name: String): Either[String, Employee] =
35+
name match {
36+
case "Joe" => Right(joe)
37+
case "Mary" => Right(mary)
38+
case "Izumi" => Right(izumi)
39+
case _ => Left("Employee not found")
40+
}
3941

4042
def Try[A](a: => A): Option[A] =
4143
try Some(a)

src/main/scala/fpinscalalib/customlib/errorhandling/OptionHelper.scala

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,47 @@ package fpinscalalib.customlib.errorhandling
2727
import scala.{Either => _, Option => _, Some => _}
2828

2929
sealed trait Option[+A] {
30-
def map[B](f: A => B): Option[B] = this match {
31-
case None => None
32-
case Some(a) => Some(f(a))
33-
}
30+
def map[B](f: A => B): Option[B] =
31+
this match {
32+
case None => None
33+
case Some(a) => Some(f(a))
34+
}
3435

35-
def getOrElse[B >: A](default: => B): B = this match {
36-
case None => default
37-
case Some(a) => a
38-
}
36+
def getOrElse[B >: A](default: => B): B =
37+
this match {
38+
case None => default
39+
case Some(a) => a
40+
}
3941

4042
def flatMap[B](f: A => Option[B]): Option[B] =
4143
map(f) getOrElse None
4244

4345
/*
4446
Of course, we can also implement `flatMap` with explicit pattern matching.
4547
*/
46-
def flatMap_1[B](f: A => Option[B]): Option[B] = this match {
47-
case None => None
48-
case Some(a) => f(a)
49-
}
48+
def flatMap_1[B](f: A => Option[B]): Option[B] =
49+
this match {
50+
case None => None
51+
case Some(a) => f(a)
52+
}
5053

5154
def orElse[B >: A](ob: => Option[B]): Option[B] =
5255
this map (Some(_)) getOrElse ob
5356

5457
/*
5558
Again, we can implement this with explicit pattern matching.
5659
*/
57-
def orElse_1[B >: A](ob: => Option[B]): Option[B] = this match {
58-
case None => ob
59-
case _ => this
60-
}
60+
def orElse_1[B >: A](ob: => Option[B]): Option[B] =
61+
this match {
62+
case None => ob
63+
case _ => this
64+
}
6165

62-
def filter(f: A => Boolean): Option[A] = this match {
63-
case Some(a) if f(a) => this
64-
case _ => None
65-
}
66+
def filter(f: A => Boolean): Option[A] =
67+
this match {
68+
case Some(a) if f(a) => this
69+
case _ => None
70+
}
6671
/*
6772
This can also be defined in terms of `flatMap`.
6873
*/

0 commit comments

Comments
 (0)