Kotlin
-
[코틀린을 다루는 기술] Kotlin에서의 재귀 함수 사용Kotlin 2020. 8. 7. 14:31
이 글은 '길벗'사의 '코틀린을 다루는 기술'을 보고 작성한 글입니다. 더 자세한 내용은 해당 책에서 참고하시면 좋을 것 같습니다. 재귀와 공재귀, 꼬리 호출 만약, 팩토리얼 함수를 구현하다고 해볼까요? fun factorial(n: Int): Long = if(n == 1) n.toLong() else n * factorial(n-1) fun main() { val num = 10 val result: Long = factorial(num) println("Factorial: $num -> $result") } 일반적으로 알고 있는 재귀를 통해 팩토리얼을 구현하면 이런 형태가 될 것입니다. 하지만 현재 코드에서 팩토리얼은 10번 호출되고 이 함수의 문맥을 유지하기 위해 프로그램은 팩토리얼 함수가 가진 ..
-
[코틀린을 다루는 기술] 코틀린 함수 정리Kotlin 2020. 7. 31. 15:13
이 글은 길벗 사의 '코틀린을 다루는 기술' 을 읽으면서 정리한 포스팅입니다. 더 자세한 내용은 '코틀린을 다루는 기술' 책에서 찾아보시면 좋을 것 같습니다. 서론 디프만 8기에서 코틀린 스터디를 진행중입니다. 이번 스터디에서는 '코틀린을 다루는 기술'이라는 책을 토대로 코틀린 사용법과 활용도를 높이고자 하는 목표를 가지고 있습니다. 특히 단순히 항상 쓰던 문법만이 아닌, 더욱 '코틀린스럽게', '코틀린답게' 코딩을 하도록 실력을 키우는 것이 중요하다고 생각합니다. 순수 함수 이 책의 초반에서 가장 강조하는 부분은 안전한 프로그래밍을 하는 것입니다. 안전한 프로그래밍을 하기 위한 여러 방법들이 있는데, 3장 함수에서는 계산 시 순수 함수만 사용하는 것과 계산 결과를 외부 세계에서 사용하려면 순수 효과만 ..
-
[Kotlin] 배열 사용법 정리Kotlin 2020. 2. 20. 12:30
Java의 배열 선언 int[] i_array; int mArray[]; 자바에서는 두가지 방법으로 배열을 선언할 수 있는데요. 타입[ ] 변수; 타입 변수[ ]; 대괄호 [ ]는 배열 변수를 선언하는 기호로 사용되는데 타입 뒤에 붙을 수도 있고 변수 뒤에 붙을 수도 있습니다. 배열을 선언하면서 바로 초기화를 할 수도 있고, 선언만 하고 나중에 초기화를 할 수도 있습니다. int[] i_array = {1,2,3,4,5}; Arrays.fill(i_array,1); for(int i=0; i