Skip to content

Commit

Permalink
Upgrade scalafmt in order to get SIP-64 syntax support
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhijit Sarkar committed Dec 29, 2024
1 parent d684f6c commit 0bb7837
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 64 deletions.
11 changes: 1 addition & 10 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
version = "3.8.3"
version = "3.8.4-RC4"
align.preset = more
maxColumn = 120
runner.dialect = scala3
assumeStandardLibraryStripMargin = true
# https://github.com/scalameta/scalameta/issues/4090
project.excludePaths = [
"glob:**/ch04/src/**.scala",
"glob:**/ch06/src/Cat.scala",
"glob:**/ch07/src/*.scala",
"glob:**/ch08/src/*.scala",
"glob:**/ch09/src/*.scala",
"glob:**/ch09/src/*.sc",
]
4 changes: 2 additions & 2 deletions ch04/src/Cat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ final case class Cat(name: String, age: Int, color: String)
object Cat:
given catDisplay: Display[Cat]:
def display(cat: Cat): String =
val name = Display.display(cat.name)
val age = Display.display(cat.age)
val name = Display.display(cat.name)
val age = Display.display(cat.age)
val color = Display.display(cat.color)
s"$name is a $age year-old $color cat."
5 changes: 2 additions & 3 deletions ch04/src/Display.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package ch04

/*
4.5 Exercise: Display Library
*/
*/
trait Display[A]:
def display(value: A): String


object Display:
given stringDisplay: Display[String]:
def display(input: String): String = input
Expand All @@ -23,4 +22,4 @@ object Display:
object DisplaySyntax:
extension [A](value: A)(using p: Display[A])
def display: String = p.display(value)
def print: Unit = Display.print(value)
def print: Unit = Display.print(value)
10 changes: 5 additions & 5 deletions ch06/src/Cat.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package ch06

import cats.{Eq, Show}
import cats.syntax.show.toShow // A.show if Show[A] exists
import cats.syntax.eq.catsSyntaxEq // ===
import cats.syntax.show.toShow // A.show if Show[A] exists
import cats.syntax.eq.catsSyntaxEq // ===

final case class Cat(name: String, age: Int, color: String)

object Cat:
/*
6.2.1.1 Exercise: Cat Show
Re-implement the Cat application from Section 4.5.1 using Show instead of Display.
*/
*/
given Show[Cat]:
override def show(cat: Cat): String =
val name = cat.name.show
Expand All @@ -21,9 +21,9 @@ object Cat:
/*
6.3.4.1 Exercise: Equality, Liberty, and Felinity
Implement an instance of Eq for our running Cat example.
*/
*/
given Eq[Cat]:
override def eqv(x: Cat, y: Cat): Boolean =
(x.name === y.name) &&
(x.age === y.age) &&
(x.color === y.color)
(x.color === y.color)
8 changes: 4 additions & 4 deletions ch07/src/Lib.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ch07

/*
We can use Semigroups and Monoids by importing two things: the type classes themselves,
We can use Semigroups and Monoids by importing two things: the type classes themselves,
and the semigroup syntax to give us the |+| operator.
*/
import cats.syntax.semigroup.catsSyntaxSemigroup // A |+| A if Semigroup[A] exists
*/
import cats.syntax.semigroup.catsSyntaxSemigroup // A |+| A if Semigroup[A] exists

object Lib:

Expand Down Expand Up @@ -47,4 +47,4 @@ object Lib:
o1.quantity + o2.quantity
)

def empty = Order(0, 0)
def empty = Order(0, 0)
6 changes: 3 additions & 3 deletions ch07/src/Monoid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ trait Monoid[A] extends Semigroup[A]:
def empty: A

object Monoid:
def apply[A : Monoid as m]: Monoid[A] = m
def apply[A: Monoid as m]: Monoid[A] = m

/*
7.2.0.1 Exercise: The Truth About Monoids
Expand Down Expand Up @@ -33,12 +33,12 @@ object Monoid:
/*
7.2.0.2 Exercise: All Set for Monoids
What monoids and semigroups are there for sets?
*/
*/
given setUnionMonoid: [A] => Monoid[Set[A]]:
def combine(a: Set[A], b: Set[A]) = a.union(b)
def empty = Set.empty[A]

given symDiffMonoid: [A] => Monoid[Set[A]]:
def combine(a: Set[A], b: Set[A]): Set[A] =
(a.diff(b)).union(b.diff(a))
def empty: Set[A] = Set.empty
def empty: Set[A] = Set.empty
6 changes: 3 additions & 3 deletions ch08/src/Codec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ trait Codec[A]:
override def decode(value: String): B =
dec(self.decode(value))

def encode[A : Codec as c](value: A): String =
def encode[A: Codec as c](value: A): String =
c.encode(value)

def decode[A : Codec as c](value: String): A =
def decode[A: Codec as c](value: String): A =
c.decode(value)

object Codec:
Expand All @@ -37,5 +37,5 @@ object Codec:
stringCodec.imap(_.toDouble, _.toString)

