Wednesday 27 September 2017

7)Range in Scala

Range in Scala:
Selective range of many types such as numbers or characters in sequence that can be used in looping constructs.

Types of Ranges:
  1. Number Ranges
  2. Alphabetical Ranges

1.Number Ranges:
Selective range of numbers using to,until, and by kewords.

Example:

i)Selective range using to, until keyword:


Note:

  • to keyword to include the the last upper bound
  • until keyword to exclude the last upper bound
ii)Range numbers 1 to 10 with increment of 3:


Note:
by keyword to increment by the number as opposed to the default value of 1.
2.Alphabetical Ranges:
Selective range of alphabets using to,until, and by kewords.

Example:
i)Selective range using to, until keyword:


Note:
  • to keyword to include the the last upper bound character
  • until keyword to exclude the last upper bound character
ii)Range characters 'A' to 'Z' with specified increment of 3:


Note:
by keyword to increment by the character as opposed to the default value of 1.
iii)Convert Scala Range into collections:
Sometimes we may need to convert the Range into some other data structure such as List, Set, Sequence or an Array.


Below, which uses methods called toList, toArray, toSet and toSeq functions to convert Range

Example:



Note:

  • mkString() function -  create a string representation of each collection type. The .mkString() function takes in a delimiter.
  • toList, toArray, toSet and toSeq - to convert Range










No comments:

Post a Comment

Fundamentals of Python programming

Fundamentals of Python programming: Following below are the fundamental constructs of Python programming: Python Data types Python...