Tuesday, January 19, 2021

Empty, isEmpty, nonEmpty, isDefined, isDefinedAt in Scala (Difficulty: Basic)

 
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.



Read More

Saturday, January 2, 2021

2020: Books That Kept Me Sane Through This Very Strange Year


It has been an incredible year. I will not recap all of the negatives that happened this year. Experiencing and surviving a pandemic is enough. I don't think it would be possible for anyone to possibly forget this year so i will simply celebrate the good.  To have achieved nothing other than staying alive this year, in itself is an accomplishment. Along the way (i mean while locked at home with my thoughts), i found companionship in books which i have listed below that kept me going. 
Uploading: 1406182 of 2084233 bytes uploaded.

 

I planned to read a lot more books this year unfortunately life happened, other things took priority (working two full time engineering jobs is not a joke) but i was still able to get a few good books in during the lock-down. I hope someone finds any of the books useful, as i did. There is a little something in here for everyone. 
It would be impossible to select which books were my favorite but i tried to categorise them by genre below. If you have read any of the books listed above, please leave a comment and share. 

Self-Help/Sociology Titles
  1. Remote - Jason Fried 
  2. Outliers: The Story Of Success - Malcolm Gladwell
 
Fiction Titles
  1. City Of Thieves - David Benioff
  2. Homegoing - Yaa Gyasi
  3. White Teeth - Zadie Smith
  4. Free Food For Millionaires - Min Jin Lee
  5. Crime and Punishment - Fyodor Dostoyevski
 
Islamic Theology/Islamic-Feminist Theory 
  1. Believing Women In Islam: Un-reading Patriarchal Interpretations Of The Qur'an - Asmaa Barlas
  2. Qur'an and Woman: Rereading the Sacred Text from a Woman's Perspective - Amina Wadud
 
Non-Fiction Titles
  1. Beneath the Tamarind Tree: A Story of Courage, Family, and the Lost Schoolgirls of Boko Haram - Isha Sesay
  2. Hallucinations or Reality - Micheal C Richards
  3. History Of Humankind: Sapiens - Yuval Noah Harari
  4. Hausa Women in the Twentieth Century - Catherine Coles, Beverly Blow Mack
 
Health Title
  1. Green For Life - Victoria Boutenko


Read More