Daniel
11/27/2018, 4:59 PMlistOf(1, 2, 3) -> (1, 2); (1,3); (2,1); (2,3); (3,1); (3,2)
Ruckus
11/27/2018, 5:05 PM(1, 1)
, (2, 2)
, or (3, 3)
as combinations? Why did you count (1, 2)
and (2, 1)
as separate combinations?Ruckus
11/27/2018, 5:05 PMRuckus
11/27/2018, 5:07 PMDaniel
11/27/2018, 5:43 PMDias
11/27/2018, 5:46 PMDias
11/27/2018, 5:47 PMDias
11/27/2018, 5:49 PMfred.deschenes
11/27/2018, 5:52 PMraulraja
11/27/2018, 5:56 PMobject AllCombinations {
@JvmStatic
fun main(args: Array<String>) {
val list = listOf(1, 2, 3)
val allCombinations: List<Pair<Int, Int>> =
list.flatMap { a ->
list.map { b ->
a to b
}
}
println(allCombinations)
//[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
}
}
raulraja
11/27/2018, 5:56 PMflatMap
and map
over a List
Ruckus
11/27/2018, 5:58 PMraulraja
11/27/2018, 6:01 PMimport arrow.instances.list.monad.binding
val allCombinations2: List<Pair<Int, Int>> =
binding {
val el1 = list.k().bind()
val el2 = list.k().bind()
el1 to el2
}
Dmitry Kandalov
11/27/2018, 6:05 PMpermutations
and combinations
in stdlib, would be nice to have them in Kotlin.Daniel
11/27/2018, 6:39 PMraulraja
11/27/2018, 6:43 PMRuckus
11/27/2018, 6:50 PMaarjav
11/28/2018, 2:41 AM