Skip to content

Instantly share code, notes, and snippets.

@hixguru
Last active January 19, 2018 06:53
Show Gist options
  • Save hixguru/4e25f3e6b9ba7a35bac49caea563635c to your computer and use it in GitHub Desktop.
Save hixguru/4e25f3e6b9ba7a35bac49caea563635c to your computer and use it in GitHub Desktop.
사이즈가 동일한 List의 요소들을 하나씩 순차적으로 merge하여 하나의 List를 반환한다.
/**
* 목적 : 각각의 리스트의 요소들을 순차적으로 하나씩 merge하여 하나의 리스트를 반환받고 싶다.
* 상황 : 각각의 question sound를 만들 때 (number + questions script + mute) 순으로 재생시키고 싶다.
*/
// 시도했던 방법
fun <T> List<T>.mergeSequentiallyWith(vararg others: List<T>): List<T> {
val list = arrayListOf<T>()
others
.firstOrNull { it.size != size }
?.let { error("All Lists must be the same size") }
this.forEachIndexed { index, t ->
list.add(t)
others.forEach { list.add(it[index]) }
}
return list
}
// 페어로 보면서 수정한 방법
fun getQuestionSoundsWithMute() {
questions
.zip(number) { question, number -> mutableListOf(number, question) }
.zip(muteBetweenQuestions) { question, mute -> question.apply { add(mute) } }
.flatten()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment