Skip to content

Commit

Permalink
Add asSequence method
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-rusu committed Mar 30, 2024
1 parent 9291513 commit 6bcaef5
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import java.io.File

private const val NUM_COMPONENT_N_FUNCTIONS = 5

private const val EMPTY_SINGLETONS_PACKAGE_NAME = "emptySingletons"
private val packageName = ImmutableArrayCodeGenerator::class.java.`package`.name
private val emptySingletonsPackageName = "$packageName.emptySingletons"

Expand Down Expand Up @@ -56,6 +55,7 @@ private fun generateImmutableArrayFile(baseType: BaseType): FileSpec {
addFirst(baseType)
addLast(baseType)
addIteratorOperator(baseType)
addAsSequence(baseType)
addForEach(baseType)
addForEachIndexed(baseType)
addCompanionObject {
Expand Down Expand Up @@ -195,7 +195,7 @@ private fun TypeSpec.Builder.addIteratorOperator(baseType: BaseType) {
val iteratorType = Iterator::class.asClassName().parameterizedBy(baseType.type)

addFunction(
kdoc = "Creates an iterator allowing iteration over the elements of the array.",
kdoc = "Returns an iterator allowing iteration over the elements of the array.",
modifiers = listOf(KModifier.OPERATOR),
name = "iterator",
returns = iteratorType,
Expand All @@ -210,6 +210,19 @@ private fun TypeSpec.Builder.addIteratorOperator(baseType: BaseType) {
}
}

private fun TypeSpec.Builder.addAsSequence(baseType: BaseType) {
addFunction(
kdoc = "Returns a [Sequence] which returns the elements of this array when iterated.",
name = "asSequence",
returns = Sequence::class.asClassName().parameterizedBy(baseType.type),
code = """
if (isEmpty()) return emptySequence()
return Sequence { iterator() }
""".trimIndent()
)
}

private fun TypeSpec.Builder.addForEach(baseType: BaseType) {
addFunction(
kdoc = "Performs the specified [action] on each element sequentially starting with the first element",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence

@JvmInline
public value class ImmutableArray<T> @PublishedApi internal constructor(
Expand Down Expand Up @@ -89,14 +90,23 @@ public value class ImmutableArray<T> @PublishedApi internal constructor(
}

/**
* Creates an iterator allowing iteration over the elements of the array.
* Returns an iterator allowing iteration over the elements of the array.
*/
@Suppress("UNCHECKED_CAST")
public operator fun iterator(): Iterator<T> {
if (isEmpty()) return EmptyIterator
return values.iterator() as Iterator<T>
}

/**
* Returns a [Sequence] which returns the elements of this array when iterated.
*/
public fun asSequence(): Sequence<T> {
if (isEmpty()) return emptySequence()

return Sequence { iterator() }
}

/**
* Performs the specified [action] on each element sequentially starting with the first element
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence

@JvmInline
public value class ImmutableBooleanArray @PublishedApi internal constructor(
Expand Down Expand Up @@ -86,13 +87,22 @@ public value class ImmutableBooleanArray @PublishedApi internal constructor(
}

/**
* Creates an iterator allowing iteration over the elements of the array.
* Returns an iterator allowing iteration over the elements of the array.
*/
public operator fun iterator(): Iterator<Boolean> {
if (isEmpty()) return EmptyIterator
return values.iterator()
}

/**
* Returns a [Sequence] which returns the elements of this array when iterated.
*/
public fun asSequence(): Sequence<Boolean> {
if (isEmpty()) return emptySequence()

return Sequence { iterator() }
}

/**
* Performs the specified [action] on each element sequentially starting with the first element
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence

@JvmInline
public value class ImmutableByteArray @PublishedApi internal constructor(
Expand Down Expand Up @@ -87,13 +88,22 @@ public value class ImmutableByteArray @PublishedApi internal constructor(
}

/**
* Creates an iterator allowing iteration over the elements of the array.
* Returns an iterator allowing iteration over the elements of the array.
*/
public operator fun iterator(): Iterator<Byte> {
if (isEmpty()) return EmptyIterator
return values.iterator()
}

/**
* Returns a [Sequence] which returns the elements of this array when iterated.
*/
public fun asSequence(): Sequence<Byte> {
if (isEmpty()) return emptySequence()

return Sequence { iterator() }
}

/**
* Performs the specified [action] on each element sequentially starting with the first element
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence

@JvmInline
public value class ImmutableCharArray @PublishedApi internal constructor(
Expand Down Expand Up @@ -87,13 +88,22 @@ public value class ImmutableCharArray @PublishedApi internal constructor(
}

/**
* Creates an iterator allowing iteration over the elements of the array.
* Returns an iterator allowing iteration over the elements of the array.
*/
public operator fun iterator(): Iterator<Char> {
if (isEmpty()) return EmptyIterator
return values.iterator()
}

/**
* Returns a [Sequence] which returns the elements of this array when iterated.
*/
public fun asSequence(): Sequence<Char> {
if (isEmpty()) return emptySequence()

return Sequence { iterator() }
}

/**
* Performs the specified [action] on each element sequentially starting with the first element
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence

@JvmInline
public value class ImmutableDoubleArray @PublishedApi internal constructor(
Expand Down Expand Up @@ -87,13 +88,22 @@ public value class ImmutableDoubleArray @PublishedApi internal constructor(
}

/**
* Creates an iterator allowing iteration over the elements of the array.
* Returns an iterator allowing iteration over the elements of the array.
*/
public operator fun iterator(): Iterator<Double> {
if (isEmpty()) return EmptyIterator
return values.iterator()
}

/**
* Returns a [Sequence] which returns the elements of this array when iterated.
*/
public fun asSequence(): Sequence<Double> {
if (isEmpty()) return emptySequence()

return Sequence { iterator() }
}

/**
* Performs the specified [action] on each element sequentially starting with the first element
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence

@JvmInline
public value class ImmutableFloatArray @PublishedApi internal constructor(
Expand Down Expand Up @@ -87,13 +88,22 @@ public value class ImmutableFloatArray @PublishedApi internal constructor(
}

/**
* Creates an iterator allowing iteration over the elements of the array.
* Returns an iterator allowing iteration over the elements of the array.
*/
public operator fun iterator(): Iterator<Float> {
if (isEmpty()) return EmptyIterator
return values.iterator()
}

/**
* Returns a [Sequence] which returns the elements of this array when iterated.
*/
public fun asSequence(): Sequence<Float> {
if (isEmpty()) return emptySequence()

return Sequence { iterator() }
}

/**
* Performs the specified [action] on each element sequentially starting with the first element
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence

@JvmInline
public value class ImmutableIntArray @PublishedApi internal constructor(
Expand Down Expand Up @@ -86,13 +87,22 @@ public value class ImmutableIntArray @PublishedApi internal constructor(
}

/**
* Creates an iterator allowing iteration over the elements of the array.
* Returns an iterator allowing iteration over the elements of the array.
*/
public operator fun iterator(): Iterator<Int> {
if (isEmpty()) return EmptyIterator
return values.iterator()
}

/**
* Returns a [Sequence] which returns the elements of this array when iterated.
*/
public fun asSequence(): Sequence<Int> {
if (isEmpty()) return emptySequence()

return Sequence { iterator() }
}

/**
* Performs the specified [action] on each element sequentially starting with the first element
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence

@JvmInline
public value class ImmutableLongArray @PublishedApi internal constructor(
Expand Down Expand Up @@ -87,13 +88,22 @@ public value class ImmutableLongArray @PublishedApi internal constructor(
}

/**
* Creates an iterator allowing iteration over the elements of the array.
* Returns an iterator allowing iteration over the elements of the array.
*/
public operator fun iterator(): Iterator<Long> {
if (isEmpty()) return EmptyIterator
return values.iterator()
}

/**
* Returns a [Sequence] which returns the elements of this array when iterated.
*/
public fun asSequence(): Sequence<Long> {
if (isEmpty()) return emptySequence()

return Sequence { iterator() }
}

/**
* Performs the specified [action] on each element sequentially starting with the first element
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence

@JvmInline
public value class ImmutableShortArray @PublishedApi internal constructor(
Expand Down Expand Up @@ -87,13 +88,22 @@ public value class ImmutableShortArray @PublishedApi internal constructor(
}

/**
* Creates an iterator allowing iteration over the elements of the array.
* Returns an iterator allowing iteration over the elements of the array.
*/
public operator fun iterator(): Iterator<Short> {
if (isEmpty()) return EmptyIterator
return values.iterator()
}

/**
* Returns a [Sequence] which returns the elements of this array when iterated.
*/
public fun asSequence(): Sequence<Short> {
if (isEmpty()) return emptySequence()

return Sequence { iterator() }
}

/**
* Performs the specified [action] on each element sequentially starting with the first element
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ class ImmutableArrayTest {
}
}

@Test
fun `asSequence validation`() {
with(ImmutableArray(3) { "element $it" }) {
val elementsFromSequence = this.asSequence().toList()
expectThat(elementsFromSequence).isEqualTo(listOf("element 0", "element 1", "element 2"))
}
}

@Test
fun `forEach validation`() {
with(ImmutableArray(3) { "element $it" }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ class ImmutableIntArrayTest {
}
}

@Test
fun `asSequence validation`() {
with(ImmutableIntArray(3) { it }) {
val elementsFromSequence = this.asSequence().toList()
expectThat(elementsFromSequence).isEqualTo(listOf(0, 1, 2))
}
}

@Test
fun `forEach validation`() {
with(ImmutableIntArray(3) { it }) {
Expand Down

0 comments on commit 6bcaef5

Please sign in to comment.