请选择 进入手机版 | 继续访问电脑版
楼主: Scalachen
3660 24

Learning Scala [推广有奖]

Lisrelchen 发表于 2016-4-4 01:10:33 |显示全部楼层 |坛友微信交流群
  1. 4.6) Write a function that calculates the difference between a pair of 2d points (x and y) and returns the result as a point. Hint: this would be a good use for tuples ([tuples_section]).

  2. Answer

  3. A 2-sized tuple is a good parameter type for specifying a point in 2d space. It’s components are unnamed, but we can imagine the first component as corresponding to x (the horizontal value) and its second as corresponding to y (the vertical value).

  4. scala> def offset(src: (Int, Int), dest: (Int, Int)): (Int, Int) = {
  5.      |   (dest._1 - src._1, dest._2 - src._2)
  6.      | }
  7. offset: (src: (Int, Int), dest: (Int, Int))(Int, Int)

  8. scala> offset( (4, 9), (122, 27) )
  9. res0: (Int, Int) = (118,18)
复制代码

使用道具

Lisrelchen 发表于 2016-4-4 01:11:44 |显示全部楼层 |坛友微信交流群
  1. 7) Write a function that takes a 2-sized tuple and returns it with the Int value (if included) in the first position. Hint: this would be a good use for type parameters and the isInstanceOf type operation.

  2. Answer

  3. This question should have stirred you to consider how you may change the type signature of a given tuple, and how changing the ordering of types in a tuple affects the return type of a function. Ultimately the resulting function must be non-type-safe, as the caller cannot expect to know in which order the types and values of a given tuple will be returned. Hopefully this solution is useful for learning the language, as it isn’t really a recommended solution for actual coding (due to the lack of type safety).

  4. scala> def intFirst[A,B](t: (A,B)): (Any,Any) = {
  5.      |   def isInt(x: Any) = x.isInstanceOf[Int]
  6.      |   (isInt(t._1), isInt(t._2)) match {
  7.      |     case (false, true) => (t._2, t._1)
  8.      |     case _ => t
  9.      |   }
  10.      | }
  11. intFirst: [A, B](t: (A, B))(Any, Any)

  12. scala> intFirst( ('a', 2) )
  13. res0: (Any, Any) = (2,a)

  14. scala> intFirst( (1, false) )
  15. res1: (Any, Any) = (1,false)

  16. scala> intFirst( (1, 4) )
  17. res2: (Any, Any) = (1,4)
复制代码

使用道具

Lisrelchen 发表于 2016-4-4 01:12:29 |显示全部楼层 |坛友微信交流群
  1. 4.8) Write a function that takes a 3-sized tuple and returns a 6-sized tuple, with each original parameter followed by its String representation. For example, invoking the function with (true, 22.25, "yes") should return (true, "true", 22.5, "22.5", "yes", "yes"). Can you ensure that tuples of all possible types are compatible with your function? When you invoke this function, can you do so with explicit types not only in the function result but in the value that you use to store the result?

  2. Answer

  3. The phrase "tuples of all possible types" indicates that we’ll need to have type parameters for the input tuple. We can reuse them for the return tuple’s tupe, since the return tuple should keep the same ordering of input types.

  4. scala> def stringify[A,B,C](t: (A,B,C)): (A,String,B,String,C,String) = {
  5.      |   (t._1, t._1.toString, t._2, t._2.toString, t._3, t._3.toString)
  6.      | }
  7. stringify: [A, B, C](t: (A, B, C))(A, String, B, String, C, String)
  8. Let’s invoke it and store the result in a value of an explicit type. The return type is based on interpolating the input tuple’s types with String types so this should be easy to figure out.

  9. scala> val t: (Int,String,Char,String,Boolean,String) = stringify( (1, 'c', true) )
  10. t: (Int, String, Char, String, Boolean, String) = (1,1,c,c,true,true)
复制代码

使用道具

Lisrelchen 发表于 2016-4-4 02:28:44 |显示全部楼层 |坛友微信交流群
  1. 5.1) Write a function literal that takes two integers and returns the higher number. Then write a higher-order function that takes a 3-sized tuple of integers plus this function literal, and uses it to return the maximum value in the tuple.

  2. Answer

  3. The answer to the first part of the question is to write a lambda expression, i.e. a function literal. If you can write this answer then you’ll understand lambda expressions and function literals.

  4. scala> val max = (x: Int, y: Int) => if (x > y) x else y
  5. max: (Int, Int) => Int = <function2>

  6. scala> max(23, 32)
  7. res0: Int = 32
  8. The second part of the question should give you pause. After all, how can you use a two-input function literal to compare three numbers? Hopefully you were able to figure out something like this:

  9. scala> def pickOne(t: (Int, Int, Int), cmp: (Int, Int) => Int): Int = {
  10.      |   cmp(t._1, cmp(t._2, t._3))
  11.      | }
  12. pickOne: (t: (Int, Int, Int), cmp: (Int, Int) => Int)Int

  13. scala> pickOne( (14, 7, 9), max )
  14. res1: Int = 14
复制代码

使用道具

Lisrelchen 发表于 2016-4-4 02:33:59 |显示全部楼层 |坛友微信交流群
  1. 5.2) The library function util.Random.nextInt returns a random integer. Use it to invoke the "max" function with two random integers plus a function that returns the larger of two given integers. Do the same with a function that returns the smaller of two given integers, and then a function that returns the second integer every time.

  2. Answer

  3. This should say "with three random integers", as the answer to the previous question was a function with a three-sized tuple and a comparison function. The max function is the function literal that takes two input numbers.

  4. First, here’s the "max()" function literal invoked with two random numbers.

  5. scala> max(util.Random.nextInt, util.Random.nextInt)
  6. res0: Int = 1436693791
  7. To keep our code brief and avoid retyping "util.Random.nextInt", let’s add a wrapper function.

  8. scala> def nextInt = util.Random.nextInt
  9. nextInt: Int

  10. scala> max(nextInt, nextInt)
  11. res1: Int = 1955118075
  12. Okay, here’s the "pickOne()" function invoked with three random numbers and the "max()" comparison function literal.

  13. scala> val t = (nextInt, nextInt, nextInt)
  14. t: (Int, Int, Int) = (-233084145,1722379081,683222211)

  15. scala> pickOne(t, max)
  16. res2: Int = 1722379081
复制代码

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-29 17:26