back to all projects

LEARNING SCALA

BASICS OF SCALA, OBJECT-ORIENTED SCALA, FUNCTIONAL PROGRAMMING SCALA


project owner: David Campos

all contributors: David Campos

tech: SCALA

level: HARD

last hacked on May 20, 2017


Scala is a general-purpose programming language that features both object-oriented and functional programming attributes. Scala was first released in 2004 by Martin Odersky, striving to add the functional paradigm to the already object-oriented Java platform. As such, Scala is highly scalable, extensible, and portable. This makes the Scala language a very attractive programming language in the world of big data processing. **Scala is object-oriented.** Scala is said to be a *pure* object-oriented language in that every value is an object; hence, Scala does not have _primitive_ data types. And, since all data types are objects, they all have defined properties and callable methods. **Scala is functional.** Scala is functional in that every function is a value. And since every value is an object, every function is an object. Scala features support for anonymous functions, higher-order functions, nesting, and currying. **Scala is compiled.** Scala source code (just as Java source code) compiles to Java bytecode, and then it runs on the Java Virtual Machine (JVM). The JVM is an abstract computing machine that enables a computer to run a Java program.

# Scala Basics ## Data Types `Boolean` * True or false `Byte` * 8-bit signed integer `Char` * 16-bit signed integer `Short` * 16-bit signed integer `Int` * 32-bit signed integer `Long` * 64-bit signed integer `Float` * 32-bit signed integer `Double` * 64-bit signed integer ## Data Structures Scala data structures are known as _collections_. Data structures, as the name suggests, are structures of data; which, in the case of Scala, represent structures of objects. ### Lists Scala lists are ordered, linear sets of items. Each element within a list contains an object, which may be any data type. Here, we declare a list of integers: scala> val intsList = List(1, 2, 3, 4, 5) And here, we declare a list of mixed data types: scala> val mixedList = List(1, "David", List(7,3), 4, 5) Notice that `mixedList` contains integers, a string, and a list of its own. ### Sets Scala sets are unordered sets of unique elements. Because they are unordered, you may not access its elements by index. Here, we declare a set of integers: scala> val intsSet = Set(1, 2, 3, 4, 5) Sets provide useful methods such as `min`, `max`, `count`, `find`, `filter`, `diff`, `intersect`, and `union`. Let's apply the `min` method to `intsSet`: scala> intsSet.min ### Tuples I like turtles. ### Maps I like turtles. ## Hello World Open terminal and `cd` to the directory of choosing. Then create the source code file: $ touch HelloWorld.scala Open `HelloWorld.scala` with your preferred text editor or IDE and input this source code: object HelloWorld { def main(args: Array[String]) { println("Hello World!") } } Compile the source code. Enter into the terminal: $ scalac HelloWorld.scala This should create two files: `HelloWorld.class` and `HelloWorld$.class`. These files contain bytecode, which runs on top of the Java Virtual Machine (JVM). To run your program, enter into your terminal: $ scala HelloWorld ## If Then Create the source file: $ touch IfThen.scala Open `IfThen.scala` and input this source code: object IfThen { def main(args: Array[String]) { if(2==2){ println("2 equals 2!"); } } } Compile the source code. Enter into the terminal: $ scalac IfThen.scala To run your program, enter into your terminal: $ scala IfThen ## While Loop Create the source code file: $ touch WhileLoop.scala Open `WhileLoop.scala` and input this source code: object WhileLoop { def main(args: Array[String]) { var x = 0; while (x <= 10){ println(x); x = x + 1; } } } Compile the source code; enter into the terminal: $ scalac WhileLoop.scala Run your program; enter into the terminal: $ scala WhileLoop ## For Loop Create the source code file: $ touch ForLoop.scala Open `ForLoop.scala` and input this source code: object ForLoop { def main(args: Array[String]) { for (x <- 0 to 10) { println(x); } } } Compile the source code: $ scalac ForLoop.scala Run the bytecode: $ scala ForLoop # Object-Oriented Programming in Scala I like turtles & objects. ## Classes I like turtles. ## Inheritance I like turtles. ## Polymorphism I like turtles. # Functional Programming in Scala I like turtles and functions. ## First-Class Functions I like turtles. ## Anonymous Functions I like turtles. ## Higher-Order Functions I like turtles. ## Closures I like turtles. ## Currying I like turtles. # Forgive Me Missing material on: * **Data structures** * **Object-oriented scala** * **Functional scala** * **Interesting code**

back to all projects