Scala: From Zero to Algorithms

Introduction

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 http://scalatutorials.com/. 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!

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)

Variables are declared using the var keyword
In many cases, the type information can be omitted

1

2

varx:Int=1+2

varx=1+2// x = 3

Final variables are declared using the val keyword (a final variable is a variable that can’t be reassigned)

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

Call function

1

2

increase(10)

// res0: Int = 11

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

//a method that requires a function as a parameter

//the function's type is (Int,Int) => Int

//e.g. maps from 2 Ints to an Int

def doWithOneAndTwo(f:(Int,Int)=>Int)={

f(1,2)

}

//Explicit type declaration

val call1=doWithOneAndTwo((x:Int,y:Int)=>x+y)

//The compiler expects 2 ints so x and y types are inferred

val call2=doWithOneAndTwo((x,y)=>x+y)

//Even more concise syntax

val call3=doWithOneAndTwo(_+_)

println(call1,call2,call3)

Practice

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

1

2

3

4

5

6

7

8

9

10

11

def fibonacci(n:Int):Int=nmatch{

case0|1=&gt;n

case_=>fibonacci(n-1)+fibonacci(n-2)

}

println("fibonacci 0: "+(fibonacci(0)))

println("fibonacci 1: "+(fibonacci(1)))

println("fibonacci 2: "+(fibonacci(2)))

println("fibonacci 3: "+(fibonacci(3)))

println("fibonacci 4: "+(fibonacci(4)))

println("fibonacci 5: "+(fibonacci(5)))

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)