// Implement a Codec for the Box type.
given boxCodec: [A : Codec as c] => Codec[Box[A]] =
given boxCodec: [A: Codec as c] => Codec[Box[A]] =
c.imap[Box[A]](Box(_), _.value)
4 changes: 2 additions & 2 deletions ch08/src/Display.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object Display:
// Given a Display[A], and a Box[A], we can create
// Display[Box[A]] by the reverse mapping Box[A] => A.
// The forward mapping would be A => Box[A].

// Parameterized typeclass with named context bound.
given [A : Display as d] => Display[Box[A]] =
given [A: Display as d] => Display[Box[A]] =
d.contramap[Box[A]](_.value)
8 changes: 4 additions & 4 deletions ch08/src/Tree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ object Tree:
import Tree.*

/* 8.5.4 Exercise: Branching out with Functors
Write a Functor for the following binary tree data type.
Write a Functor for the following binary tree data type.
Verify that the code works as expected on instances of Branch and Leaf.
*/
*/
given Functor[Tree]:
override def map[A, B](fa: Tree[A])(f: A => B): Tree[B] =
fa match
case Branch(l, r) => Branch(map(l)(f), map(r)(f))
case Leaf(value) => Leaf(f(value))

// The compiler can find a Functor instance for Tree but not for Branch or Leaf
// The compiler can find a Functor instance for Tree but not for Branch or Leaf
// (Functor is invariant in F). Let's add some smart constructors to compensate.
def branch[A](left: Tree[A], right: Tree[A]): Tree[A] =
Branch(left, right)

def leaf[A](value: A): Tree[A] =
Leaf(value)
Leaf(value)
6 changes: 3 additions & 3 deletions ch09/src/Lib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package ch09

import cats.{Eval, MonadError}
import cats.data.{Reader, Writer, State}
import cats.syntax.applicative.catsSyntaxApplicativeId // pure
import cats.syntax.writer.catsSyntaxWriterId // tell
import cats.syntax.apply.catsSyntaxApplyOps // *>
import cats.syntax.applicative.catsSyntaxApplicativeId // pure
import cats.syntax.writer.catsSyntaxWriterId // tell
import cats.syntax.apply.catsSyntaxApplyOps // *>

object Lib:
/*
Expand Down
2 changes: 1 addition & 1 deletion ch09/src/Monad.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ trait Monad[F[_]]:
def pure[A](a: A): Id[A] = a

def flatMap[A, B](value: Id[A])(f: A => Id[B]): Id[B] =
f(value)
f(value)
42 changes: 21 additions & 21 deletions ch09/src/Tree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,32 @@ package ch09
despite the fact that we haven’t directly implemented flatMap or map on Tree.
Don't feel you have to make tailRecM tail-recursive. Doing so is quite difficult.
*/
enum Tree[A]:
case Leaf(value: A)
case Branch(left: Tree[A], right: Tree[A])
*/
enum Tree[A]:
case Leaf(value: A)
case Branch(left: Tree[A], right: Tree[A])

object Tree:
import Tree.*
object Tree:
import Tree.*

given cats.Monad[Tree]:
override def pure[A](x: A): Tree[A] =
Leaf(x)
given cats.Monad[Tree]:
override def pure[A](x: A): Tree[A] =
Leaf(x)

override def flatMap[A, B](t: Tree[A])(f: A => Tree[B]): Tree[B] =
override def flatMap[A, B](t: Tree[A])(f: A => Tree[B]): Tree[B] =
t match
case Leaf(x) => f(x)
case Branch(l, r) => Branch(flatMap(l)(f), flatMap(r)(f))
case Leaf(x) => f(x)
case Branch(l, r) => Branch(flatMap(l)(f), flatMap(r)(f))

// Not stack-safe!
override def tailRecM[A, B](a: A)(f: A => Tree[Either[A, B]]): Tree[B] =
// Not stack-safe!
override def tailRecM[A, B](a: A)(f: A => Tree[Either[A, B]]): Tree[B] =
flatMap(f(a)):
case Left(value) => tailRecM(value)(f)
case Right(value) => Leaf(value)
case Left(value) => tailRecM(value)(f)
case Right(value) => Leaf(value)

// Smart constructors to help the compiler.
def branch[A](left: Tree[A], right: Tree[A]): Tree[A] =
Branch(left, right)
// Smart constructors to help the compiler.
def branch[A](left: Tree[A], right: Tree[A]): Tree[A] =
Branch(left, right)

def leaf[A](value: A): Tree[A] =
Leaf(value)
def leaf[A](value: A): Tree[A] =
Leaf(value)
6 changes: 3 additions & 3 deletions ch09/src/ch09.worksheet.sc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import cats.{Id, Eval, MonadError}
import cats.data.{Reader, Writer, State}
import cats.syntax.functor.toFunctorOps // map
import cats.syntax.flatMap.toFlatMapOps // flatMap
import cats.syntax.applicative.catsSyntaxApplicativeId // pure
import cats.syntax.functor.toFunctorOps // map
import cats.syntax.flatMap.toFlatMapOps // flatMap
import cats.syntax.applicative.catsSyntaxApplicativeId // pure
import cats.syntax.writer.catsSyntaxWriterId // tell

// 9.3 The Identity Monad
Expand Down

0 comments on commit 0bb7837

Please sign in to comment.