Scala 切片函数

今天我们来看看 Scala 切片功能。

缩小尺寸

Scala slice 函数在以下 API 类中可用:

我们将采集每个API,并在下一节中深入讨论它们,并提供适当的示例。

使用 Scala Slice 函数

In Scala API, 'slice' function is used to select an interval of elements. It takes two parameters of "Int" type and returns subset or whole or none element(s) of original Collection (or String or Array). Real-world slice scenario:- We can use this slice function in our regular real-world life too as shown below. Scala slice function Here Bread.slice(0,10) means take bread slices from 0 to 9 so total 10 Bread slices.(Just to resemble Scala's slice function here I'm using zero, but assume this as one.) slice function syntax: In Scala Standard Library (API), this slice function is defined as follows:

1def slice(from-index: Int, until-index: Int): ScalaAPIClass[A]

在这里,ScalaAPIClass是指任何Scala Collection类(支持基于索引的访问,如Seq、List等)、String、Array类。 **示例:-**下列函数在Scala API的Seq类中定义。

1def slice(from-index: Int, until-index: Int): Seq[A]

** slice 函数参数:** slice 函数参数的使用在下表中描述:

S.No.Function ParamsUsage
1.First ParameterStarting index (Inclusive). It should be zero or any any positive integer less than the length of the Collection or String or Array.
2.Second ParameterEnding index (Exclusive).

slice 函数提取元素从first-index (包括) 到until-index (专属)。

Scala Array 片段

在 Scala API 中,Array 类将切片函数定义如下:

1def slice(from: Int, until: Int): Array[T]

在这里,‘from’是数组的起始索引(包括),‘until’是数组的终结索引(专属)。

1scala> val marksArray = Array(56,79,60,99,71)
2marksArray: Array[Int] = Array(56, 79, 60, 99, 71)

Int of Array 有 5 个值是创建的,所以它的索引开始值为 0 和索引终端值为 4。

1scala> marksArray.slice(0,2)
2res0: Array[Int] = Array(56, 79)

它以第一个元素的0索引开始,并检索所有元素,直到2意味着索引 = 1,这就是为什么我们在这里得到0th元素和1st元素的原因。

1scala> marksArray.slice(3,4)
2res1: Array[Int] = Array(99)

我们可以访问任何指数范围。

1scala> marksArray.slice(3,3)
2res2: Array[Int] = Array()

如果我们给出相同的开始和结束值,如上所述,我们会得到空的数组为什么? 开始指数 = 3 终结指数 = 3 - 1 = 2 无法从 3 到 2 指数的数组中获取一组元素。

1scala> marksArray.slice(-1,3)
2res3: Array[Int] = Array(56, 79, 60)

如果我们给 -ve 值,它只会从上面的指数开始。

1scala> marksArray.slice(0,5)
2
3res4: Array[Int] = Array(56, 79, 60, 99, 71)

如果我们给出的第 2 个参数值超过它的可用索引如上所示(Available max index value in marksArray 仅为 4 因为它的长度是 = 5),它只忽略了该值,只返回可用索引。

Scala 收藏品

在 Scala 的标准 API 中,大多数类都定义了支持基于索引元素访问的此切片函数,例如,列表类定义了如下所示的此函数:

1def slice(from: Int, until: Int): List[A]

列表切片函数示例:- 与 Array 示例一样,我们将为任何集合 API 获得相同的结果。

 1scala> val list = List(56, 79, 60, 99, 71)
 2list: List[Int] = List(56, 79, 60, 99, 71)
 3
 4scala> list.slice(0,2)
 5res5: List[Int] = List(56, 79)
 6
 7scala> list.slice(3,4)
 8res6: List[Int] = List(99)
 9
10scala> list.slice(3,3)
11res7: List[Int] = List()
12
13scala> list.slice(-1,3)
14res8: List[Int] = List(56, 79, 60)
15
16scala> list.slice(0,5)
17res9: List[Int] = List(56, 79, 60, 99, 71)

如果我们访问一个空名单,我们只会收到空名单,如下所示

1scala> val list2 = List()
2list2: List[Nothing] = List()
3
4scala> list2.slice(0,1)
5
6res10: List[Nothing] = List()

斯卡拉 String 片段

在 Scala API 中,在 scala.collection.immutable 包中定义了StringOps类,它定义了如下所示的片段函数:

1def slice(from: Int, until: Int): String

** 注意:-** 在 Scala 中,我们使用 Java 的 String 类,但这个类没有切片函数. 当我们在 Java 的 String 对象上使用切片函数时,Scala Compiler 会内部将这个 String 对象转换为 StringOps 类对象,以便使用此切片函数。

1scala> val str = "Hello I'm doing good. How are you?"
2str: String = Hello I'm doing good. How are you?
3
4scala> str.slice(6,9)
5
6res8: String = I'm

这里 from-index = 6 意味着 until-index = 9 (它是专属的,所以我们只需要考虑 till index = 8) String 的子串函数与其切片函数相同,如下所示:

1scala> str.substring(6,9)
2res12: String = I'm

在这里, str.slice(6,9) 和 str.substring(6,9) 都返回相同的值。 slice Vs substring String 类的 slice 和 substring 函数之间的差异

  • 功能智慧和语法智慧没有区别
  • 性能几乎相似且不可忽视

** 注意:-** 在 Scala 中,我们可以访问 String 字符,就像下面所示的 Array 元素一样:

1scala> str(0)
2res0: Char = H

这里返回一个 Char,但不是一个 String

 1scala> str(-1)
 2java.lang.StringIndexOutOfBoundsException: String index out of range: -1
 3  at java.lang.String.charAt(String.java:658)
 4  at scala.collection.immutable.StringOps$.apply$extension(StringOps.scala:38)
 5  ... 33 elided
 6
 7scala> str.length
 8res2: Int = 34
 9
10scala> str(34)
11java.lang.StringIndexOutOfBoundsException: String index out of range: 34
12  at java.lang.String.charAt(String.java:658)
13  at scala.collection.immutable.StringOps$.apply$extension(StringOps.scala:38)
14
15  ... 33 elided

** 注意:-** 如果我们试图访问范围外的 String 字符,我们将获得 StringIndexOutOfBoundsException,如上所示。

1scala> str(0)
2res4: Char = H
3
4scala> str.substring(0,1)
5res5: String = H
6
7scala> str.slice(0,1)
8res6: String = H

这就是Scala 的切片函数的使用。我们将在我即将到来的帖子中讨论一些更多 Scala 概念。

Published At
Categories with 技术
Tagged with
comments powered by Disqus