Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- implicit def whatwehave_to_whatwegenerate
- implicit def whatwehave_whatitcando
- implicit def whatwecandowith_whatwehave
- class FoldingPair[A,B](t2: (A,B)) {
- def fold[Z](f: (A,B) => Z) = f(t2._1, t2._2)
- }
- implicit def pair_is_foldable[A,B](t2: (A,B)) = new FoldingPair(t2)
- val v = Vector(Vector("This","is","2D" ...
- val w = v.updated(2, v(2).updated(5, "Hi")) // Messy!
- val w = change(v)(2,5)("Hi") // Okay, better for a few uses
- val w = v change (2,5) -> "Hi" // Arguably clearer, and...
- val w = v change ((2,5) -> "Hi", (2,6) -> "!")) // extends naturally to this!
- implicit def string_to_int(s: String) = s.toInt
- class Hashed[A](private[Hashed] val a: A) {
- override def equals(o: Any) = a == o
- override def toString = a.toString
- override val hashCode = a.##
- }
- object Hashed {
- implicit def anything_to_hashed[A](a: A) = new Hashed(a)
- implicit def hashed_to_anything[A](h: Hashed[A]) = h.a
- }
- val d = "20110513".toDate //YES
- val d : Date = "20110513" //NO!
- val (duration, unit) = 5.seconds //YES
- val b = someRef.isContainedIn(aColl) //NO!
- aColl exists_? aPred //NO! - just use "exists"
- trait Numeric[T] {
- def plus(x: T, y: T): T
- def minus(x: T, y: T): T
- def times(x: T, y: T): T
- //...
- }
- implicit object ShortIsNumeric extends Numeric[Short] {
- def plus(x: Short, y: Short): Short = (x + y).toShort
- def minus(x: Short, y: Short): Short = (x - y).toShort
- def times(x: Short, y: Short): Short = (x * y).toShort
- //...
- }
- //...
- trait Numeric[T] {
- // ...
- class Ops(lhs: T) {
- def +(rhs: T) = plus(lhs, rhs)
- def -(rhs: T) = minus(lhs, rhs)
- def *(rhs: T) = times(lhs, rhs)
- // ...
- }
- }
- implicit def infixNumericOps[T](x: T)(implicit num: Numeric[T]): Numeric[T]#Ops =
- new num.Ops(x)
- def addAnyTwoNumbers[T: Numeric](x: T, y: T) = x + y
- object PimpTypeClass {
- trait Numeric[T] {
- def plus(x: T, y: T): T
- def minus(x: T, y: T): T
- def times(x: T, y: T): T
- class Ops(lhs: T) {
- def +(rhs: T) = plus(lhs, rhs)
- def -(rhs: T) = minus(lhs, rhs)
- def *(rhs: T) = times(lhs, rhs)
- }
- }
- object Numeric {
- implicit object ShortIsNumeric extends Numeric[Short] {
- def plus(x: Short, y: Short): Short = (x + y).toShort
- def minus(x: Short, y: Short): Short = (x - y).toShort
- def times(x: Short, y: Short): Short = (x * y).toShort
- }
- implicit def infixNumericOps[T](x: T)(implicit num: Numeric[T]): Numeric[T]#Ops =
- new num.Ops(x)
- def addNumbers[T: Numeric](x: T, y: T) = x + y
- }
- }
- object PimpTest {
- val x: Short = 1
- val y: Short = 2
- println(x + y)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement