快学scala 第21章练习题 隐式转换

1. ->的工作原理是什么?或者说,”Hello” -> 42和42 -> “Hello”怎么会和对偶(“Hello”, 42)和(42, “Hello”)扯上关系呢?提示:Predef.any2ArrowAssoc

implicit final class ArrowAssoc[A](private val self: A) extends AnyVal {
    @inline def -> [B](y: B): Tuple2[A, B] = Tuple2(self, y)
    def →[B](y: B): Tuple2[A, B] = ->(y)
  }

Predef内定义隐式类ArrowAssoc[A] 泛型A相当于任意任意类型 在目标类型 包装增加 -> 方法 返回Tuple2


2. 定义一个操作符+%,将一个给定的百分比添加到某个值。举例来说,120 +% 10应得到132。提示:由于操作符的方法,而不是函数,你需要提供一个implicit。


object Test {
  def main(args: Array[String]): Unit = {
    val i = 120 +% 10
    println(i)
  }

  implicit def int2MyRichInt(n: Int): MyRichInt = {
    new MyRichInt(n)
  }
}
class MyRichInt(val self: Int) {
  def +%(n: Int): Int = {
    self + (self / n)
  }
}

3. 定义一个!操作符,计算某个整数的阶乘。举例来说,5!应得到120。你将会需要一个经过丰富的类和一个隐式转换

object Test {
  def main(args: Array[String]): Unit = {
    println(5 !)
  }

  implicit def int2MyRichInt(n: Int): MyRichInt = {
    new MyRichInt(n)
  }
}
class MyRichInt(val self: Int) {
  def ! : Int = {
    1 to self product
  }
}


5. 提供执行21.6节中的下述运算所需要的代码:

smaller(Fraction(1, 7), Fraction(2, 9) 

给出一个扩展自Ordered[Fraction]的RichFraction类。

object Test {
  def main(args: Array[String]): Unit = {
    implicit val richFraction: Fraction => RichFraction = (fraction: Fraction) => RichFraction(fraction)
    println(smaller(Fraction(2, 3), Fraction(3, 3)))
  }
  def smaller[T](a: T, b: T)(implicit ordered: T => Ordered[T]): T = if (a < b) a else b
}
case class RichFraction(fraction: Fraction) extends Ordered[Fraction] {
  override def compare(that: Fraction): Int = (fraction.n * fraction.d) - (that.n * that.d)
}

case class Fraction(n: Int, d: Int) {
  def *(other: Fraction) = Fraction(n * other.n, d * other.d)
}

6. 比较java.awt.Point类的对象,按词典顺序比较(即依次比较x坐标和y坐标的值)。

class PointOrder(point: Point) extends Ordered[Point] {
  override def compare(that: Point): Int = {
    if (point.x > that.x || (point.x == that.x && point.y > that.y)) 1
    else if (point.x == that.x && point.y == that.y) 0
    else -1
  }
}

7. 继续前一个练习,根据两个点到原点的距离进行比较。你如何在两种排序之间切换?

object Test {
  def main(args: Array[String]): Unit = {
    import PointOrder.pointOrder7

    println(new Point(2, 3) < new Point(3, 2))
  }

  object PointOrder {
    implicit val pointOrder: Point => PointOrder = (point: Point) => new PointOrder(point)
    implicit val pointOrder7: Point => PointOrder7 = (point: Point) => new PointOrder7(point)
  }
} 
class PointOrder7(point: Point) extends Ordered[java.awt.Point] {
  def compare(that: java.awt.Point): Int =
    if (point.x * point.x + point.y * point.y > that.x * that.x + that.y * that.y) 1
    else if (point.x * point.x + point.y * point.y < that.x * that.x + that.y * that.y) -1
    else 0
}

8. 在REPL中使用implicitly命令来召唤出21.5节及21.6节中的隐式对象。你得到了哪些对象?

implicitly(FrenchPunctuation.quoteDelimiters)
res14: Delimiters = Delimiters(<<,>>)

implicitly(Ordered)
res25: math.Ordered.type = scala.math.Ordered$@40712d3

9. 在Predef.scala中查找=:=对象。解释它的工作原理。

例如 
   def getFirstChar[T](arg: T)(implicit ev: T =:= String) = ev(arg).head
   事先编译器并不知道参数arg是什么类型的, 但由ev参数将其隐式地转化成了String, 就可以调用head方法了
   
  def getFirstChar[T](arg: T)(implicit ev: T =:= String) = ev(arg).head

10. 表达式"abc".map(_.toUpper)的结果是一个String,但"abc".map(_.toInt)的结果是一个Vector。搞清楚为什么会这样。

Predef有两个隐式CanBuildFrom
implicit val StringCanBuildFrom: CanBuildFrom[String, Char, String] = new CanBuildFrom[String, Char, String] {
    def apply(from: String) = apply()
    def apply()             = mutable.StringBuilder.newBuilder
  }
implicit def fallbackStringCanBuildFrom[T]: CanBuildFrom[String, T, immutable.IndexedSeq[T]] =
    new CanBuildFrom[String, T, immutable.IndexedSeq[T]] {
      def apply(from: String) = immutable.IndexedSeq.newBuilder[T]
      def apply() = immutable.IndexedSeq.newBuilder[T]
    }

map方法 
def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
    def builder = { // extracted to keep method size under 35 bytes, so that it can be JIT-inlined
      val b = bf(repr)
      b.sizeHint(this)
      b
    }
    val b = builder
    for (x <- this) b += f(x)
    b.result
  }
"abc".map(_.toUpper)
Repr是String在继承的时候写死了,B传入函数返回类型,toUpper返回Char,所以匹配
隐式StringCanBuildFrom,生成StringBuilder是一个字符串缓冲,result方法调用toString
返回String

"abc".map(_.toInt)
匹配fallbackStringCanBuildFrom生成的是Vector,每次+=都会忘集合添加元素,最终返回Vector
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值