REPL:
1. In the scala REPL, type 3.
followed by the Tab key. What methods can be applied?
在Scala REPL中键入3.
然后按Tab键。有哪些方法可以被应用?
scala> 3.
!= + <= >> getClass toDouble toString
## - <init> >>> hashCode toFloat unary_+
% / == ^ isInstanceOf toInt unary_-
& < > asInstanceOf toByte toLong unary_~
* << >= equals toChar toShort |
疑惑:在Intellij里输入一个int,比如3,怎么才能找到这些方法对应的源码呢?
2. In the Scala REPL, compute the square root of 3, and then square that value. By how much does the result differ from 3? (Hint: The res
variables are your friend.)
在Scala REPL中,计算3的平方根,然后再对该值求平方。现在,这个结果与3相差多少?(提示:res变量是你的朋友)
“`scala
scala> math.pow(3, 0.5)
res1: Double = 1.7320508075688772
scala> math.pow(res1, 2)
res2: Double = 2.9999999999999996
“`
3. Are the res
variables val
or var
?
res变量是val还是var?
val
scala> res2 = 5
<console>:13: error: reassignment to val
res2 = 5
4. Scala lets you mulptiply a string with a number – try out "crazy" * 3
in the REPL. What does this operation do? Where can you find it in Scaladoc? Scala允许你用数字去乘字符串—去REPL中试一下”crazy”*3。这个操作做什么?在Scaladoc中如何找到这个操作?
scala> "crazy" * 3
res5: String = crazycrazycrazy
在StringLike特质中可以找到
5. What does 10 max 2
mean? In which class is the max
method defined? 10 max 2的含义是什么?max方法定义在哪个类中?
10.max(2), RichInt
6. Using BigInt
, compute
21024
. 用BigInt计算2的1024次方
scala> import scala.math
import scala.math
scala> math.BigInt(2)
res12: scala.math.BigInt = 2
scala> res12.pow(1024)
res13: scala.math.BigInt = 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
7. What do you need to import so that you can get a random prime as probablePrime(100, Random)
, without any qualifiers before probablePrime
and Random
? 为了在使用probablePrime(100,Random)获取随机素数时不在probablePrime和Radom之前使用任何限定符,你需要引入什么?
scala> import scala.math.BigInt._
import scala.math.BigInt._
scala> probablePrime()
<console>:14: error: not enough arguments for method probablePrime: (bitLength: Int, rnd: scala.util.Random)scala.math.BigInt.
Unspecified value parameters bitLength, rnd.
probablePrime()
^
scala> import scala.util.Random
import scala.util.Random
scala> Random
res1: scala.util.Random.type = scala.util.Random$@2ed0fbae
8. One way to create random file or directory names is to produce a random BigInt
and convert it to base 36, yielding a string such as "qsnvbevtomcj38o06kul"
. Poke around Scaladoc to find a way of doing this in scala. 创建随机文件的方式之一是生成一个随机的BigInt,然后将它转换成三十六进制,输出类似”qsnvbevtomcj38o06kul”这样的字符串。查阅Scaladoc,找到在Scala中实现该逻辑的办法。
scala> new Random()
res8: scala.util.Random = scala.util.Random@5740ff5e
scala> res8.nextInt.toString(36)
res10: String = lco8go
9. How do you get the first character of a string in Scala? The last character? 在Scala中如何获取字符串的首字符和尾字符?
scala> "abc"
res16: String = abc
scala> res16.head
res17: Char = a
scala> res16.take(1)
res18: String = a
scala> res16(0)
res19: Char = a
scala> res16.takeRight(1)
res20: String = c
scala> res16.reverse(0)
res22: Char = c
10. What do the take
, drop
, takeRight
, and dropRight
string functions do? What advantage or disadvantage do they have over using substring? take,drop,takeRight和dropRight这些字符串函数是做什么用的?和substring相比,他们的优点和缺点都是哪些?
take
就是取前几个字符,drop
就是扔掉前几个字符,takeRight
和dropRight
分别实现相反的功能。好处和坏处都是使用错误的下标不会报错吧?
scala> res23.drop(1)
res24: String = bcdefg
scala> res23.drop(25)
res25: String = ""
scala> res23.substring(1)
res26: String = bcdefg
scala> res23.substring(-1)
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1914)
... 33 elided
scala> res23.drop(-1)
res28: String = abcdefg
// take, drop内部是用slice实现的
scala> "abc".slice(-1, 5)
res29: String = abc