Scala: From Zero to Algorithms


Hi all,

In previous post, I have guided you to prepare the environment, install IDE, and write first Hello World Scala application using IntellJ. If you have not read it, please take a quick look at this link

Today, I will introduce somethings basic of Scala, and solve some basical algorithms using Scala with you guys. I will continue the rests in next some articles.

First, I want to introdue that Scala is functional programming + object oriented programming. How you guys differentiate procedures, methods and functions? Check out my opinion!

– Procedure: A sequence of statements. such as assignments, tests, loops and invocations of sub procedures. These procedures are functions, which map arguments to return statements.

– Method: Something like procedure, however, it is belong to class or object. And can be public, private or protected. It is also can be overloaded. In order to use method, you call via a class or object. However, In my opinion, you cant use methods as parameters, or another type of variables.

– Function: You can google Scala functions, It is also a sequence of statements. However, It much more useful and flexible. You can use functions as other functions parameters, types of variables.

Ok, now what you are going to do is some basic of Scala, ofcourse, including functional programming.
Below are what I summarize from this You can check it out for more details. I just summarize first part of this tutorial in this post. Next parts will be in my next articles.

Basic Scala – part 1

Please read this to the end and practice with the project you have created at previous article by simply typing into it!

  1. In Scala, primitives are represented as objects. If you are familiar with Ruby, you will understand this (Although after compilation they are using Java’s primitives when possible for performance).Since they are objects, operators are simply method calls!
    So 1 + 2 is simply calling a method named + on the object 1 (an Int literal)
  2. Variables are declared using the var keyword
    In many cases, the type information can be omitted
  3. Final variables are declared using the val keyword (a final variable is a variable that can’t be reassigned)
  4. String interpolation
    From Scala 2.10, string interpolation is supported.
  5. Number operations
  6. String operations
  7. Useful methods on collections
  8. In Scala methods are defined using def
  9. return multiple variables using Tuples
  10. Declare and assign multiple variables to the same value.
  11. Loops using while
  12. Loops using for
  13. Conditions
  14. Multiple cases
  15. Array
  16. Functional programming
    Example: (You can practice on terminal via command scala)

    This means: “A function that map from integer (Int) to (=>) an integer (Int)

    Call function

    Another example of calling anonymous function as parameter. (You can back to your IDE)


Now, I think it is enough for the first basic part of Scala. And now I want to solve some small problems as practices

The first algorithm I want to solve is the very famous Fibonacci algroithm

The next program should be one of the problem we did together at TDD bootcamp: closedRanged
I want to deploy three functions in this: initialize range, select lower end point, and select upper end point. In each function, we should have a message error if range is not valid. Below are examples functions (ofcourse, at this time, it’s not neccessary to refactor these three functions)

Add a Comment

Your email address will not be published.