Functional programming is fun. Scala is fun-ner. There are so many little helpful functions in scala that make writing code less tedious and a lot more exciting. Scala took Java's verbosity and smashed it with a baseball bat and I am here for it. Throughout these series, I will discuss useful helper functions (scala in-built) that just make life easier for you and your data structure of choice (based on use-case/performance etc).
We will start with functions Empty, isEmpty, nonEmpty, isDefinedAt and isDefined. These functions are used on different types of scala data structures, across mutable and immutable collections. We will briefly look into these data structures before getting into which functions are applicable to what data structure.
Collection
Scala Collections are the containers that hold a sequenced linear set of items like List, Set, Tuple, Option, Map etc. Collections may be strict or lazy. The memory is not allocated until they are accessed. Scala has mutable and immutable collections. According to the scala docs, A mutable collection can be updated or extended in place. This means you can change, add, or remove elements of a collection as a side effect. Immutable collections, by contrast, never change. You still have operations that simulate additions, removals, or updates, but those operations will in each case return a new collection and leave the old collection unchanged.
Examples of mutable collections are:
Array Buffers
List Buffers
StringBuilders
Linked Lists
Double Linked Lists
Mutable Lists
Queues
Array Sequences
Stacks
Array Stacks
Hash Tables
Hash Maps
Mutable Bitsets
Examples of immutable collections are:
Lists
Streams
Vectors
Immutable stacks
Immutable Queues
Ranges
Hash Tries
Red-Black Trees
Immutable BitSets
List Maps
Option
Scala's Option represents optional values. Instances of Option are either an instance of scala Some or the object None. (note: Options have some useful functions like get and getOrElse to convert an option of a type into a type which will come in another article)
Now that you are familiar with the data structures, let's get into the functions.
isDefined — True if not empty
isEmpty — True if empty
nonEmpty — True if not empty
You can use isEmpty to evaluate both scala Option and also to test for emptiness on a scala Collection. At the source of scala isEmpty, this is what you would find.
final def nonEmpty = isDefined
Now based on the above example, nonEmpty and isDefined are the same thing. Much wow, right?
Okay more examples:
val caseOne = Some("Jane")
val caseTwo = None
val collectionOne = Seq("Jane", "John", "Ali")
val collectionTwo = Seq.empty
caseOne.isEmpty : false
caseOne.nonEmpty : true
caseTwo.isEmpty : true
caseTwo.nonEmpty : false
collectionOne.isEmpty : false
collectionOne.nonEmpty : true
collectionTwo.isEmpty : true
collectionTwo.nonEmpty : false
caseOne.isDefined : true
caseTwo.isDefined : false
collectionOne.isDefined : value isDefined is not a member of Seq[String]
did you mean isDefinedAt?
collectionTwo.isDefined : value isDefined is not a member of Seq[String]
did you mean isDefinedAt?
collectionOne.isDefinedAt(0): true
collectionTwo.isDefinedAt(0) : false
isEmpty function is applicable to both Scala's Mutable and Immutable collection data structures. It is also applicable to the generic data types like Strings and Options. It checks whether a given collection/string/option is empty and will return a boolean response (either true or false). The same applies to nonEmpty which checks if a variable is not empty and returns true or false.
Empty on the other hand creates an empty collection.
isDefined checks if a variable of type Option is defined. For collections, one would have to invoke the
isDefinedAt which checks a particular index of the collection. In the above example, we check for index 0 on collectionOne which returns true but when we tried to invoke isDefined on a collection,
This brings us to the end of this series. If you notice, all our responses are boolean responses, this is not a coincidence (insert wink). As we go further into scala and its uses, syntaxes and semantics, we will discuss other non-boolean return type functions and hopefully, get to have some fun along the way.