Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package types
- import org.scalatest.{FlatSpec, Matchers}
- object Types {
- case class MyList[A](list: List[A]) {
- //def sortBy2[B : Ordering](f: A => B): List[A] = list.sortBy(f)(implicitly[Ordering[B]])
- def sortBy[B: Ordering](f: A => B): List[A] = list.sortBy(f)
- }
- case class FrameA(id: Int, msg: String)
- case class FrameB(id: Int, msg: String, property_b: String)
- case class FrameC(id: Int, msg: String, property_c: Long)
- trait FrameType {
- type Repr
- val frame: Repr
- val id: Int
- }
- object Implicits {
- implicit object FrameAOrdering extends Ordering[FrameA] {
- override def compare(x: FrameA, y: FrameA): Int = {
- x.id.compare(y.id) * -1
- }
- }
- implicit def A2FrameType(f: FrameA): FrameType = {
- new FrameType {
- type Repr = FrameA
- val frame = f
- val id = f.id
- }
- }
- implicit def B2FrameType(f: FrameB): FrameType = {
- new FrameType {
- type Repr = FrameB
- val frame = f
- val id = f.id
- }
- }
- }
- import Implicits._
- // 1. create a Vector[FrameType]()
- // 2. insert a FrameA/FrameB to Vector[FrameType]
- // 3. pop and then pattern match an element
- def init(): Vector[FrameType] = {
- Vector[FrameType]()
- }
- def insert[T <% FrameType](frame: T, frames: Vector[FrameType]): Vector[FrameType] = {
- //val x: Vector[FrameType] = frames :+ frame
- ???
- }
- }
- import Types._
- import Types.Implicits._
- class TypesSpec extends FlatSpec with Matchers {
- "test" should "" in {
- //val l1 = MyList(List(FrameA(1, "1"), FrameA(6, "6"), FrameA(2, "2")))
- val l1 = MyList(List(1, 6, 2))
- val l2 = l1.sortBy(FrameA(_, ""))
- println(l2)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement