Archive for the ‘scala’ Category
SCALA – Future untuk Proses Asynchronous
Hal yang perlu dicatat dari kode program di atas adalah:
import scala.actors.Future
import scala.actors.Futures._
object App {
def main(args: Array[String]) = {
var results = List[Future[Int]]()
for (i <- 1 to 10) {
println("Sending " + i + "...")
val f = future {
println("Processing " + i + "...")
Thread.sleep(500)
println("Processed " + i)
i
}
println("Sent " + i)
results = results ::: List(f)
}
results.foreach(future =>
println("result: " + future()))
}
}
- foreach pada baris terakhir bersifat blocking. Apabila pada suatu waktu T, dia telah memiliki nilai 1, 2, 3, 5, dan 7; dia akan memprint 1, 2, 3 lalu menunggu 4 selesai diproses, barulah dia lanjut ke 5
- TBD
Kode program yang mendelegasikan proses lalu mengharapkan (nilai) kembalian membutuhkan sistem Future dan Promise.
SCALA – Future untuk Proses Asynchronous
Hal yang perlu dicatat dari kode program di atas adalah:
import scala.actors.Future
import scala.actors.Futures._
object App {
def main(args: Array[String]) = {
var results = List[Future[Int]]()
for (i <- 1 to 10) {
println("Sending " + i + "...")
val f = future {
println("Processing " + i + "...")
Thread.sleep(500)
println("Processed " + i)
i
}
println("Sent " + i)
results = results ::: List(f)
}
results.foreach(future =>
println("result: " + future()))
}
}
- foreach pada baris terakhir bersifat blocking. Apabila pada suatu waktu T, dia telah memiliki nilai 1, 2, 3, 5, dan 7; dia akan memprint 1, 2, 3 lalu menunggu 4 selesai diproses, barulah dia lanjut ke 5
- TBD
Kode program yang mendelegasikan proses lalu mengharapkan (nilai) kembalian membutuhkan sistem Future dan Promise.
SCALA – Menyediakan Konfigurasi bagi Aplikasi
Asumsikan bahwa kita mempunyai struktur folder sebagai berikut:
PROJECT_NAME
+ conf
+ datamover
+ config.properties
+ libs
+ bin
+ run.sh
Di file run.sh, pastikan bahwa semua folder (libraries, configurations) telah diinclude dalam CLASSPATH, sehingga CLASSPATH=conf;conf/datamover;libs;bin
Ada beberapa cara setting variables
1. Menggunakan config file
Untuk mengakses
import java.util.Properties
import java.io._
val DEFAULT_CONFIG : String = "config.properties"
val fileProperties : Properties
def getFileProperties(propertiesFileName: String = DEFAULT_CONFIG): Properties = {
if (fileProperties == null) {
fileProperties = new Properties
fileProperties.load(new FileReader(
new File(AppDocExtractor.getClass.getResource(propertiesFileName).toURI)))
}
fileProperties
}
fileProperties = getFileProperties(DEFAULT_CONFIG)
if (fileProperties.getProperty("sleep.second") != null) {
val sleepSecond : Int = fileProperties.getProperty("sleep.second").toInt
}
2. Mengambil parameter dari pemanggilan aplikasi
Misalnya ada aplikasi dipanggil dengan: java -Dusethread=1
USE_THREAD_PARAM = "usethread"
val isUseThread = if (System.getProperty(USE_THREAD_PARAM) != null)
if ("1".equals(System.getProperty(USE_THREAD_PARAM))) true else false
else false
SCALA – Menyediakan Konfigurasi bagi Aplikasi
Asumsikan bahwa kita mempunyai struktur folder sebagai berikut:
PROJECT_NAME
+ conf
+ datamover
+ config.properties
+ libs
+ bin
+ run.sh
Di file run.sh, pastikan bahwa semua folder (libraries, configurations) telah diinclude dalam CLASSPATH, sehingga CLASSPATH=conf;conf/datamover;libs;bin
Ada beberapa cara setting variables
1. Menggunakan config file
Untuk mengakses
import java.util.Properties
import java.io._
val DEFAULT_CONFIG : String = "config.properties"
val fileProperties : Properties
def getFileProperties(propertiesFileName: String = DEFAULT_CONFIG): Properties = {
if (fileProperties == null) {
fileProperties = new Properties
fileProperties.load(new FileReader(
new File(AppDocExtractor.getClass.getResource(propertiesFileName).toURI)))
}
fileProperties
}
fileProperties = getFileProperties(DEFAULT_CONFIG)
if (fileProperties.getProperty("sleep.second") != null) {
val sleepSecond : Int = fileProperties.getProperty("sleep.second").toInt
}
2. Mengambil parameter dari pemanggilan aplikasi
Misalnya ada aplikasi dipanggil dengan: java -Dusethread=1
USE_THREAD_PARAM = "usethread"
val isUseThread = if (System.getProperty(USE_THREAD_PARAM) != null)
if ("1".equals(System.getProperty(USE_THREAD_PARAM))) true else false
else false
SCALA – Menjalankan Aplikasi Scala sebagai Console
1. mendefinisikan fungsi main
object MainApplication {
def main (args: Array[String]) : Unit = {
Console.println("Hello World");
}
}
2. inherit App
object MainApplication extend App {
Console.println("Hello World");
}
SCALA – Menjalankan Aplikasi Scala sebagai Console
1. mendefinisikan fungsi main
object MainApplication {
def main (args: Array[String]) : Unit = {
Console.println("Hello World");
}
}
2. inherit App
object MainApplication extend App {
Console.println("Hello World");
}
? State of Scala IDE on Eclipse
Even thought Scala is now among the trendy programming languages, it still misses a lot before it is considered as one of the major players.
Most Scala programmers are they who are not satisfied with Java, so it’s natural that most of them were before programming with Java. One of the things Java programmers miss from Scala is a robust and functional IDE. Since I’m a big fans of Eclipse, this means Scala IDE for Eclipse. While surely the improvements have introduced to the plugin, there are many things left out.
So what are still missing? This is my list based on several months experience with Scala IDE. If some missing features are missing (duh…), just comment and I’ll try to update the post.
- No refactoring. I miss refactoring a lot. I still remember the day I find refactoring features on RefactorIT for JBuilder. It’s like the day you saw sun for the first time. And not long after that, I found Eclipse with its build-in refactoring features. Call me lazy programmer, but programmer should be lazy, isn’t it?
- The content assist takes (almost) forever. Yesterday I found myself restarting Eclipse about five times just because I’m programming with Scala. And I’ve increase the timeout of content assist ten folds.
- Content Assist doesn’t show (not nearly) all posibilities. When it works, it doesn’t even show nearly all possibilities I want to have. On some cases, it even can’t show a pretty simple completion for a field. As small example: if I write ‘va’, I hope I can get ‘var’ and ‘val’ as the first entries of the completion suggestion.
- Limited formatting options. Eclipse’s Java Formatter is the best configurable formatter I’ve ever saw. This Scala formatter doesn’t even come near it.
- No suggestion, not even correction suggestion. I often use Ctrl+1 in Java editor to assign the statement to a variable. Not possible. If you got errors, you got no correction suggestion.
- No integration to scala documentation. Weird, but the Scala editor can show Javadoc but no Scala documentation is accessible.
- No auto import. I ended up importing the whole package everytime I need a class.
But yes, I still put a lot of hope woth Scala IDE. It shouldn’t take a lot of time, because the code is in Scala and Scala means productivity, right?
State of Scala IDE on Eclipse originally appeared on firdau.si on January 28, 2011.
? Representation of class in Scala
Seems very simple but can be source of frustration. So, how to represent class in Scala?
In Java, you refer to a class by adding .class
at the end of the class name. In Scala, you get the equivalent of that by using classOf[]
.
An example for referring String class is as follow:
classOf[String]
You can read more about Scala Type System here.
Representation of class in Scala originally appeared on firdau.si on January 27, 2011.