all possible combinations of two lists


wei

Given I have two lists:

val ints = listOf(0, 1, 2)
val strings = listOf("a", "b", "c")

I want all possible combinations of their elements

0a, 1a, 2a, 0betc

Is there a more elegant way than:

ints.forEach { int -> 

    strings.forEach { string ->  


        println("$int $string")

    }

}
Dinech

You can write the following extension functions based on flatMapstdlib functions:

// Extensions
fun <T, S> Collection<T>.cartesianProduct(other: Iterable<S>): List<Pair<T, S>> {
    return cartesianProduct(other, { first, second -> first to second })
}

fun <T, S, V> Collection<T>.cartesianProduct(other: Iterable<S>, transformer: (first: T, second: S) -> V): List<V> {
    return this.flatMap { first -> other.map { second -> transformer.invoke(first, second) } }
}

// Example
fun main(args: Array<String>) {
    val ints = listOf(0, 1, 2)
    val strings = listOf("a", "b", "c")

    // So you could use extension with creating custom transformer
    strings.cartesianProduct(ints) { string, int ->
        "$int $string"
    }.forEach(::println)

    // Or use more generic one
    strings.cartesianProduct(ints)
            .map { (string, int) ->
                "$int $string"
            }
            .forEach(::println)
}

Related


all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0betc Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0band many more Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0band many more Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

Find all possible combinations of two lists

Mike V Given two lists of arbitrary length: list1 = ['a', 'b', 'c'] list2 = ['1', '2', '3'] list1is a list of objects, and list2is the possible value for each object. How to find all possible combinations of these two lists (I'm not sure if this is the correc

Find all possible combinations of two lists

Mike V Given two lists of arbitrary length: list1 = ['a', 'b', 'c'] list2 = ['1', '2', '3'] list1is a list of objects, and list2is the possible value for each object. How to find all possible combinations of these two lists (I'm not sure if this is the correc

Find all possible combinations of two lists

Mike V Given two lists of arbitrary length: list1 = ['a', 'b', 'c'] list2 = ['1', '2', '3'] list1is a list of objects, and list2is the possible value for each object. How to find all possible combinations of these two lists (I'm not sure if this is the correc

Find all possible combinations of two lists

Mike V Given two lists of arbitrary length: list1 = ['a', 'b', 'c'] list2 = ['1', '2', '3'] list1is a list of objects, and list2is the possible value for each object. How to find all possible combinations of these two lists (I'm not sure if this is the correc

Find all possible combinations of two lists

Mike V Given two lists of arbitrary length: list1 = ['a', 'b', 'c'] list2 = ['1', '2', '3'] list1is a list of objects, and list2is the possible value for each object. How to find all possible combinations of these two lists (I'm not sure if this is the correc

How can I create all possible combinations of these two lists?

octopus I want to create a list of all combinations from these two lists, where each combination is also a list. E.g Given two lists: [1,2,3]and[True, False] combination: [(1, False), (2, False), (3, False)] [(1, False), (2, False), (3, True )] [(1, False), (2

How to get all possible combinations of two different lists?

mimosa I'm having a lot of trouble understanding my problem: I have 2 lists: from = ['A', 'B', 'C'] to = ['D', 'E', 'F'] I need to produce a matrix combining each list from one list to another, for example: final = [[['A', 'D'], ['B', 'E'], ['C', 'F']],

make all possible combinations of lists

Dean: I need to be able to create a list containing all possible combinations of input lists. For example, a list [1,2,3]should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]the list does not have to be in any particular order. On this site I found many functions th

all possible combinations of vector lists

citizen I have a list of vectors and I want to get a list of all possible combinations between the elements of each vector, i.e. combinations of n elements (from a vector) taken two or more at a time. For example, I have the following list: > DF $`1` A B

make all possible combinations of lists

Dean: I need to be able to create a list containing all possible combinations of input lists. For example, a list [1,2,3]should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]the list does not have to be in any particular order. On this site I found many functions th

make all possible combinations of lists

Dean: I need to be able to create a list containing all possible combinations of input lists. For example, a list [1,2,3]should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]the list does not have to be in any particular order. On this site I found many functions th

all possible combinations of vector lists

citizen I have a list of vectors and I want to get a list of all possible combinations between the elements of each vector, i.e. combinations of n elements (from a vector) taken two or more at a time. For example, I have the following list: > DF $`1` A B