Skip to content

Update scalafmt-core to 2.5.3 #86

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

Merged
merged 1 commit into from
Jun 16, 2020
Merged
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
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=2.4.2
version=2.5.3
style = defaultWithAlign
maxColumn = 100

Expand Down
47 changes: 28 additions & 19 deletions src/main/scala/fpinscalalib/ErrorHandlingSection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ object ErrorHandlingSection
* Let's try it out:
*/
def optionMapAssert(res0: (Option[Employee]) => Option[String]): Unit = {
def lookupByName(name: String): Option[Employee] = name match {
case "Joe" => Some(Employee("Joe", "Finances", Some("Julie")))
case "Mary" => Some(Employee("Mary", "IT", None))
case "Izumi" => Some(Employee("Izumi", "IT", Some("Mary")))
case _ => None
}
def lookupByName(name: String): Option[Employee] =
name match {
case "Joe" => Some(Employee("Joe", "Finances", Some("Julie")))
case "Mary" => Some(Employee("Mary", "IT", None))
case "Izumi" => Some(Employee("Izumi", "IT", Some("Mary")))
case _ => None
}

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

def parseInt(a: String): Option[Int] = Try(a.toInt) match {
case Success(r) => Some(r)
case _ => None
}
def parseInt(a: String): Option[Int] =
Try(a.toInt) match {
case Success(r) => Some(r)
case _ => None
}

traverse(list1)(i => parseInt(i)) shouldBe res0
traverse(list2)(i => parseInt(i)) shouldBe res1
Expand All @@ -257,12 +259,13 @@ object ErrorHandlingSection
* `Either` type to obtain the department of each employee:
*/
def eitherMapAssert(res0: (Either[String, Employee]) => Either[String, String]): Unit = {
def lookupByNameViaEither(name: String): Either[String, Employee] = name match {
case "Joe" => Right(Employee("Joe", "Finances", Some("Julie")))
case "Mary" => Right(Employee("Mary", "IT", None))
case "Izumi" => Right(Employee("Izumi", "IT", Some("Mary")))
case _ => Left("Employee not found")
}
def lookupByNameViaEither(name: String): Either[String, Employee] =
name match {
case "Joe" => Right(Employee("Joe", "Finances", Some("Julie")))
case "Mary" => Right(Employee("Mary", "IT", None))
case "Izumi" => Right(Employee("Izumi", "IT", Some("Mary")))
case _ => Left("Employee not found")
}

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

Expand Down Expand Up @@ -348,9 +351,15 @@ object ErrorHandlingSection
def employeesShareDepartment(employeeA: Employee, employeeB: Employee) =
employeeA.department == employeeB.department

lookupByNameViaEither("Joe").map2(lookupByNameViaEither("Mary"))(employeesShareDepartment) shouldBe res0
lookupByNameViaEither("Mary").map2(lookupByNameViaEither("Izumi"))(employeesShareDepartment) shouldBe res1
lookupByNameViaEither("Foo").map2(lookupByNameViaEither("Izumi"))(employeesShareDepartment) shouldBe res2
lookupByNameViaEither("Joe").map2(lookupByNameViaEither("Mary"))(
employeesShareDepartment
) shouldBe res0
lookupByNameViaEither("Mary").map2(lookupByNameViaEither("Izumi"))(
employeesShareDepartment
) shouldBe res1
lookupByNameViaEither("Foo").map2(lookupByNameViaEither("Izumi"))(
employeesShareDepartment
) shouldBe res2
}

/**
Expand Down
21 changes: 11 additions & 10 deletions src/main/scala/fpinscalalib/FPinScalaLibrary.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ object FPinScalaLibrary extends Library {

override def color = Some("#E22D34")

override def sections = scala.collection.immutable.List(
GettingStartedWithFPSection,
FunctionalDataStructuresSection,
ErrorHandlingSection,
StrictnessAndLazinessSection,
FunctionalStateSection,
FunctionalParallelismSection,
PropertyBasedTestingSection,
ParserCombinatorsSection
)
override def sections =
scala.collection.immutable.List(
GettingStartedWithFPSection,
FunctionalDataStructuresSection,
ErrorHandlingSection,
StrictnessAndLazinessSection,
FunctionalStateSection,
FunctionalParallelismSection,
PropertyBasedTestingSection,
ParserCombinatorsSection
)

override def logoPath = "fp_in_scala"
}
38 changes: 21 additions & 17 deletions src/main/scala/fpinscalalib/FunctionalDataStructuresSection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ object FunctionalDataStructuresSection
* `setHead` follows a similar principle. Let's take a look at how it works:
*/
def listSetHeadAssert(res0: List[Int], res1: List[String]) = {
def setHead[A](l: List[A], h: A): List[A] = l match {
case Nil => sys.error("setHead on empty list")
case Cons(_, t) => Cons(h, t)
}
def setHead[A](l: List[A], h: A): List[A] =
l match {
case Nil => sys.error("setHead on empty list")
case Cons(_, t) => Cons(h, t)
}
setHead(List(1, 2, 3), 3) shouldBe res0
setHead(List("a", "b"), "c") shouldBe res1
}
Expand Down Expand Up @@ -246,7 +247,7 @@ object FunctionalDataStructuresSection
*
* In fact, we can write `foldLeft` in terms of `foldRight`, and the other way around:
*
* {{{ 
* {{{
* def foldRightViaFoldLeft[A,B](l: List[A], z: B)(f: (A,B) => B): B =
* foldLeft(reverse(l), z)((b,a) => f(a,b))
*
Expand Down Expand Up @@ -430,10 +431,11 @@ object FunctionalDataStructuresSection
* Let's try to implement a function `size` to count the number of nodes (leaves and branches) in a tree:
*/
def treeSizeAssert(res0: Int, res1: Int): Unit = {
def size[A](t: Tree[A]): Int = t match {
case Leaf(_) => res0
case Branch(l, r) => res1 + size(l) + size(r)
}
def size[A](t: Tree[A]): Int =
t match {
case Leaf(_) => res0
case Branch(l, r) => res1 + size(l) + size(r)
}

def t = Branch(Branch(Leaf(1), Leaf(2)), Leaf(3))
size(t) shouldBe 5
Expand All @@ -458,10 +460,11 @@ object FunctionalDataStructuresSection
* tree to any leaf.
*/
def treeDepthAssert(res0: Int, res1: Int): Unit = {
def depth[A](t: Tree[A]): Int = t match {
case Leaf(_) => res0
case Branch(l, r) => res1 + (depth(l) max depth(r))
}
def depth[A](t: Tree[A]): Int =
t match {
case Leaf(_) => res0
case Branch(l, r) => res1 + (depth(l) max depth(r))
}
def t = Branch(Branch(Leaf(1), Leaf(2)), Leaf(3))
depth(t) shouldBe 2
}
Expand All @@ -473,10 +476,11 @@ object FunctionalDataStructuresSection
* in a tree with a given function. Let's try it out in the following exercise:
*/
def treeMapAssert(res0: Branch[Int]): Unit = {
def map[A, B](t: Tree[A])(f: A => B): Tree[B] = t match {
case Leaf(a) => Leaf(f(a))
case Branch(l, r) => Branch(map(l)(f), map(r)(f))
}
def map[A, B](t: Tree[A])(f: A => B): Tree[B] =
t match {
case Leaf(a) => Leaf(f(a))
case Branch(l, r) => Branch(map(l)(f), map(r)(f))
}

def t = Branch(Branch(Leaf(1), Leaf(2)), Leaf(3))
Tree.map(t)(_ * 2) shouldBe res0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ object GettingStartedWithFPSection

isSorted(Array(1, 3, 5, 7), (x: Int, y: Int) => x < y) shouldBe res0
isSorted(Array(7, 5, 1, 3), (x: Int, y: Int) => x > y) shouldBe res1
isSorted(Array("Scala", "Exercises"), (x: String, y: String) => x.length < y.length) shouldBe res2
isSorted(
Array("Scala", "Exercises"),
(x: String, y: String) => x.length < y.length
) shouldBe res2
}

/**
Expand Down
46 changes: 25 additions & 21 deletions src/main/scala/fpinscalalib/StrictnessAndLazinessSection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ object StrictnessAndLazinessSection
* `Stream` to a `List` (which will force its evaluation):
*/
def streamToListAssert(res0: List[Int]): Unit = {
def toList[A](s: Stream[A]): List[A] = s match {
case Cons(h, t) => h() :: toList(t())
case _ => List()
}
def toList[A](s: Stream[A]): List[A] =
s match {
case Cons(h, t) => h() :: toList(t())
case _ => List()
}

val s = Stream(1, 2, 3)
toList(s) shouldBe res0
Expand All @@ -79,11 +80,12 @@ object StrictnessAndLazinessSection
* }}}
*/
def streamTakeAssert(res0: Int): Unit = {
def take[A](s: Stream[A], n: Int): Stream[A] = s match {
case Cons(h, t) if n > 0 => cons[A](h(), t().take(n - res0))
case Cons(h, _) if n == 0 => cons[A](h(), Stream.empty)
case _ => Stream.empty
}
def take[A](s: Stream[A], n: Int): Stream[A] =
s match {
case Cons(h, t) if n > 0 => cons[A](h(), t().take(n - res0))
case Cons(h, _) if n == 0 => cons[A](h(), Stream.empty)
case _ => Stream.empty
}

take(Stream(1, 2, 3), 2).toList shouldBe List(1, 2)
}
Expand All @@ -92,10 +94,11 @@ object StrictnessAndLazinessSection
* `drop` is similar to `take`, but skips the first `n` elements of a `Stream` instead:
*/
def streamDropAssert(res0: Int): Unit = {
def drop[A](s: Stream[A], n: Int): Stream[A] = s match {
case Cons(_, t) if n > 0 => t().drop(n - res0)
case _ => s
}
def drop[A](s: Stream[A], n: Int): Stream[A] =
s match {
case Cons(_, t) if n > 0 => t().drop(n - res0)
case _ => s
}

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

takeWhile(Stream(1, 2, 3, 4, 5), (x: Int) => x < 3).toList shouldBe res0
takeWhile(Stream(1, 2, 3, 4, 5), (x: Int) => x < 0).toList shouldBe res1
Expand Down Expand Up @@ -206,9 +210,9 @@ object StrictnessAndLazinessSection
// Apply map to the second element:
val step3 = cons(12, res2.map(_ + 10)).filter(_ % 2 == 0).toList
// Apply filter to the second element. Produce the first element of the result:
val step4 = 12 :: Stream(3, 4).map(_ + 10).filter(_ % 2 == 0).toList
val step5 = 12 :: cons(res3, res4.map(_ + 10)).filter(_ % 2 == 0).toList
val step6 = 12 :: Stream(4).map(_ + 10).filter(_ % 2 == 0).toList
val step4 = 12 :: Stream(3, 4).map(_ + 10).filter(_ % 2 == 0).toList
val step5 = 12 :: cons(res3, res4.map(_ + 10)).filter(_ % 2 == 0).toList
val step6 = 12 :: Stream(4).map(_ + 10).filter(_ % 2 == 0).toList
val step7 = 12 :: cons(res5, Stream[Int]().map(_ + 10)).filter(_ % 2 == 0).toList
// Apply filter to the fourth element and produce the final element of the result.
val step8 = 12 :: 14 :: Stream[Int]().map(_ + 10).filter(_ % 2 == 0).toList
Expand Down Expand Up @@ -242,7 +246,7 @@ object StrictnessAndLazinessSection
*/
def streamOnesAssert(res0: List[Int], res1: Boolean, res2: Boolean, res3: Boolean): Unit = {
ones.take(5).toList shouldBe res0
ones.exists(_ % 2 != 0) shouldBe res1
ones.exists(_ % 2 != 0) shouldBe res1
ones.map(_ + 1).exists(_ % 2 == 0) shouldBe res2
ones.forAll(_ != 1) shouldBe res3
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ object Either {

It's also possible to use `Either[List[E],_]` directly to accumulate errors, using different implementations of
helper functions like `map2` and `sequence`.
*/
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@ object ExampleHelper {
val mary = Employee("Mary", "IT", None)
val izumi = Employee("Izumi", "IT", Some("Jaime"))

def lookupByName(name: String): Option[Employee] = name match {
case "Joe" => Some(joe)
case "Mary" => Some(mary)
case "Izumi" => Some(izumi)
case _ => None
}
def lookupByName(name: String): Option[Employee] =
name match {
case "Joe" => Some(joe)
case "Mary" => Some(mary)
case "Izumi" => Some(izumi)
case _ => None
}

def lookupByNameViaEither(name: String): Either[String, Employee] = name match {
case "Joe" => Right(joe)
case "Mary" => Right(mary)
case "Izumi" => Right(izumi)
case _ => Left("Employee not found")
}
def lookupByNameViaEither(name: String): Either[String, Employee] =
name match {
case "Joe" => Right(joe)
case "Mary" => Right(mary)
case "Izumi" => Right(izumi)
case _ => Left("Employee not found")
}

def Try[A](a: => A): Option[A] =
try Some(a)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,47 @@ package fpinscalalib.customlib.errorhandling
import scala.{Either => _, Option => _, Some => _}

sealed trait Option[+A] {
def map[B](f: A => B): Option[B] = this match {
case None => None
case Some(a) => Some(f(a))
}
def map[B](f: A => B): Option[B] =
this match {
case None => None
case Some(a) => Some(f(a))
}

def getOrElse[B >: A](default: => B): B = this match {
case None => default
case Some(a) => a
}
def getOrElse[B >: A](default: => B): B =
this match {
case None => default
case Some(a) => a
}

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

/*
Of course, we can also implement `flatMap` with explicit pattern matching.
*/
def flatMap_1[B](f: A => Option[B]): Option[B] = this match {
case None => None
case Some(a) => f(a)
}
def flatMap_1[B](f: A => Option[B]): Option[B] =
this match {
case None => None
case Some(a) => f(a)
}

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

/*
Again, we can implement this with explicit pattern matching.
*/
def orElse_1[B >: A](ob: => Option[B]): Option[B] = this match {
case None => ob
case _ => this
}
def orElse_1[B >: A](ob: => Option[B]): Option[B] =
this match {
case None => ob
case _ => this
}

def filter(f: A => Boolean): Option[A] = this match {
case Some(a) if f(a) => this
case _ => None
}
def filter(f: A => Boolean): Option[A] =
this match {
case Some(a) if f(a) => this
case _ => None
}
/*
This can also be defined in terms of `flatMap`.
*/
Expand Down
Loading