title: Grouping same neighbours with Kotlin Collections Operations
body: <p>Which operation from <a href="https://kotlinlang.org/docs/reference/collection-operations.html" rel="nofollow noreferrer">Kotlin Collections</a> would be the most suitable for grouping the same consecutive neighbours in a list?
  Let's say we have a list:</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>['A', 'A', 'B', 'B', 'B', 'A', 'B']
  </code></pre>
  
  <p>and I'd like to obtain a list of groups splitted when an item changes (preserving ordering):</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>[['A', 'A'], ['B', 'B', 'B'], ['A'], ['B']]
  </code></pre>
  
  <p>It seems to be a common case, so I thought that it must be supported by rich Kotlin Collections operations, but I didn't find anything appropriate there.</p>
  
  <p>I've already tried using <code>reduce</code>, <code>fold</code> and it all seems not to handle that case at all. 
  I think that using <code>groupBy</code> is pretty close to desired result:</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>val chars: List&lt;Char&gt; = "AABBBAB".toList()
  val groups = chars.groupBy { char -&gt; char }
      .map { (key, grouped) -&gt;
           grouped
      }
  </code></pre>
  
  <p>It works almost well but it joins same group types together: <code>[['A', 'A', 'A'], ['B', 'B', 'B', 'B']]</code>)</p>
  
  <p>I know how to do that using some ugly loops and buffers, but my question is how to achieve that using some fancy Kotlin operations (or combination of them)?</p>
  
  <p>My real case is slightly more complex (I'd like to group some objects by its property in a way described above).</p>