Slice notation in Scala?

后端 未结 4 2076
轮回少年
轮回少年 2020-12-14 15:38

Is there something similar to the slice notation in Python in Scala?

I think this is really a useful operation that should be incorporated in all languages.

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-14 16:14

    See the ScalaAPI here

    So not the same notational convenience, but the operation is there

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

    Selects an interval of elements.

    Selects an interval of elements.

    Note: c.slice(from, to) is equivalent to (but possibly more efficient than) c.drop(from).take(to - from)

    from the index of the first returned element in this sequence. until the index one past the last returned element in this sequence.

    returns

    a sequence containing the elements starting at index from and extending up to (but not including) index until of this sequence.

    definition classes: IterableLike → TraversableLike

提交回复
热议问题