Skip to content

Kotlin/collection functional api - search, sort, aggregate type api#20

Merged
sh1mj1 merged 26 commits intodevfrom
kotlin/collection-functional-api
May 2, 2025
Merged

Kotlin/collection functional api - search, sort, aggregate type api#20
sh1mj1 merged 26 commits intodevfrom
kotlin/collection-functional-api

Conversation

@sh1mj1
Copy link
Owner

@sh1mj1 sh1mj1 commented May 2, 2025

🎉 Kotlin 컬렉션 함수형 API 학습 테스트 (Aggregation, Sorting, Searching) 추가

💡 함수형 API 학습 목표
Kotlin 컬렉션 함수형 API의 다양한 활용법을 배우고 숙달하여, 보다 간결하고 가독성 높은 코드를 작성하는 능력 향상
컬렉션 데이터를 효과적으로 집계, 정렬, 탐색하는 패턴 이해 및 적용
불변성 유지, 부수 효과 최소화 등 함수형 프로그래밍의 원칙을 실제 코드에 반영

✨ 작업 내용 요약
Kotlin 컬렉션의 데이터를 집계(Aggregation), 정렬(Sorting), 탐색(Searching)하는 함수들의 사용법을 학습하기 위한 테스트 코드를 추가했습니다. 각 함수가 어떤 상황에서 유용하게 사용될 수 있는지 다양한 케이스를 통해 검증합니다.

주요 컬렉션 대상: List, Map

파일별 작업 요약:

✅ AggregationTest.kt

  • count, sum, sumOf, average 등 기본적인 집계 함수 테스트
  • max, min, maxOf, minOf maxOrNull, minOrNull 등 최대/최소값 탐색 함수 테스트 보강
  • reduce, fold, runningReduce, runningFold 와 같은 누적 연산 함수 테스트

✅ SortingTest.kt

  • sorted, sortedDescending 등 기본 정렬 함수 테스트
  • sortedBy, sortedByDescending 등 특정 속성 기준 정렬 함수 테스트
  • sortedWith 와 다양한 Comparator (compareBy, thenBy 등)를 활용한 복합 정렬 테스트 추가
  • Map 타입의 정렬 (toList().sorted... 패턴 활용) 테스트

✅ SearchingTest.kt

  • first, find, last, findLast 등 요소 탐색 함수 테스트
  • all, any, none 등 조건 만족 여부 확인 함수 테스트
  • indexOf, lastIndexOf, indexOfFirst, indexOfLast 등 인덱스 탐색 함수 테스트 및 takeIf를 활용하여 -1 대신 null 처리하는 패턴 학습
  • firstNotNullOf, firstNotNullOfOrNull 등 null이 아닌 첫 값 탐색 함수 테스트

sh1mj1 added 25 commits April 15, 2025 15:56
map, flatMap, associate methods
associateTo, associateWithTo, associateByTo
remove flattening test in MappingTest.kt
first, find, firstOrNull, last, findLast, lastOrNull, indexOf, all, any, none method
lastIndexOf, indexOfFirst, indexOfLast
sorted, sortedDescending, sortedBy, sortedByDescending, reversed
compareBy, thenBy, thenByDescending, naturalOrder, reverseOrder 추가
count, sum, sumOf, average, max, maxOfOrNull, reduce, fold, runningReduce, runningFold
@sh1mj1 sh1mj1 requested a review from Copilot May 2, 2025 12:02
@sh1mj1 sh1mj1 changed the title Kotlin/collection functional api - map, filter, flatten type api Kotlin/collection functional api - search, sort, aggregate type api May 2, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a comprehensive suite of tests to demonstrate and validate the Kotlin collection functional API for aggregation, sorting, searching, mapping, flattening, and filtering.

  • Introduces new test cases covering various Kotlin collection operations and their edge cases.
  • Provides practical examples to reinforce functional programming principles using immutable collections and accumulator functions.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
SortingTest.kt Added test cases for list sorting using various comparator strategies including stable sorts.
SearchingTest.kt Introduced tests for list searching and index-based operations with proper null handling.
MappingTest.kt Provided tests for transforming collections and associating list elements with keys/values.
FlatteningTest.kt Added tests demonstrating the flattening of nested lists and maps.
FilteringTest.kt Introduced various filtering tests for lists and maps using different predicate strategies.
AggregationTest.kt Added tests for aggregation functions such as count, sum, reduce, fold, runningReduce, and runningFold.

val words: List<String> = listOf("a", "b", "c")

val runningWordsFolded: List<String> = words.runningFold("Start: ") { acc, word -> acc + word }
runningWordsFolded shouldBe listOf("Start: a", "Start: ab", "Start: abc")
Copy link

Copilot AI May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runningFold includes the initial value in its result; update the expected output to include the initial value, for example: listOf("Start: ", "Start: a", "Start: ab", "Start: abc").

Suggested change
runningWordsFolded shouldBe listOf("Start: a", "Start: ab", "Start: abc")
runningWordsFolded shouldBe listOf("Start: ", "Start: a", "Start: ab", "Start: abc")

Copilot uses AI. Check for mistakes.
emptyList<User>().maxOfOrNull(User::age) shouldBe null
}

"maxOfOrNull - 특정 속성 기준으로 컬렉션에서 가장 작은 요소 (없으면 null)" {
Copy link

Copilot AI May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test description mentions maxOfOrNull but the test actually uses minOfOrNull; update the description to 'minOfOrNull - 특정 속성 기준으로 컬렉션에서 가장 작은 요소 (없으면 null)'.

Suggested change
"maxOfOrNull - 특정 속성 기준으로 컬렉션에서 가장 작은 요소 (없으면 null)" {
"minOfOrNull - 특정 속성 기준으로 컬렉션에서 가장 작은 요소 (없으면 null)" {

Copilot uses AI. Check for mistakes.
@sh1mj1 sh1mj1 merged commit 15c1cec into dev May 2, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants