본 강좌에서는 클로저에 대해 알아보도록 하겠습니다. 클로저란 내부 함수와 내부 함수에 영향을 미치는 주변환경을 모두 포함한 객체로정의할 수 있습니다. 또한 클로저는 func와 함수이름이 생략된 경량의 함수객체로 클로저 표현을 통해 문장릉 짧고 간단하게 만들 수 있습니다.
Youtube
- https://youtu.be/0bIRlDCH8kU
본 강좌에서는 클로저에 대해 알아보도록 하겠습니다. 클로저란 내부 함수와 내부 함수에 영향을 미치는 주변환경을 모두 포함한 객체로정의할 수 있습니다. 또한 클로저는 func와 함수이름이 생략된 경량의 함수객체로 클로저 표현을 통해 문장릉 짧고 간단하게 만들 수 있습니다.
Youtube
- https://youtu.be/0bIRlDCH8kU
LG전자 SEED2016 발표
개미수열 문제를 풀어보면서 다양한 프로그래밍 개념들을 적용시켜봅니다. (Java/JavaScript/Go/C/Scala/Haskell 코드가 조금씩 등장)
- regex
- list processing
- iterator/generator
- coroutine/continuation
- lazy list
LG전자 SEED2016 발표
개미수열 문제를 풀어보면서 다양한 프로그래밍 개념들을 적용시켜봅니다. (Java/JavaScript/Go/C/Scala/Haskell 코드가 조금씩 등장)
- regex
- list processing
- iterator/generator
- coroutine/continuation
- lazy list
DroidKnight 2018 State machine by Selaed classMyeongin Woo
The document discusses view state management in Android. It explains that as views become more complex, the number of states increases, making state management difficult. It introduces sealed classes in Kotlin as a way to better organize view states and prevent issues like crashes from invalid states. Adding new states easily without errors is discussed as an advantage of using sealed classes.
The document discusses how a company called RejinsComics uses Kotlin in their projects. It provides pros and cons of using Kotlin compared to Java, as well as tips for adopting Kotlin. Currently, over half of new code at the company is written in Kotlin, and they aim to convert existing Java code to Kotlin when refactoring or if there is time. The document emphasizes benefits like Java interoperability, higher-order functions, and improved readability and productivity in Kotlin.
The document discusses Kotlin collections and aggregate operations on collections. It explains that Kotlin collections can be mutable or immutable, and by default collections are immutable unless specified as mutable. It then covers various aggregate operations that can be performed on collections like any, all, count, fold, foldRight, forEach, max, min, none etc and provides code examples for each operation.
The document summarizes various functions in the Kotlin standard library including let, with, apply, run, also, takeIf, takeUnless, use, and repeat. It provides code examples to demonstrate how each function works and what it is commonly used for. let, with, apply, and also allow passing a receiver object to a lambda and accessing it as "it". takeIf and takeUnless return the receiver if a predicate is true or false, respectively. use automatically closes resources. repeat runs an action a specified number of times.
The document provides examples of Kotlin classes and properties compared to Java classes. Some key points summarized:
1. Kotlin classes can define properties directly rather than private fields with getters/setters like in Java. Properties are accessed directly rather than through getter methods.
2. Data classes provide equals(), hashCode(), toString() by default and allow destructuring declarations. Regular classes do not have these features by default.
3. Default values can be defined for properties in Kotlin classes, unlike Java where default values require a builder pattern.
9. Expression(if-else)
• Java와 다르게 Kotlin 에서는 if, else도 표현식입니다. 따라
서 if else는 항상 반환값이 있습니다.
• {} 블럭으로 있을 경우에는 마지막 줄의 값을 반환합니다.
• if, else을 표현식으로 사용 할경우에는 if가 있으면 else도
항상 있어야 합니다.
11. Expression(when)
/* Java */
int x = 1;
switch (x) {
case 1: {
System.out.println("1");
break;
}
case 2: {
System.out.println("2");
break;
}
default: {
System.out.println("default");
break;
}
}
12. Expression(when)
val x = 1
when {
x == 1 -> print("x == 1")
x == 2 -> print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")
}
}
13. Expression(when)
val x = 1
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")
}
}
14. Expression(when)
val x = 1
when (x) {
0, 1 -> print("x == 0 or x == 1")
else -> print("otherwise")
}
15. Expression(when)
val s = "1"
val x = 1
when (x) {
parseInt(s) -> print("s encodes x")
else -> print("s does not encode x")
}
}
fun parseInt(value : String) : Int {
return 1
}
16. Expression(when)
val x = 129
val validNumbers = 100..120
when (x) {
in 1..10 -> print("x is in the range")
in validNumbers -> print("x is valid")
!in 10..20 -> print("x is outside the range")
else -> print("none of the above")
}
17. Expression(when)
/* Java */
public void test(Object obj) {
if (obj instanceof String) {
System.out.println(((String)obj)
.startsWith(“preFix"));
} else if (obj instanceof Integer) {
Integer.valueOf(String.valueOf(obj));
} else if (obj instanceof JavaPerson) {
((JavaPerson) obj).getName();
}
}
18. Expression(when)
/* Java */
public void test(Object obj) {
if (obj instanceof String) {
System.out.println(((String)obj)
.startsWith(“preFix"));
} else if (obj instanceof Integer) {
Integer.valueOf(String.valueOf(obj));
} else if (obj instanceof JavaPerson) {
((JavaPerson) obj).getName();
}
}
19. Expression(when)
/* Kotlin */
fun test(x: Any) : Any = when(x) {
is String -> x.startsWith("prefix")
is Int -> x + 3
is Person -> x.name
else -> false
}
20. Expression(when)
/* Kotlin */
fun test(x: Any) : Any = when(x) {
is String -> x.startsWith("prefix")
is Int -> x + 3
is Person -> x.name
else -> false
}
smartCasting
23. Expression(when)
• Java의 switch case와 비슷합니다. 하지만 표현식으로 활용
할수 있어 값을 할당 할 수 있습니다.
• Kotlin의 is는 Java의 instanceOf 와 비슷합니다.
• {} 블럭으로 있을 경우에는 마지막 라인의 값을 반환합니다
.
24. Expression(when)
• Java의 switch case와 비슷합니다. 하지만 표현식으로 활용
할수 있어 값을 할당 할 수 있습니다.
• Kotlin의 is는 Java의 instanceOf 와 비슷합니다.
• {} 블럭으로 있을 경우에는 마지막 라인의 값을 반환합니다
.
• when을 표현식으로 사용 할경우에는 else도 항상 있어야
합니다.(enum제외)
25. Expression(when)
• Java의 switch case와 비슷합니다. 하지만 표현식으로 활용
할수 있어 값을 할당 할 수 있습니다.
• Kotlin의 is는 Java의 instanceOf 와 비슷합니다.
• {} 블럭으로 있을 경우에는 마지막 라인의 값을 반환합니다
.
• when을 표현식으로 사용 할경우에는 else도 항상 있어야
합니다.(enum제외)
• when으로 type을 분기 하면 별도로 casting을 하지 않아도
됩니다.
26. 실습
• when을 이용해 String value를 입력받아 아래의 조건들로
분리
• 1. test 로 시작하는지 (startsWith)
• 2. length 가 3보다 작은지
30. Expression(try-catch)
• Java의 try catch 와 비슷합니다. 하지만 표현식으로 활용
할수 있어 값을 할당 할 수 있습니다.
• 정상적인 케이스에는 try 값이 exception이 발생하면 catch
값이 할당 됩니다.
31. Expression(try-catch)
• Java의 try catch 와 비슷합니다. 하지만 표현식으로 활용
할수 있어 값을 할당 할 수 있습니다.
• 정상적인 케이스에는 try 값이 exception이 발생하면 catch
값이 할당 됩니다.
• try, catch 는 {}의 마지막 줄의 값을 반환합니다.
32. Expression(try-catch)
• Java의 try catch 와 비슷합니다. 하지만 표현식으로 활용
할수 있어 값을 할당 할 수 있습니다.
• 정상적인 케이스에는 try 값이 exception이 발생하면 catch
값이 할당 됩니다.
• try, catch 는 {}의 마지막 줄의 값을 반환합니다.
• Java와 마찬가지로 finally 도 제공하지만 optional 입니다.