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
// consider this similar to | |
// class EmployeeDAO(@Autowired dataSource: EmployeeDataSource) | |
class EmployeeDAO()(implicit dataSource: EmployeeDataSource) { | |
def getEmployeeData(): List[Employee] = { | |
dataSource.fetch() | |
// data validation | |
// modification | |
// ... | |
} |
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
trait EmployeeDataSource { | |
def fetch(): List[Employee] | |
} | |
class CassandraDataSource extends EmployeeDataSource { | |
override def fetch(): List[Employee] = ??? | |
} | |
class FileDataSource extends EmployeeDataSource { | |
override def fetch(): List[Employee] = ??? |
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 MyComparator { | |
implicit val compare: MyComparator[Rectangle] = (r1, r2) => r1.breadth.compareTo(r2.breadth) | |
} |
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 Sorting8 { | |
def sort[A : MyComparator](collection: List[A]): List[A] = collection match { | |
case Nil => Nil | |
case h :: t => insert(h, sort(t)) | |
} | |
private def insert[A : MyComparator](elem: A, collection: List[A]): List[A] = { | |
collection match { | |
case Nil => List(elem) | |
case h :: t if MyComparator.compare(elem, h) < 0 => h :: collection |
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 MyComparator { | |
def compare[T](t1: T, t2:T)(implicit comparator: MyComparator[T]): Int = comparator.compare(t1, t2) | |
} |
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 Sorting7 { | |
def sort[A : MyComparator](collection: List[A]): List[A] = collection match { | |
case Nil => Nil | |
case h :: t => insert(h, sort(t)) | |
} | |
private def insert[A : MyComparator](elem: A, collection: List[A]): List[A] = { | |
val comparator = implicitly[MyComparator[A]] | |
collection match { | |
case Nil => List(elem) |
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 Sorting6 { | |
def sort[A](collection: List[A])(implicit comparator: MyComparator[A]): List[A] = collection match { | |
case Nil => Nil | |
case h :: t => insert(h, sort(t)) | |
} | |
private def insert[A](elem: A, collection: List[A])(implicit comparator: MyComparator[A]): List[A] = { | |
collection match { | |
case Nil => List(elem) | |
case h :: t if comparator.compare(elem, h) < 0 => h :: collection |
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 Sorting5 { | |
def sort[A](collection: List[A])(comparator: MyComparator[A]): List[A] = collection match { | |
case Nil => Nil | |
case h :: t => insert(h, sort(t)(comparator))(comparator) | |
} | |
private def insert[A](elem: A, collection: List[A])(comparator: MyComparator[A]): List[A] = { | |
collection match { | |
case Nil => List(elem) | |
case h :: t if comparator.compare(elem, h) < 0 => h :: collection |
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
trait MyComparator[T] { | |
def compare(t1: T, t2: T): 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
// providing the implicit value explicitly when multiple implicit definitions | |
// are in the same scope | |
case class Rectangle(length: Int, breadth: Int) | |
object Rectangle { | |
implicit val compareByBreadth: (Rectangle, Rectangle) => Int = | |
(r1, r2) => r1.breadth.compareTo(r2.breadth) | |
implicit val compareByLength: (Rectangle, Rectangle) => Int = | |
(r1, r2) => r1.length.compareTo(r2.length) |
NewerOlder