Hey, I am trying to write sequence chain with list...
# announcements
p
Hey, I am trying to write sequence chain with list of Strings as input, 2 predicates are given and as output I'd like to recieve Map (or Pair?) of list of strings that fullfil predicates (some strings may return true for both). Do you have any ideas? I got requirement working but wondring if this is possible to achieve with one chain
p
You could group by the pair of predicate results, which would result in partitioning the list into four sets:
Copy code
val containsO = { it: String -> it.contains("o") }
val containsE = { it: String -> it.contains("e") }
val list = listOf("hello", "world", "cheese", "pancake")
val map = list.groupBy { containsO(it) to containsE(it) }
val containingOandE = map[true to true].orEmpty()
val containingOnlyO = map[true to false].orEmpty()
val containingOnlyE = map[false to true].orEmpty()
val containingNeither = map[false to false].orEmpty()
val containingO = containingOnlyO + containingOandE
val containingE = containingOnlyE + containingOandE