-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
// Trait of all extractors with unapply methods | ||
trait Matcher[A, B]: | ||
def unapply(x: A): Option[B] | ||
|
||
// An extractor defined by an unappy method | ||
object Even extends Matcher[Int, Int]: | ||
def unapply(x: Int): Option[Int] = | ||
if x % 2 == 0 then Some(x) else None | ||
|
||
// Method using a given extractor in pattern position | ||
def collect[A, B](xs: List[A], m: Matcher[A, B]): List[B] = | ||
xs match | ||
case Nil => Nil | ||
case m(x) :: xs1 => x :: collect(xs1, m) | ||
case _ :: xs1 => collect(xs1, m) | ||
|
||
@main def test = | ||
val xs = List(1, 2, 3, 4) | ||
val ys = collect(xs, Even) | ||
println(ys) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import language.experimental.into | ||
|
||
class BigInt(x: Int): | ||
def + (other: into BigInt): BigInt = ??? | ||
def * (other: into BigInt): BigInt = ??? | ||
|
||
object BigInt: | ||
given Conversion[Int, BigInt] = BigInt(_) | ||
|
||
extension (x: into BigInt) | ||
def + (other: BigInt): BigInt = ??? | ||
def * (other: BigInt): BigInt = ??? | ||
|
||
@main def Test = | ||
val x = BigInt(2) | ||
val y = 3 | ||
val a1 = x + y | ||
val a2 = y * x | ||
val a3 = x * x | ||
val a4 = y + y | ||
|