I have a List[String]
. I want to group it by a given equivalence relation:
def f(x: String, y: String): Boolean = {
if (x == "" || y == "") {
false
} else {
x == y
}
}
I have tried method groupBy
, but I cannot achieve empty strings to be in separate groups.
I would prefer the answer for any equivalence relation f(x: String, y: String): Boolean
, not just this one.
EDIT: I have not specified, but the input type is really List[String]
, not List[(String, String)]
, f
is a binary relation
, that's why it has 2 String inputs, and expected return type is List[List[String]]
EDIT: As @andrey-tyukin mentioned, f
is not an equivalence relation, so requesting "answer for any equivalence relation" is nonsense.
EDIT: An example:
Input: List("a", "a", "b", "", "")
Output: List(List("a", "a"), List("b"), List(""), List(""))
- "a" == "a", that's why they go in the same group
- Although "" == "", but
f
would result in false, so they are not in the same group