Tuesday, 24 May 2016

Scala for Beginners (Part-1): slice function in Collection API and Array


In this post, we will discuss about one of the most important and frequently used function in Scala Based Applications: slice function.

This slice function is available in most of classes:
  • Collection API 
  • String
  • Array etc. 

We will discuss about "How to use slice function in Collection API and Array" in this post. We will look into "How to use slice function with String and difference between 'slice' and 'substring' functions in String" with some useful examples in my coming posts. (Please click here for "String Slice function" post soon)

The 'slice' function usage:

In Scala API, 'slice' function is used to select an interval of elements. It takes two parameters of Int type as shown below:

slice function syntax:

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

Parameter Usage
Frist Starting index (Inclusive). It should be zero or any any positive integer less than the length of the Collection or String or Array.
Second Ending index (Exclusive).

slice function extract elements starting from 'first-index' (Inclusive) to 'until-index' (exclusive). Here elements numbers for an Array of Numbers, Characters for a String, an object for a Collection.

Array slice Function Examples:-

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

Int of Array with 5 values are created so it's index start value is 0 and index end value is 4.
It's length = 5

Let us play with slice function now.

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

It starts with 0 index that is first element and retrieves all elements until 2 means index = 1 that's why we got 0th element and 1st element here.

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

We can access with any indices range.

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

If we give same values for start and end like above we will get empty array why?

Start index = 3
End index = 3 - 1 = 2 

It's not possible to retrieve a set of elements from an array from 3 to 2 indices right.

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

If we give -ve values, it just starts with available index as shown above.

scala> marksArray.slice(0,5)

res4: Array[Int] = Array(56, 79, 60, 99, 71)

If we give 2nd parameter value in beyond it's available index as shown above (Available max index value in marksArray is 4 only as it's length = 5), it just ignores that value and returns upto available index only.

NOTE:- Unlike Java, it does not throw any ArrayIndexOutOfBoundsException. 

Collection API slice Function Examples:-

Same as Array examples, we will get same results for any Collection API.

scala> val list = List(56, 79, 60, 99, 71)
list: List[Int] = List(56, 79, 60, 99, 71)

scala> list.slice(0,2)
res5: List[Int] = List(56, 79)

scala> list.slice(3,4)
res6: List[Int] = List(99)

scala> list.slice(3,3)
res7: List[Int] = List()

scala> list.slice(-1,3)
res8: List[Int] = List(56, 79, 60)

scala> list.slice(0,5)
res9: List[Int] = List(56, 79, 60, 99, 71)

If we access an empty list, we will get empty list only as shown below

scala> val list2 = List()
list2: List[Nothing] = List()

scala> list2.slice(0,1)

res10: List[Nothing] = List()

Cool stuff yaar!.

We will discuss String class slice and substring functions usage in my next post.

Thank you for reading my posts. 
Please drop me your valuable suggestions or comments.