Category Archives: scala

Scala for impatient Reading notes

scala for impatient

Table of contents

1.Basic
2.Control structure and function
3.Array and operations
4.Map and Array
5.Class
6.Object
7.Package and import
8.Inheritance
9.File and regular expression
10.Trait
11.Operator
12.High-order function
13.Collections
14.Pattern matching and case class
15.Annotation
16.XML
17.Type parameters
18.Advanced type
19.Analysis
20.Actor
21.Implicit conversion and implicit parameter
22.Continuations

## Basic ##

Val defines a const, var defines a variable;
Types:  
> basic numerical types, Boolean, String, classes;
> scala do not differenciate basic types and reference types like java;

Operators like (+-*/) are actually method in scala:

a + b <=> a.+(b)

function and method call:
> function call:

import scala.math._
sqrt(2)
> scala doesn’t have static method, its corresponding feature is singleton object;

> class in scala has a companion object, the methods in the companion object are similar to the static method in java;

> the brackets of the method can be omitted if there is no parameter;

Apply method
> a kind of grammar similar to method calling;

val hello = “Hello”
hello(1) // ‘e’
// equals to
hello.apply(1)

> create the object of one class with the apply method in the companion object of the class;

val num = BigInt.apply(“999999”)

Some notes:
> String objective is usually converted implicitly to a StringOps object which own many string operators. Similar to Int, Double, Char -> RichInt, RichDouble, RichChar;

“Hello”.intersect(“World”)

> O and C near the class name in scala doc, C is class and O is its companion object;

> function can be used as the parameter of the method;

def count(p: (char) => Boolean) : Int // p is a function accpet a char parameter and return True or False

s.count(_.isUpper)

## Control structure and function ##

Get Started Scala-native

To get closer to bare metal, Scala-native is proposed. https://github.com/scala-native/scala-native

I build it in my own laptop with Mac osx system.

The environment setup can be referred here http://scala-native.readthedocs.io/en/latest/user/setup.html

I still met some problem when I run sbt in scala-native and I just record them for somebody also met these issues.

  • Cannot find sbt-cross plugin. You can download https://github.com/scala-native/sbt-cross and publish a local repository with sbt.
  • The path of the local publishing is ivy/local path. However, when building scala-native, the dependencies lookup path is ivy/cache. Once you want to use the local published repository, you can remove the folder of the same dependency under cache.

Building scala-native with:
sbt
publishLocal

Then you can run a native demo here by running:
demoNative/run

In this program, it imports your published native-scala libraries like this:

import scalanative.native._, stdlib._, stdio._

It is a real native problem and you can check its dependencies with otool

otool -L demo/native/target/scala-2.11/demonative-out
demo/native/target/scala-2.11/demonative-out:
/usr/local/opt/bdw-gc/lib/libgc.1.dylib (compatibility version 2.0.0, current version 2.3.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)