Skip to content

Commit 75330d5

Browse files
committed
Scala 3 Braceless Syntax cont.
1 parent 9f32cc6 commit 75330d5

File tree

13 files changed

+70
-97
lines changed

13 files changed

+70
-97
lines changed

src/pages/applicatives/parallel.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,11 @@ by defining a `FunctionK` that converts an `Option` to a `List`.
115115
```scala mdoc:silent
116116
import cats.arrow.FunctionK
117117

118-
object optionToList extends FunctionK[Option, List] {
118+
object optionToList extends FunctionK[Option, List]:
119119
def apply[A](fa: Option[A]): List[A] =
120-
fa match {
120+
fa match
121121
case None => List.empty[A]
122122
case Some(a) => List(a)
123-
}
124-
}
125123
```
126124
```scala mdoc
127125
optionToList(Some(1))

src/pages/case-studies/parser/applicative.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ Define an extension method `flatten`, and an instance of `Flattener` for a neste
1919

2020
<div class="solution">
2121
~~~ scala
22-
extension [A, B, C](in: ((A, B), C)) {
22+
extension [A, B, C](in: ((A, B), C))
2323
def flatten: (A, B, C) =
24-
in match {
24+
in match
2525
case ((a, b), c) => (a, b, c)
26-
}
27-
}
2826
~~~
2927

3028
It's fairly straightforward to define this class, but we can't abstract over it in any way. If we want to define `flatten` for a tuple like `(a, (b, c))` we need to define a new extension method. Similarly if we want to flatten a tuple with four elements.

src/pages/case-studies/parser/intro.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ case class Parser(parse: String => ParseResult) {
5151
@tailrec
5252
def loop(result: String, remainder: String): ParseResult = {
5353
val result1 = this.parse(remainder)
54-
result1 match {
54+
result1 match
5555
case _ if result1.failed =>
5656
ParseResult(result, remainder)
5757
case ParseResult(result1, remainder1) =>
5858
loop(result + result1, remainder1)
59-
}
6059
}
6160
loop("", input)
6261
}
@@ -115,18 +114,18 @@ Checkout the `parser-initial-a` tag to see the complete code with `~` implemente
115114
def ~(next: Parser): Parser =
116115
Parser { input =>
117116
val result = this.parse(input)
118-
result match {
117+
result match
119118
case _ if result.failed =>
120119
result
121120
case ParseResult(parsed, remainder) =>
122121
val result1 = next.parse(remainder)
123-
result1 match {
122+
result1 match
124123
case _ if result1.failed =>
125124
ParseResult("", input)
126125
case ParseResult(parsed1, remainder1) =>
127126
ParseResult(parsed + parsed1, remainder1)
128-
}
129-
}
127+
end match
128+
end match
130129
}
131130
~~~
132131
</div>

src/pages/case-studies/validation/check.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,14 @@ import cats.syntax.semigroup.* // for |+|
240240
```
241241

242242
```scala mdoc:silent
243-
sealed trait Check[E, A] {
243+
sealed trait Check[E, A]:
244244
import Check.*
245245

246246
def and(that: Check[E, A]): Check[E, A] =
247247
And(this, that)
248248

249249
def apply(a: A)(using s: Semigroup[E]): Either[E, A] =
250-
this match {
250+
this match
251251
case Pure(func) =>
252252
func(a)
253253

@@ -258,9 +258,9 @@ sealed trait Check[E, A] {
258258
case (Right(_), Left(e)) => e.asLeft
259259
case (Right(_), Right(_)) => a.asRight
260260
}
261-
}
262-
}
263-
object Check {
261+
end match
262+
263+
object Check:
264264
final case class And[E, A](
265265
left: Check[E, A],
266266
right: Check[E, A]) extends Check[E, A]
@@ -270,7 +270,6 @@ object Check {
270270

271271
def pure[E, A](f: A => Either[E, A]): Check[E, A] =
272272
Pure(f)
273-
}
274273
```
275274

276275
Let's see an example:
@@ -336,29 +335,27 @@ import cats.syntax.apply.* // for mapN
336335
```
337336

338337
```scala mdoc:silent
339-
sealed trait Check[E, A] {
338+
sealed trait Check[E, A]:
340339
import Check.*
341340

342341
def and(that: Check[E, A]): Check[E, A] =
343342
And(this, that)
344343

345344
def apply(a: A)(using s: Semigroup[E]): Validated[E, A] =
346-
this match {
345+
this match
347346
case Pure(func) =>
348347
func(a)
349348

350349
case And(left, right) =>
351350
(left(a), right(a)).mapN((_, _) => a)
352-
}
353-
}
354-
object Check {
351+
352+
object Check:
355353
final case class And[E, A](
356354
left: Check[E, A],
357355
right: Check[E, A]) extends Check[E, A]
358356

359357
final case class Pure[E, A](
360358
func: A => Validated[E, A]) extends Check[E, A]
361-
}
362359
```
363360
</div>
364361

@@ -381,7 +378,7 @@ import cats.data.Validated.* // for Valid and Invalid
381378
```
382379

383380
```scala mdoc:silent
384-
sealed trait Check[E, A] {
381+
sealed trait Check[E, A]:
385382
import Check.*
386383

387384
def and(that: Check[E, A]): Check[E, A] =
@@ -391,7 +388,7 @@ sealed trait Check[E, A] {
391388
Or(this, that)
392389

393390
def apply(a: A)(using s: Semigroup[E]): Validated[E, A] =
394-
this match {
391+
this match
395392
case Pure(func) =>
396393
func(a)
397394

@@ -407,9 +404,9 @@ sealed trait Check[E, A] {
407404
case Invalid(e2) => Invalid(e1 |+| e2)
408405
}
409406
}
410-
}
411-
}
412-
object Check {
407+
end match
408+
409+
object Check:
413410
final case class And[E, A](
414411
left: Check[E, A],
415412
right: Check[E, A]) extends Check[E, A]
@@ -420,7 +417,6 @@ object Check {
420417

421418
final case class Pure[E, A](
422419
func: A => Validated[E, A]) extends Check[E, A]
423-
}
424420
```
425421
</div>
426422

src/pages/case-studies/validation/kleisli.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ import cats.syntax.semigroup.* // for |+|
177177
import cats.data.Validated
178178
import cats.data.Validated.{Valid, Invalid}
179179

180-
sealed trait Predicate[E, A] {
180+
sealed trait Predicate[E, A]:
181181
import Predicate.*
182182

183183
def and(that: Predicate[E, A]): Predicate[E, A] =
@@ -190,7 +190,7 @@ sealed trait Predicate[E, A] {
190190
(a: A) => this(a).toEither
191191

192192
def apply(a: A)(using s: Semigroup[E]): Validated[E, A] =
193-
this match {
193+
this match
194194
case Pure(func) =>
195195
func(a)
196196

@@ -206,10 +206,10 @@ sealed trait Predicate[E, A] {
206206
case Invalid(e2) => Invalid(e1 |+| e2)
207207
}
208208
}
209-
}
210-
}
209+
end match
210+
end Predicate
211211

212-
object Predicate {
212+
object Predicate:
213213
final case class And[E, A](
214214
left: Predicate[E, A],
215215
right: Predicate[E, A]) extends Predicate[E, A]
@@ -226,7 +226,7 @@ object Predicate {
226226

227227
def lift[E, A](error: E, func: A => Boolean): Predicate[E, A] =
228228
Pure(a => if(func(a)) Valid(a) else Invalid(error))
229-
}
229+
end Predicate
230230
```
231231

232232
Working around limitations of type inference

0 commit comments

Comments
 (0)