LoginSignup
0
0

More than 3 years have passed since last update.

Kotlin: 配列の直積を返すSequenceを作ってみた

Last updated at Posted at 2020-11-14

JavaScript: 配列の直積を返すジェネレータを作ってみた」に倣い、
順列組み合わせの次には直積をやってみる。
こちらは元記事とは異なるアルゴリズム。


/** 直積(可変長引数版) */
fun <T> product(vararg optionsArray: List<T>): Sequence<List<T>> {
    if (optionsArray.isEmpty()) {
        return emptySequence()
    }

    return productInternal(optionsArray.asList())
}

/** 直積(リスト版) */
fun <T> List<List<T>>.product(): Sequence<List<T>> {
    if (isEmpty()) {
        return emptySequence()
    }

    return productInternal(this)
}

private fun <T> productInternal(optionsList: List<List<T>>): Sequence<List<T>> {
    if (optionsList.isEmpty()) {
        return sequenceOf(emptyList())
    }

    return sequence {
        optionsList.first().forEach { first ->
            productInternal(optionsList.drop(1)).forEach {
                yield(listOf(first) + it)
            }
        }
    }
}

使用例:

fun main() {
    product(listOf(0, 1), listOf(2, 3), listOf(4, 5))
        .also { println(it.toList()) }
    // > [[0, 2, 4], [0, 2, 5], [0, 3, 4], [0, 3, 5], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5]]
}

/以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0