Skip to content

Commit

Permalink
Deleted method rotateAt and added applyO
Browse files Browse the repository at this point in the history
  • Loading branch information
mcallisto committed May 25, 2024
1 parent 3d198ba commit 3577f3c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
25 changes: 12 additions & 13 deletions src/main/scala/scala/collection/decorators/SeqDecorator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ class SeqDecorator[C, S <: IsSeq[C]](coll: C)(implicit val seq: S) {
private def index(i: IndexO): Index =
java.lang.Math.floorMod(i, seq(coll).size)

/** Gets the element at some circular index.
*
* @param i circular index
* @return the element of the sequence at the given index
* @throws java.lang.ArithmeticException if the sequence is empty
* @example {{{
* Seq(0, 1, 2).applyO(3) => 0
* }}}
*/
def applyO(i: IndexO): this.seq.A =
seq(coll).apply(index(i))

/** Considers the sequence circular and rotates it right by `step` places.
*
* @param step the number of places to be rotated to the right
Expand Down Expand Up @@ -100,17 +112,4 @@ class SeqDecorator[C, S <: IsSeq[C]](coll: C)(implicit val seq: S) {
def rotateLeft[B >: seq.A, That](step: Int)(implicit bf: BuildFrom[C, B, That]): That =
rotateRight(-step)

/** Considers the sequence circular and rotates it to start with the element at `i` circular index.
*
* @param i the circular index of the element to be rotated at the start of the new collection
* @tparam B the element type of the returned collection
* @return a new collection consisting of all elements of this collection
* circularly rotated so to start with the element at circular index `i`.
* @example {{{
* List(1, 2, 3, 4, 5).startAt(2) => List(3, 4, 5, 1, 2)
* }}}
*/
def startAt[B >: seq.A, That](i: IndexO)(implicit bf: BuildFrom[C, B, That]): That =
rotateLeft(i)

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scala.collection
package decorators

import org.junit.Assert.assertEquals
import org.junit.Assert.{assertEquals, assertSame, assertThrows}
import org.junit.Test
import scala.collection.immutable._

Expand Down Expand Up @@ -46,6 +46,13 @@ class SeqDecoratorTest {
assertEquals(s.replaced(4, 4), s)
}

@Test def testApplyO(): Unit = {
val s = Seq(0, 1, 2)
assertEquals(s.applyO(3), 0)
val empty = Vector.empty[Int]
assertThrows(classOf[java.lang.ArithmeticException], () => empty.applyO(1))
}

@Test def testRotatedRight(): Unit = {
val s = Seq(1, 2, 3, 2, 1)
val sRotated = Seq(1, 1, 2, 3, 2)
Expand All @@ -66,12 +73,4 @@ class SeqDecoratorTest {
assertEquals(s.rotateLeft(-4), sRotated)
}

@Test def testStartedAt(): Unit = {
val s = Seq(1, 2, 3, 2, 1)
val sRotated = Seq(2, 3, 2, 1, 1)
assertEquals(s.startAt(1), sRotated)
assertEquals(s.startAt(6), sRotated)
assertEquals(s.startAt(-4), sRotated)
}

}

0 comments on commit 3577f3c

Please sign in to comment.