Hello Everyone!
Today my topic is the Scala programming language. This is a Language that I was introduced to in College at UMASS Amherst.
Scala is a very cool Language because it runs on the JVM (Java Virtual Machine). Not only does it run on the JVM but it uses java libraries at its core. Essentially Scala’s aim is to have a highly scalable development language and also offer all the perks of Java. Scala is an Object Oriented-Functional Language, now this may seem odd if you have not encountered a functional language before but its is amazing and I can’t wait to show you why!
Immutable Data Structures
So to start Scala has all the basic data structures you could ever want. Lists,Maps, etc.
These all inherit from the trait Traversable. Now Traits at a basic view point is a interface that can have some implementation. Although I am not sure if they offer the same benefit of no overhead like interfaces. (Add that to the list to learn) Since all of these Data structures all are a “traversable” object it has methods that allow you to “convert” your structure to a list, map, array, set and a few more without any effort.
EX:
CODE Goes here ….example of a list that is made into a array.
Now lets think for a second…..since they all are a traversable object than would it not makes sense for it to have functions that allow you to TRAVERSE your structure. Of course you’re a genius! (Or Scala Creators are)
You can simply iterate through your structure and preform functions on each element much like you would in java but….no for loops!
EX:
CODE Goes here …. example of a list being multiplied by any number and some output to prove it.
Wait! I have not addressed the elephant in the room…All of these structures are Immutable. Now if you are new to coding or have not seen this before this means that the structure can NOT be changed. Every time you modify the data structure it will create a “new” structure with the changes you have made. This allows it to be safe to be used between threads with no issues because it can not be corrupted adding known reliability to your project.
Readable code
Now I am sure some seasoned developers out there may not like this because all languages are designed to be “human readable” yet so far in my experience I have not read anything that is so intuitive to read and understand. Most java algorithms can be rewritten in Scala with less lines and a simpler feel to them. Now its time for me to back up my claims;
- No Semi colons
- Spaces instead of the dot to call upon a method of a object.
- Operators that are created to reduce the words used
- +: this operator is how you append an object to a list
- :: will add to the beginning of a list
I could go on for a while so check out the API docs: http://www.scala-lang.org/api
Scala EX VS Java EX:
CODE Goes here
Object Creation
As many of you know a Object is instance of a Class. Yet when you have a Main method to access your program it is normally a class. Yet when you run your program it will become an object. Scala lets you define your Main method as an object to only allow one instance to be defined. This can be useful in the case of a database class that you would only want one object accessing your data at a time.
Pattern Matching
Functional Programming
Now to put functional programming in simple terms it is a language that has functions/methods that always output a value. Of course it has an equivalent to void yet it allows you to pass a method a function that has areturn type of int and it will accept your method as a valid argument. This is very different from traditional Object Oriented programming, where you would have to store the returned result as a variable and pass it to the method after. This as you imagine reduces code length and keeps it short and sweet.
Sources for more technical explanations:
Immutable Data Structures Pros and Cons:
https://www.quora.com/What-are-the-advantages-and-disadvantages-of-immutable-data-structures
Scala API:
http://www.scala-lang.org/api/
From the blog CS443 – Triforce Code| Exploring and Learning by CS443 – Triforce Code| Exploring and Learning and used with permission of the author. All other rights reserved by the author.