当前位置:嗨网首页>书籍在线阅读

11-标准库支持

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

2.3.4 标准库支持

1.artifacts及拆分包

现在,Kotlin的标准库可以完全兼容Java 9的模块系统,它禁止对包进行拆分(多个JAR包文件在同一个包中声明类)。为了支持这一点,需要引入新的artifacts kotlin-stdlib-jdk7和kotlin-stdlib-jdk8来取代旧的kotlin-stdlib-jre7和 kotlin-stdlib-jre8。

从Kotlin的角度来看,新artifacts中的声明在相同包名下可见,但是对Java而言它们则位于不同的包中。因此,切换到新的artifacts后,不需要对代码进行任何更改。

另外,新模块系统移除了kotlin.reflect包中的声明,如果要在新版本中使用它们,则需要使用kotlin.reflect.full包中的声明,自Kotlin 1.1版本就开始支持该包的内容。

2.chunked、windowed、zipWithNext

在新版本中,Iterable\、Sequence\和CharSequence等扩展了诸如缓冲或批处理(chunked)、滑动窗口和滑动平均值计算(windowed)以及处理subsequent item对(zipWithNext)等用例。

fun main(args: Array<String>) {
    val items = (1..9).map { it * it }
    val chunkedIntoLists = items.chunked(4)
    val points3d = items.chunked(3) { (x, y, z) -> Triple(x, y, z) }
    val windowed = items.windowed(4)
    val slidingAverage = items.windowed(4) { it.average() }
    val pairwiseDifferences = items.zipWithNext { a, b -> b - a }
    println("items: $items ")
    println("chunked into lists: $chunkedIntoLists")
    println("3D points: $points3d")
    println("windowed by 4: $windowed")
    println("sliding average by 4: $slidingAverage")
    println("pairwise differences: $pairwiseDifferences")
}

在Kotlin 1.2版本的JVM中执行上面的代码,输出结果如下。

items: [1, 4, 9, 16, 25, 36, 49, 64, 81]
chunked into lists: [[1, 4, 9, 16], [25, 36, 49, 64], [81]]
3D points: [(1, 4, 9), (16, 25, 36), (49, 64, 81)]
windowed by 4: [[1, 4, 9, 16], [4, 9, 16, 25], [9, 16, 25, 36], [16, 25, 36, 49], [25, 36, 49, 64], [36, 49, 64, 81]]
sliding average by 4: [7.5, 13.5, 21.5, 31.5, 43.5, 57.5]
pairwise differences: [3, 5, 7, 9, 11, 13, 15, 17]

3.fill、replaceAll、shuffle/shuffled

除此之外,Kotlin 1.2版本还添加了一系列扩展函数用于处理列表,这些函数包括针对MutableList的fill、replaceAll、shuffle以及针对只读List的shuffled。

fun main(args: Array<String>) {
    val items = (1..5).toMutableList()
    items.shuffle()
    println("Shuffled items: $items")
    items.replaceAll { it * 2 }
    println("Items doubled: $items")
    items.fill(5)
    println("Items filled with 5: $items")
}

在Kotlin 1.2版本的JVM中执行上面的代码,输出结果如下。

Shuffled items: [1, 5, 2, 3, 4]
Items doubled: [2, 10, 4, 6, 8]
Items filled with 5: [5, 5, 5, 5, 5]

4.kotlin-stdlib中的数学运算

为了方便开发者进行数学公式计算,Kotlin 1.2增加了一些用于数学运算的API。这些API对于JVM和JavaScript是通用的。

  • 常量:PI和E。
  • 三角函数:cos、sin、tan及其逆函数acos、asin、atan、atan2。
  • 双曲三角函数:cosh、sinh、tanh及其逆函数acosh、asinh、atanh。
  • 指数函数:pow、sqrt、hypot、exp、expm1。
  • 对数函数:log、log2、log10、ln。
  • Round函数:ceil、floor、round、roundToInt、roundToLong。
  • 符号函数和绝对值:abs和sign函数、absoluteValue和sign扩展属性。

5.BigInteger和BigDecimal的运算与转换

Kotlin 1.2引入了一组用于操作BigInteger和BigDecimal,以及使用其他数字类型进行转换的函数。这些函数主要包括以下几个。

  • 用于Int和Long类型的toBigInteger。
  • 用于Int、Long、Float、Double和BigInteger类型的toBigDecimal。
  • 二元算数运算符:+、-、*、/、%和中缀函数and、or、xor、shl和shr。
  • 一元算数运算符:-、++、- -和函数inv。

6.浮点数转换

Kotlin 1.2版本添加了一些新函数,用于将Double和Float转换成位表示形式。

  • toBits和toRawBits对于Double类型返回Long,而对于Float返回Int。
  • Double.fromBits和Float.fromBits用于将位表示形式转换为浮点数。

7.可序列化的Regex类

kotlin.text.Regex类是一个可序列化的类,可以直接在可序列化的层次结构中使用它。