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