Circular Vector custom collection

Hi @BalmungSan

thank you for the tip, that is definitely a possibility as soon as the library accepts Scala 3. I see something is moving in that direction: Compile with Scala 3 by povder · Pull Request #166 · scala/scala-collection-contrib · GitHub

On a different level, I am noticing that all the methods I wrote for Vector can be generalized for Seq. So I have already tested a more general draft version RingSeq. And now the dumb question: which is in this case the correct way to work with bounds and higher kinds?

I have gone this way and it seems to work:

trait RingSeq:
  
  /* for improved readability, a Vector index */
  type Index = Int

  /* and a RingVector index, any value is valid */
  type IndexO = Int

  extension[A, B <: Seq[A]](ring: B)
  
    private def index(i: IndexO): Index =
      java.lang.Math.floorMod(i, ring.size)

    def applyO(i: IndexO): A =
      ring(index(i))

    def rotateRight(step: Int): B =
      if ring.isEmpty then ring
      else
        val j: Index = ring.size - index(step)
        (ring.drop(j) ++ ring.take(j)).asInstanceOf[B]

   ...

but as soon as I wrote the first asInstanceOf[B] I felt horribly guilty as a student who has not done the proper homework.

Can someone point me in the right direction?

Thank you in advance,

Mario

1 Like