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
enum MyList[+T]: | |
case Cons(head: T, tail: MyList[T]) | |
case EmptyList | |
def isEmpty: Boolean = this match { | |
case Cons(_, _) => false | |
case EmptyList => true | |
} | |
def prepend[R >: T](element: R): MyList[R] = Cons(element, this) |
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
sealed trait MyList[+T] { | |
def isEmpty: Boolean | |
def prepend[R >: T](element: R): MyList[R] = Cons(element, this) | |
} | |
case class Cons[+T](head: T, tail: MyList[T]) extends MyList[T] { | |
override def isEmpty: Boolean = false | |
} | |
case object EmptyList extends MyList[Nothing] { |
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
package com.template | |
trait BinarySearchTree[+T] { | |
def insert[R >: T](element: R)(implicit ord: Ordering[R]): BinarySearchTree[R] | |
def isEmpty: Boolean | |
} | |
case class Node[+T](value: T, left: BinarySearchTree[T], right: BinarySearchTree[T]) extends BinarySearchTree[T] { | |
override def isEmpty: Boolean = false | |
override def insert[R >: T](element: R)(implicit ord: Ordering[R]): BinarySearchTree[R] = { |
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
import CompanySalaryApplication._ | |
object CompanyBBonus: | |
given companyBonus: CompanyBonus = CompanyBonus(2000) | |
object CompanyB extends App with SalaryComputation: | |
import CompanyBBonus.given | |
val julie = Employee("Julie", 8000) | |
val julia = Employee("Julia", 10000) | |
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
import CompanySalaryApplication._ | |
object CompanyABonus: | |
given CompanyBonus = CompanyBonus(1000) | |
object CompanyA extends App with SalaryComputation: | |
import CompanyABonus.given | |
val jack = Employee("Jack", 8000) | |
val jill = Employee("Jill", 10000) |
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
object CompanySalaryApplication: | |
case class Employee(name: String, salary: Int) | |
case class CompanyBonus(amount: Int) | |
trait SalaryComputation: | |
def computeSalary(employee: Employee)(using companyBonus: CompanyBonus): Int = | |
employee.salary + companyBonus.amount |
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
class StudentPromotion extends Promotion { | |
override def promotionDiscountFactor: Int = 4 | |
} |
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
open class Promotion { | |
def promotionDiscountFactor: Int = 2 | |
} |
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
import scala.util.Random | |
object TransparentTrait { | |
val Sell = "sell" | |
val Buy = "buy" | |
transparent trait Id { | |
def id: Int | |
} |
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
object TraitParameters extends App { | |
trait Order(quantity: Int) { | |
val printQuantity: String = s"This order has a quantity of $quantity" | |
} | |
class Buy(q: Int) extends Order(q) | |
class Sell(q: Int) extends Order(q) | |
object Trade { |
NewerOlder