- Download and install SBT - http://www.scala-sbt.org/download.html
- Create SBT Scala Project by running this command - sbt new sbt/scala-seed.g8
- When prompted for the project name, type
HelloWorld. This will create a new project under a directory named HelloWorld.

- Run
the sample application by access HelloWorld folder, type sbt, compile and run.

- Add sbt-assembly as a dependency in project/assembly.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5") - Type assembly.

- Now you'll have an awesome new assembly task which will compile your project, run your tests, and then pack your class files and all your dependencies into a single JAR file: target/scala_X.X.X/projectname-assembly-X.X.X.jar.
- Type java -jar target/scala_X.X.X/projectname-assembly-X.X.X.jar to run the program.
Showing posts with label Scala. Show all posts
Showing posts with label Scala. Show all posts
Tuesday, July 4, 2017
Create Scala Project using SBT and Jar using sbt-assembly
Monday, December 5, 2016
JavaScript Evolution ECMAScript 6 - Function with Default Parameter
In ES2015, a developer can add a default parameter into a function. Example:
function loadProfile(usernames=[]){
if (usernames.lenght > 3){
let loadingMessage ="This will take a while.";
}
}
With default parameter, you can call the function eitherloadProfile(['Simon','Alex','Arlene']);
OrloadProfile();
Scala has more powerful default parameter. Let's create Scala function with default parameter.def addInt(a:Int=5, b:Int=7) = {
var sum: Int = a + b;
return sum
}
You can call the function by passing the parameter using a pointer.addInt(b=10)
Passing parameter using pointer is not implement in JavaScript.
Tuesday, November 17, 2015
Scala map vs flatMap
map is loop thru all element and apply a function into it.
http://www.brunton-spall.co.uk/post/2011/12/02/map-map-and-flatmap-in-scala/
http://alvinalexander.com/scala/collection-scala-flatmap-examples-map-flatten
scala> val i = List("Apple", "Banana", "Orange")
i: List[String] = List(Apple, Banana, Orange)
scala> i.map(x => x.toUpperCase)
res2: List[Char] = List(APPLE, BANANA, ORANGE)
flatMap is loop thru all element, flatten the element and apply a function into it.scala> val i = List("Apple", "Banana", "Orange")
i: List[String] = List(Apple, Banana, Orange)
scala> i.flatMap(x => x.toUpperCase)
res2: List[Char] = List(A, P, P, L, E, B, A, N, A, N, A, O, R, A, N, G, E)
Here is an example on different between map and flatMap in List(List(), List())scala> val l = List(1,2,3)
l: List[Int] = List(1, 2, 3)
scala> def x(v:Int) = List(v,v+1)
x: (v: Int)List[Int]
scala> l.map(i => x(i))
res5: List[List[Int]] = List(List(1, 2), List(2, 3), List(3, 4))
scala> l.flatMap(i => x(i))
res6: List[Int] = List(1, 2, 2, 3, 3, 4)
Here is an example on different between map and flatMap in Option (None and Some). flatMap remove empty value (None).scala> val l = List(1,2,3,4,5)
l: List[Int] = List(1, 2, 3, 4, 5)
scala> def f(x:Int) = if (x > 2) Some(x) else None
f: (x: Int)Option[Int]
scala> l.map( x => f(x))
res11: List[Option[Int]] = List(None, None, Some(3), Some(4), Some(5))
scala> l.flatMap( x => f(x))
res12: List[Int] = List(3, 4, 5)
References:http://www.brunton-spall.co.uk/post/2011/12/02/map-map-and-flatmap-in-scala/
http://alvinalexander.com/scala/collection-scala-flatmap-examples-map-flatten
Sunday, November 15, 2015
Scala For Loop Syntax
For Loop with Counter
The simplest loop syntax is:for (a <- 1 to 5){
println(“Print a: ” + a);
}
Print a: 1
Print a: 2
Print a: 3
Print a: 4
Print a: 5
By default, counter increment is 1. You can change the number of counter increment using "by".
for (a <- 1 to 5 by 2) {
println(“Print a: ” + a);
}
When the above code is compiled and executed, it produces following result:
Print a: 1
Print a: 3
Print a: 5
For Loop with Filter
You can filter out some of the elements. Following is the example of for loop along with filters:for (
a <- 1 to 5
if ( a > 3)
) {
println(“Print a: ” + a);
}
When the above code is compiled and executed, it produces following result:
Print a: 4
Print a: 5
For Loop with Yield
This is one of the awesome feature in Scala where you generate a static type variable using loop. I don’t really see in other programming language.When For loop finishes running, it returns a collection of all these yielded values.
The type of the collection that is returned is the same type that you were iterating over.
// Loop through and generate a variable
val i = for (
a <- 1 to 5
if ( a > 3)
) yield {
a
}
// Print the result
for( a <- i){
println( "Value of a: " + a );
}
When the above code is compiled and executed, it produces following result:
Print a: 4
Print a: 5
Sunday, December 21, 2014
A great scala introduction with all available tools for developer
http://assist-software.net/blog/awesome-scala
Tuesday, November 4, 2014
Using Scala Recursion Functions For Permutation Calculation
Permutation is an ordered combination - how many possible ordered combination.
There are 2 types of Permutation:
But how do we write that mathematically? Answer: we use the "factorial function".
There are 2 types of Permutation:
- Permutation with Repeat is Allowed
Permutation Lock is an example of Repeat Allowed.
There are 10 numbers to choose from (0,1,...9) and we choose 3 of them.
10 x 10 x 10 = 1000 permutationFormula: nr where n is the number of things to choose from, and r is number of times - Permutation with No Repeat

A good example is lottery which number can not be repeat.
There are 3 numbers to choose from (1,2 and 3) and we choose 3 of them.
3 x 2 x 1 = 6 permutationFormula: Without repetition our choices get reduced each time.
object Permutations {
def main(args: Array[String]) {
print("How many lottery number? ")
val num1 = readInt()
println()
print("How many lottery number to pick? ")
val num2 = readInt()
println("Total Permutation: " + factorial(num1,num2).toString)
}
def factorial(x: BigInt, y: BigInt) : BigInt = {
if (y > 1)
x * factorial( x - 1, y - 1)
else
x
}
}
Tips: Remember to has exist call on recursive function to avoid unstopped loop
Subscribe to:
Posts (Atom)
