0

I'm looking for a "chunkBy" kind of operator that provides the below functionality:

test("chunkBy") {
  val s = Stream(1, 2, 3, 4, 5, 6, 7, 8).covary[IO]
  val range = 4
  val actual: List[(Int, Chunk[Int])] = s.chunkBy(element => element % range).toList

  val expected = List((1, Chunk(1,5)), (2, Chunk(2,6)), (3, Chunk(3,7)), (4, Chunk(4,8)))
  assertEquals(actual, expected)
}

note: chunkBy is the operator I'm looking for.

I couldn't find any such API provided by FS2 to create chunks out of a stream based on predicates.

Jeet Banerjee
  • 194
  • 2
  • 2
  • 12
  • 1
    Such operator doesn't make sense for `Stream` because you are assuming the data is finite and can be folded in memory, at that point is better to just `compile` the `Stream` into a `List` and use `groupBy` in it. – Luis Miguel Mejía Suárez Mar 31 '23 at 13:35
  • While I agree with @LuisMiguelMejíaSuárez for your example, note that Java has a groupingBy collector for streams and [this post](https://stackoverflow.com/questions/28609848/alternative-to-collectors-groupingby-for-scala) might give an answer to your question. I think there can be other grouping-examples where groupingBy can also be meaningful for infinite streams: when you know that grouped items will be only finitely far apart. – Sebastian Apr 01 '23 at 16:52
  • There seems to be yet another related question [here](https://stackoverflow.com/questions/51003257/how-to-group-objects-using-a-classifier-function-in-fs2?rq=2) – Sebastian Apr 01 '23 at 17:05
  • 2
    @Sebastian **Java** `Streams` are not really intended for infinite data, they are just a poor man's way of adding functional combinators to the **Java** stdlib. - Anyways, a simple way to get what OP wants in this case would be to `compile.foldMonoid` using the `Monoid` for `Map[K, List[V]]`, but again, that implies loading all the data in memory. – Luis Miguel Mejía Suárez Apr 01 '23 at 19:07
  • @LuisMiguelMejíaSuárez well, I agree that in Java it's a poor man's way implementation, but still Java Streams explicitly allow infinite data and try to lazily evaluate. But you're right, functions like `groupBy` or even worse `sorted` have finite data in mind. But as I said, `groupBy` can make sense also for infinite data, depending on the grouping criteria. Anyway, I guess your suggestion for this case makes perfect sense. – Sebastian Apr 01 '23 at 19:37
  • Does this answer your question? [GroupBy in fs2?](https://stackoverflow.com/questions/75250579/groupby-in-fs2) – Daenyth May 02 '23 at 19:10
  • @Daenyth I followed the `broadcastThrough` multiple pipes + `filter` approach as mentioned in [this](https://stackoverflow.com/a/71603380/9476859) answer and it worked perfectly for my use-case. – Jeet Banerjee May 03 '23 at 20:05

0 Answers0