Skip to content

Instantly share code, notes, and snippets.

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)
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] {
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] = {
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)
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)
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
class StudentPromotion extends Promotion {
override def promotionDiscountFactor: Int = 4
}
open class Promotion {
def promotionDiscountFactor: Int = 2
}
import scala.util.Random
object TransparentTrait {
val Sell = "sell"
val Buy = "buy"
transparent trait Id {
def id: Int
}
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 {