In [1]:
1+1
Out[1]:
In [2]:
1.0/2
Out[2]:
In [3]:
"a"+"b"
Out[3]:
In [4]:
"a"+("b")
Out[4]:
In [ ]:
1 + 1
:type 1
1.0 / 2
:type 1.0
"a" + "b"
:t "a"
"ab" * 3
"a".+("b")
"ab".*(3)
"ab" + 'c'
'ab' + "c"
BigInt("12345679") * 9
val a = 1
var b = 2
b = 3
a = 4
val c = 1 + 2; c + 3
def size = 2
5 * size
def square(x: Int) = x * x
square(2)
square(square(2))
def sumOfSquare(x: Int, y: Int) = square(x) + square(y)
def abs(x: Int) = if (x >=0) x else -x
In [2]:
"ab"+3
Out[2]:
In [3]:
"a".*(3)
Out[3]:
In [4]:
"a"*3
Out[4]:
In [5]:
"ab" + 'c'
Out[5]:
In [6]:
'a' + "bc"
Out[6]:
In [6]:
val a = 1
a = 2
In [7]:
var b = 2
b = 3
Out[7]:
In [7]:
"hello".toUpperCase
// argument가 없는 경우
Out[7]:
In [8]:
// argument가 있는 경우
// sequence하면 모두 take가 가능
"heelo".take(3)
Out[8]:
In [9]:
"hello".take(2)
Out[9]:
In [10]:
// Infix operator
1+1
Out[10]:
In [11]:
// 위와 같은 표현
1.+(1)
Out[11]:
In [12]:
"hello" take 2
Out[12]:
In [13]:
1
// 32 bit
Out[13]:
In [14]:
1.0
// 64 bit
Out[14]:
In [15]:
1.0f
Out[15]:
In [16]:
1L
Out[16]:
In [18]:
true
Out[18]:
In [19]:
false
Out[19]:
In [20]:
'a'
Out[20]:
In [21]:
"hello world"
Out[21]:
In [26]:
"""hello
|
|!!!"""
Out[26]:
In [27]:
""" "hi" """
Out[27]:
In [23]:
null
// 스칼라에선 null을 안쓰는 방식으로 코딩하는 것을 추천
Out[23]:
In [24]:
()
In [41]:
println("hello world")
In [29]:
:type println("hello-world")
// Unit이라고 떠야하는데 왜 오류지 : repl에선 됨
In [ ]:
In [28]:
object Test
Out[28]:
In [29]:
Test
Out[29]:
In [30]:
object Test {}
Out[30]:
In [31]:
Test
Out[31]:
In [36]:
object Test2 {
def name: String="홍길동"
def name1: String="바보"
}
Out[36]:
In [35]:
Test2.name
Out[35]:
In [37]:
Test2.name1
Out[37]:
repl에서 여러줄을 사용하려면 :paste라고 쓰고 해야함!
In [1]:
object Test3 {
def hello(to: String): String="Hello " + to
}
In [2]:
Test3.hello("Alice")
Out[2]:
In [40]:
Test3 hello "hi"
Out[40]:
In [43]:
object Test4 {
def hello(firstName: String, lastName: String="!"): String=
"Hello " + firstName + " " +lastName
}
// lastName이 없으면 "!"
Out[43]:
In [44]:
Test4.hello("tony")
Out[44]:
In [45]:
Test4.hello("tony", "stark")
Out[45]:
In [46]:
Test4.hello(lastName="hi", firstName="kkkk")
Out[46]:
In [51]:
object inc {
def apply(n: Int): Int = n+1
}
// 인자는 항상 타입을 적어줘야 함
// return 타입을 항상 작성해주는 것을 권장
Out[51]:
In [48]:
inc.apply(1)
Out[48]:
In [49]:
inc(1)
Out[49]:
In [54]:
def add(n: Int): Int ={
println("adding!")
n+1
}
// 마지막꺼만 타입 리턴
Out[54]:
In [55]:
add(10)
Out[55]:
In [60]:
def add(n: Int): Int ={println(s"adding $n"); n+1}
// s를 써야 %n에 타입을 넘길 수 있음
Out[60]:
In [61]:
add(10)
Out[61]:
In [62]:
object Test5 {
val name: String = "Alice"
def hello(other: String): String = name + " says hi to " + other
}
Out[62]:
In [63]:
Test5.name
Out[63]:
In [64]:
Test5.hello("Bob")
Out[64]:
In [67]:
object Test6 {
val simpleField = {
println("evaluating simpleField")
42
}
lazy val lazyField = {
println("Evaluating lazy")
42
}
def noParameterMethod = {
println("evaluating")
42
}
}
// 생성 당시엔 아무것도 실행이 안되고
// object가 호출되면 필드값들이 실행
Out[67]:
In [66]:
Test6
Out[66]:
In [66]:
// val은 처음 실행될 떄 한번
// lazy가 실행될 때 시작
// def는 계속
// 사이드 이펙트
In [68]:
Test6.lazyField
Out[68]:
In [69]:
Test6.simpleField
Out[69]:
In [70]:
Test6.simpleField
// println이 안됨! 처음만 실행
Out[70]:
In [71]:
Test6.noParameterMethod
Out[71]:
In [72]:
Test6.noParameterMethod
Out[72]:
In [72]:
// lazy는 드물게 호출되는 케이스에 사용
In [73]:
object foo {
def a = {
println("a")
1
}
val b = {
println("b")
a + 2
}
def c = {
println("c")
a
b + "c"
}
}
Out[73]:
In [74]:
foo.c + foo.b + foo.a
Out[74]:
In [74]:
// foo가 불리는 순간 b, a+2니까 a가 찍히고 c가 찍히고 a가 찍히고 b는 value라 안나오고
In [76]:
// vi HelloWorld.scala file
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
// main method를 실행!
// 리턴이 없는 Unit
Out[76]:
In [ ]:
> scala HelloWorld
In [76]:
> scalac HelloWorld.scala
// scalac는 컴파일 -> .class라는 파일 생성되며 $가 붙은 것은 추후 설명해드림
In [77]:
object HelloWorld extends App {
println("Hello, world!")
}
Out[77]:
In [77]:
// extends App이라고 달아주면 main 함수 안을 바로 작성할 수 있음
In [79]:
{1; 2; 3}
Out[79]:
In [80]:
{
println("This is a side-effect")
println("This is a side-effect as well")
3
}
Out[80]:
In [81]:
def name: String = {
val title = "Doctor"
val name = "Strange"
title + " " + name
}
Out[81]:
In [82]:
name
Out[82]:
In [83]:
if (1 < 2) "Yes" else "No"
// res: String = Yes
Out[83]:
In [ ]:
:type if (1 < 2) "Yes" else "No"
// String
In [84]:
if(1 < 2) println("Yes") else println("No")
// Yes
In [ ]:
:type if(1 < 2) println("Yes") else println("No")
// Unit
In [98]:
object calc {
def square(x: Double): Double = x*x
def square(x: Int): Int = x*x
def cubic(x: Double): Double = square(x)*x
def cubic(x: Int): Int = square(x)*x
}
// 함수 이름이 같아도 타입이 다르면 다른거가 됨!! 충격적임ㅋㅋㅋㅋㅋㅋㅋㅋㅋ
Out[98]:
In [95]:
calc.square(2)
Out[95]:
In [97]:
calc.cubic(3)
Out[97]:
In [92]:
calc.cubic(2.0)
Out[92]:
In [90]:
println(calc.square(2.0) == 4.0)