Shared Memory 사용않고개발은?
Functional language 사용
erlang : Actor
erjang : jvm 위에서 사용하는 erlang
clojure : jvm, .net에서 돌아가는 lisp
scala : scalable 라고 jvm,.net에 돌아
감
Scala high level
//this is Java
boolean nameHasUpperCase = false;
for (int i = 0; i < name.length(); ++i) {
if (Character.isUpperCase(name.charAt(i)))
{
nameHasUpperCase = true; break;
}
}
val nameHasUpperCase = name.exists(_.isUpper)
24.
Scala’s root
많은 언어들에서영향을 받음
문법은 기본적으로 Java, C#을 채용함
Convention은 C,C++에 빌려옴
Object models 기본적으로 Smalltalk, 부분
적으로 Ruby
Actor models은 erlang
변수 선언
val msg= “Hello, World!”
var msg2 = “Hello, World!”
val : FP 형태의 immutable 선언방식
var : Java 형태의 mutable 선언방식
27.
함수 선언
def max(x:Int, y: Int): Int =
{
if (x > y) x
else y
}
def max2(x: Int, y: Int) = if
(x > y) x else y
28.
class
class Person( varname: String, var age: Int)
class ChecksumAccumulator {
// class definition goes here
}
class Person( var name: String, var age: Int) {
var name:String = name;
var age: Int = age;
}
val person = new Person(“jaejin”,30)
val checksum = new ChecksumAccumulator
29.
object
싱글톤 클래스
scala에서는 statictype이 없어서 object을
이용하거나 import로 static을 대신할 수 있다.
사용방법은 똑같은 이름의 class가 같은 scala
파일에 있어야 한다.
class와 똑같은 명칭의 object을 선언하면 사용
할 수 있다.
30.
object
object SingleObject {
defprintSingle(a:String) {
print(a)
}
}
scala> val t = SingleObject
scala> t.print(“test”)
test
scala> SingleObject.print(“test”)
31.
Companion object
// Infile ChecksumAccumulator.scala!
class ChecksumAccumulator { !
private var sum = 0 !
def add(b: Byte) { sum += b } !
def checksum(): Int = ~(sum & 0xFF) + 1!
}
32.
// In fileChecksumAccumulator.scala
import scala.collection.mutable.Map
object ChecksumAccumulator {
private val cache = Map[String, Int]()
def calculate(s: String): Int =
if (cache.contains(s))
cache(s)
else {
val acc = new ChecksumAccumulator
for (c <- s)
acc.add(c.toByte)
val cs = acc.checksum()
cache += (s -> cs)
cs
}
}
33.
expression
var filename ="default.txt" !
if (!args.isEmpty)!
filename = args(0)
val filename = !
if (!args.isEmpty) args(0) !
else "default.txt"
for (i <- 1 to 4) !
println("Iteration "+ i)
val filesHere = (new java.io.File(".")).listFiles !
for (file <- filesHere if file.getName.endsWith(".scala"))!
println(file)
pattern matching
def matchAny(a:Any): Any = a match {
case 1 => “one”
case “two” => 2
case i: Int => “scala.int”
case <tag>{ t }</tag> => t
case head :: tail => head
case _ => default
}
55.
xml
abstract class CCTherm{ !
val description: String !
val yearMade: Int !
val dateObtained: String !
val bookPrice: Int!// in US cents !
val purchasePrice: Int // in US cents !
val condition: Int! // 1 to 10!
override def toString = description!
def toXML = <cctherm>!
<description>{description}</description> !
<yearMade>{yearMade}</yearMade> !
<dateObtained>{dateObtained}</dateObtained> !
<bookPrice>{bookPrice}</bookPrice> !
<purchasePrice>{purchasePrice}</purchasePrice> !
<condition>{condition}</condition>!
</cctherm>!
}
56.
xml
scala> val therm= new CCTherm { !
val description = "hot dog #5"!
val yearMade = 1952 !
val dateObtained = "March 14, 2006" !
val bookPrice = 2199 !
val purchasePrice = 500 !
val condition = 9!
}!
therm: CCTherm = hot dog #5!
scala> therm.toXML!
res6: scala.xml.Elem = <cctherm>!
<description>hot dog #5</description> !
<yearMade>1952</yearMade> !
<dateObtained>March 14, 2006</dateObtained> !
<bookPrice>2199</bookPrice> !
<purchasePrice>500</purchasePrice> !
<condition>9</condition>!
</cctherm>
57.
actor
val echoActor =actor { !
while (true) { !
receive { !
case msg =>!
println("received message: "+ msg)!
}!
}!
}
58.
actor
scala> echoActor !"hi there"
received message: hi there!
scala> echoActor ! 15
received message: 15