Slide 53
Slide 53 text
scala> :paste
// Entering paste mode (ctrl-D to finish)
import mw.domain._
import mw.domain.Person._
import mw.ops._
import cats.Order
import cats.Order._
import cats.implicits._
// Exiting paste mode, now interpreting.
…
scala> val personLs = List(Person(23, "dan"), Person(35, "bob"), Person(21, "charlie"), Person(23, "alice"))
personLs: List[mw.domain.Person] = List(Person(23,dan), Person(35,bob), Person(21,charlie), Person(23,alice))
scala> sort(personLs)
res0: List[mw.domain.Person] = List(Person(21,charlie), Person(23,alice), Person(23,dan), Person(35,bob))
scala> sort(personLs)(by(_.name))
res1: List[mw.domain.Person] = List(Person(23,alice), Person(35,bob), Person(21,charlie), Person(23,dan))
scala> sort(personLs)(by(_.age))
res2: List[mw.domain.Person] = List(Person(21,charlie), Person(23,dan), Person(23,alice), Person(35,bob))
scala> sort(personLs)(reverse(by(_.age)))
res3: List[mw.domain.Person] = List(Person(35,bob), Person(23,dan), Person(23,alice), Person(21,charlie))
scala> sort(personLs)(by((p:Person) => (p.age, p.name)))
res4: List[mw.domain.Person] = List(Person(21,charlie), Person(23,alice), Person(23,dan), Person(35,bob)) scala>
sort(personLs)(by((p:Person) => (p.age,p.name)))res8: List[mw.domain.Person] = List(Person(21,charlie), Person(23,alice), Person(23,dan), Person(35,bob)
scala> sort(personLs)(whenEqual(by(_.age), by(_.name)))
res5: List[mw.domain.Person] = List(Person(21,charlie), Person(23,alice), Person(23,dan), Person(35,bob))
scala> val intLs = List(-5, 8, 10, 2, 5)
intLs: List[Int] = List(-5, 8, 10, 2, 5)
scala> sort(intLs)
res6: List[Int] = List(-5, 2, 5, 8, 10)
scala> sort(intLs)(reverse(implicitly[Order[Int]]))
res7: List[Int] = List(10, 8, 5, 2, -5)
@philip_schwarz
Let’s have a quick go
at using version 3