11. Shared Memory 사용않고 개발은?
Functional language 사용
erlang : Actor
erjang : jvm 위에서 사용하는 erlang
clojure : jvm, .net에서 돌아가는 lisp
scala : scalable 라고 jvm,.net에 돌아
감
19. ㅋ!
Martin Odersky
Since 2003 Sponsored by EPFL
Pragmatic
Runs on the JVM
Statically typed
Growing new types
Seamless integration with Java
20. Concise
// Java
public class Person {
private String name;
private int age;
public Person(String name,
int age)
{
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String
name) {
this.name = name;
}}
// Scala
class Person( var name:
String, var age: Int)
23. 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
26. 변수 선언
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( var name: 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에서는 static type이 없어서 object을
이용하거나 import로 static을 대신할 수 있다.
사용방법은 똑같은 이름의 class가 같은 scala
파일에 있어야 한다.
class와 똑같은 명칭의 object을 선언하면 사용
할 수 있다.
30. object
object SingleObject {
def printSingle(a:String) {
print(a)
}
}
scala> val t = SingleObject
scala> t.print(“test”)
test
scala> SingleObject.print(“test”)
31. Companion object
// In file ChecksumAccumulator.scala!
class ChecksumAccumulator { !
private var sum = 0 !
def add(b: Byte) { sum += b } !
def checksum(): Int = ~(sum & 0xFF) + 1!
}
32. // In file ChecksumAccumulator.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)
40. Stackable
modifications
val set = new java.util.HashSet[String]
with IgnoreCaseSet
set.add(“HI THERE“) // uppercase
set.contains(“hi there“) // lowercase
=> true
48. FP - higher order
List(1, 2, 3).map((x: Int) => x + 1)
List(2,3,4)
49. currying
def sum(f: Int => Int): (Int, Int) => Int = { !
def sumF(a: Int, b: Int): Int =!
if (a > b) 0 else f(a) + sumF(a + 1, b) !
sumF!
}
scala> sum(x=>x*x)(1,10)
result : Int = 385
50. currying-tail
def sum(f: Int => Int)(a: Int, b: Int): Int = { !
def iter(a: Int, result: Int): Int = {!
if (a>b) result else iter(a+1, f(a)+result)!
iter(a, 0)!
}
scala> sum(x=>x*x)(1,10)
result : Int = 385
51. List 만들기
val list = List(1,2,3)
val list2= 1 :: 2 :: 3 :: Nil
val list3 = List(1) ::: List(2) ::: List(3)
val list4 = 1 :: (2 :: (3 :: Nil))
list == list2 ==list3 == list4
=> true
52. List 이해
val list = List(1,2,3)
list.head == 1 => true
list.tail == List(2,3) => true
val head :: tail = List(1,2,3)
head == 1 => true
tail == List(2,3) => true
54. 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