|
| 1 | +package lectures.implicits |
| 2 | + |
| 3 | +object OrganizingImplicits extends App { |
| 4 | + |
| 5 | + implicit def reverseOrdering: Ordering[Int] = Ordering.fromLessThan(_ > _) |
| 6 | + // implicit def reverseOrdering(): Ordering[Int] = Ordering.fromLessThan(_ > _) // Not valid, since it is not an accesor method |
| 7 | + |
| 8 | + println(List(1,4,5,3,2).sorted) |
| 9 | + /* |
| 10 | + Implicits (used as implicit parameters): |
| 11 | + - val/var |
| 12 | + - object |
| 13 | + - accesor methods = defs with no parentheses |
| 14 | + */ |
| 15 | + |
| 16 | + case class Person(name: String, age: Int) |
| 17 | + |
| 18 | + val persons = List( |
| 19 | + Person("Stuart", 30), |
| 20 | + Person("Anna", 22), |
| 21 | + Person("Jesus", 66) |
| 22 | + ) |
| 23 | + |
| 24 | +// implicit def alphabeticOrdering: Ordering[Person] = Ordering.fromLessThan( |
| 25 | +// (a, b) => a.name.compareTo(b.name) < 0) |
| 26 | + |
| 27 | + /* |
| 28 | + Implicit scope, from highest to lowest priority |
| 29 | + - normal scope = local scope |
| 30 | + - imported scope |
| 31 | + - companion object of all types involved in the method signature |
| 32 | + - List |
| 33 | + - Ordering |
| 34 | + - all the types involved = A or any supertype |
| 35 | + */ |
| 36 | + |
| 37 | + object AlphabeticNameOrdering { |
| 38 | + implicit def alphabeticOrdering: Ordering[Person] = Ordering.fromLessThan( |
| 39 | + (a, b) => a.name.compareTo(b.name) < 0) |
| 40 | + } |
| 41 | + |
| 42 | + object ageOrdering { |
| 43 | + implicit def ageOrdering: Ordering[Person] = Ordering.fromLessThan((a, b) => a.age < b.age) |
| 44 | + } |
| 45 | + |
| 46 | +// import ageOrdering.* |
| 47 | + import AlphabeticNameOrdering.* |
| 48 | + print(persons.sorted) |
| 49 | + |
| 50 | + /* |
| 51 | + Exercise |
| 52 | + */ |
| 53 | + case class Purchase(numOfUnits: Int, unitPrice: Double) |
| 54 | + |
| 55 | + object Purchase { // Good practice: define the most used ordering in the companion object |
| 56 | + implicit val totalPriceOrdering: Ordering[Purchase] = Ordering.fromLessThan( |
| 57 | + (a, b) => (a.numOfUnits * a.unitPrice) < (b.numOfUnits * b.unitPrice)) |
| 58 | + } |
| 59 | + |
| 60 | + object UnitPriceOrdering { |
| 61 | + implicit val unitPriceOrdering: Ordering[Purchase] = Ordering.fromLessThan(_.unitPrice < _.unitPrice) |
| 62 | + } |
| 63 | + |
| 64 | + object UnitCountOrdering { |
| 65 | + implicit val unitCountOrdering: Ordering[Purchase] = Ordering.fromLessThan(_.numOfUnits < _.numOfUnits) |
| 66 | + } |
| 67 | + |
| 68 | + val purchase1 = Purchase(10, 2000) |
| 69 | + val purchase2 = Purchase(8, 4000) |
| 70 | + val purchase3 = Purchase(2, 1000) |
| 71 | + val purchases = List(purchase1, purchase2, purchase3) |
| 72 | + |
| 73 | + import UnitPriceOrdering.* |
| 74 | +// import UnitCountOrdering.* |
| 75 | + println() |
| 76 | + println(purchases.sorted) |
| 77 | + println(purchases.sorted(unitPriceOrdering)) |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
0 commit comments