Advertisement
Guest User

Untitled

a guest
May 30th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. package types
  2. import org.scalatest.{FlatSpec, Matchers}
  3.  
  4. object Types {
  5.  
  6. case class MyList[A](list: List[A]) {
  7. //def sortBy2[B : Ordering](f: A => B): List[A] = list.sortBy(f)(implicitly[Ordering[B]])
  8. def sortBy[B: Ordering](f: A => B): List[A] = list.sortBy(f)
  9. }
  10.  
  11.  
  12. case class FrameA(id: Int, msg: String)
  13. case class FrameB(id: Int, msg: String, property_b: String)
  14. case class FrameC(id: Int, msg: String, property_c: Long)
  15.  
  16. trait FrameType {
  17. type Repr
  18. val frame: Repr
  19. val id: Int
  20. }
  21.  
  22. object Implicits {
  23.  
  24. implicit object FrameAOrdering extends Ordering[FrameA] {
  25. override def compare(x: FrameA, y: FrameA): Int = {
  26. x.id.compare(y.id) * -1
  27. }
  28. }
  29.  
  30. implicit def A2FrameType(f: FrameA): FrameType = {
  31. new FrameType {
  32. type Repr = FrameA
  33. val frame = f
  34. val id = f.id
  35. }
  36. }
  37.  
  38. implicit def B2FrameType(f: FrameB): FrameType = {
  39. new FrameType {
  40. type Repr = FrameB
  41. val frame = f
  42. val id = f.id
  43. }
  44. }
  45. }
  46.  
  47. import Implicits._
  48.  
  49. // 1. create a Vector[FrameType]()
  50. // 2. insert a FrameA/FrameB to Vector[FrameType]
  51. // 3. pop and then pattern match an element
  52.  
  53. def init(): Vector[FrameType] = {
  54. Vector[FrameType]()
  55. }
  56.  
  57. def insert[T <% FrameType](frame: T, frames: Vector[FrameType]): Vector[FrameType] = {
  58. //val x: Vector[FrameType] = frames :+ frame
  59. ???
  60. }
  61.  
  62. }
  63.  
  64. import Types._
  65. import Types.Implicits._
  66.  
  67. class TypesSpec extends FlatSpec with Matchers {
  68.  
  69. "test" should "" in {
  70. //val l1 = MyList(List(FrameA(1, "1"), FrameA(6, "6"), FrameA(2, "2")))
  71. val l1 = MyList(List(1, 6, 2))
  72. val l2 = l1.sortBy(FrameA(_, ""))
  73. println(l2)
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement