Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. implicit def whatwehave_to_whatwegenerate
  2. implicit def whatwehave_whatitcando
  3. implicit def whatwecandowith_whatwehave
  4.  
  5. class FoldingPair[A,B](t2: (A,B)) {
  6. def fold[Z](f: (A,B) => Z) = f(t2._1, t2._2)
  7. }
  8. implicit def pair_is_foldable[A,B](t2: (A,B)) = new FoldingPair(t2)
  9.  
  10. val v = Vector(Vector("This","is","2D" ...
  11. val w = v.updated(2, v(2).updated(5, "Hi")) // Messy!
  12. val w = change(v)(2,5)("Hi") // Okay, better for a few uses
  13. val w = v change (2,5) -> "Hi" // Arguably clearer, and...
  14. val w = v change ((2,5) -> "Hi", (2,6) -> "!")) // extends naturally to this!
  15.  
  16. implicit def string_to_int(s: String) = s.toInt
  17.  
  18. class Hashed[A](private[Hashed] val a: A) {
  19. override def equals(o: Any) = a == o
  20. override def toString = a.toString
  21. override val hashCode = a.##
  22. }
  23. object Hashed {
  24. implicit def anything_to_hashed[A](a: A) = new Hashed(a)
  25. implicit def hashed_to_anything[A](h: Hashed[A]) = h.a
  26. }
  27.  
  28. val d = "20110513".toDate //YES
  29. val d : Date = "20110513" //NO!
  30.  
  31. val (duration, unit) = 5.seconds //YES
  32. val b = someRef.isContainedIn(aColl) //NO!
  33. aColl exists_? aPred //NO! - just use "exists"
  34.  
  35. trait Numeric[T] {
  36. def plus(x: T, y: T): T
  37. def minus(x: T, y: T): T
  38. def times(x: T, y: T): T
  39. //...
  40. }
  41.  
  42. implicit object ShortIsNumeric extends Numeric[Short] {
  43. def plus(x: Short, y: Short): Short = (x + y).toShort
  44. def minus(x: Short, y: Short): Short = (x - y).toShort
  45. def times(x: Short, y: Short): Short = (x * y).toShort
  46. //...
  47. }
  48.  
  49. //...
  50.  
  51. trait Numeric[T] {
  52. // ...
  53.  
  54. class Ops(lhs: T) {
  55. def +(rhs: T) = plus(lhs, rhs)
  56. def -(rhs: T) = minus(lhs, rhs)
  57. def *(rhs: T) = times(lhs, rhs)
  58. // ...
  59. }
  60. }
  61.  
  62. implicit def infixNumericOps[T](x: T)(implicit num: Numeric[T]): Numeric[T]#Ops =
  63. new num.Ops(x)
  64.  
  65. def addAnyTwoNumbers[T: Numeric](x: T, y: T) = x + y
  66.  
  67. object PimpTypeClass {
  68. trait Numeric[T] {
  69. def plus(x: T, y: T): T
  70. def minus(x: T, y: T): T
  71. def times(x: T, y: T): T
  72. class Ops(lhs: T) {
  73. def +(rhs: T) = plus(lhs, rhs)
  74. def -(rhs: T) = minus(lhs, rhs)
  75. def *(rhs: T) = times(lhs, rhs)
  76. }
  77. }
  78. object Numeric {
  79. implicit object ShortIsNumeric extends Numeric[Short] {
  80. def plus(x: Short, y: Short): Short = (x + y).toShort
  81. def minus(x: Short, y: Short): Short = (x - y).toShort
  82. def times(x: Short, y: Short): Short = (x * y).toShort
  83. }
  84. implicit def infixNumericOps[T](x: T)(implicit num: Numeric[T]): Numeric[T]#Ops =
  85. new num.Ops(x)
  86. def addNumbers[T: Numeric](x: T, y: T) = x + y
  87. }
  88. }
  89.  
  90. object PimpTest {
  91. val x: Short = 1
  92. val y: Short = 2
  93. println(x + y)
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement