Skip to content

Commit

Permalink
Merge pull request #172 from Kotlin/seq-of
Browse files Browse the repository at this point in the history
[Feature] Seq of functions
  • Loading branch information
Jolanrensen authored Jul 20, 2022
2 parents c653d11 + 9849369 commit 3b07b9a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jetbrains.kotlinx.spark.api

import scala.collection.immutable.`Seq$`.`MODULE$` as Seq
import scala.collection.immutable.Seq as Seq
import scala.collection.mutable.`Seq$`.`MODULE$` as MutableSeq
import scala.collection.mutable.Seq as MutableSeq

/**
* Returns a new empty immutable Seq.
*/
fun <T> emptySeq(): Seq<T> = Seq.empty<T>() as Seq<T>

/**
* Returns a new immutable Seq with the given elements.
*/
fun <T> seqOf(vararg elements: T): Seq<T> =
if (elements.isEmpty())
emptySeq()
else
Seq.newBuilder<T>().apply {
for (it in elements)
`$plus$eq`(it)
}.result() as Seq<T>

/**
* Returns a new mutable Seq with the given elements.
*/
fun <T> emptyMutableSeq(): MutableSeq<T> = MutableSeq.empty<T>() as MutableSeq<T>

/**
* Returns a new mutable Seq with the given elements.
*/
fun <T> mutableSeqOf(vararg elements: T): MutableSeq<T> =
if (elements.isEmpty())
emptyMutableSeq()
else
MutableSeq.newBuilder<T>().apply {
for (it in elements)
`$plus$eq`(it)
}.result() as MutableSeq<T>
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ class ApiTest : ShouldSpec({
context("miscellaneous integration tests") {
withSpark(props = mapOf("spark.sql.codegen.comments" to true)) {

should("Create Seqs") {
spark.createDataset(seqOf(1, 2, 3), encoder())
.collectAsList() shouldBe listOf(1, 2, 3)


seqOf(1, 2, 3) shouldBe seqOf(1, 2, 3)
mutableSeqOf(1, 2, 3) shouldBe mutableSeqOf(1, 2, 3)

seqOf<Int>() shouldBe emptySeq<Int>()
mutableSeqOf<Int>() shouldBe emptyMutableSeq<Int>()
}

@OptIn(ExperimentalStdlibApi::class)
should("broadcast variables") {
val largeList = (1..15).map { SomeClass(a = (it..15).toList().toIntArray(), b = it) }
Expand Down

0 comments on commit 3b07b9a

Please sign in to comment.