Sek is a Java wrapper for Kotlin's Sequence.
With Sek you can use the full suite of operations that Kotlin's Sequence provide without leaving the Java ecosystem, on top of outperforming Java's Stream in sequential processing operations in a variety of use-cases.
This project can be installed into yours by adding a maven dependency, like so:
<dependency>
<groupId>com.tinyield</groupId>
<artifactId>sek</artifactId>
<version>1.0.0</version>
</dependency>
If you would prefer not to add a dependency to this project, you can also just copy the Sek.java file to your project. You will however need to add Kotlin to your project's dependencies, so if you're using maven:
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
For more information check Kotlin's official page on using maven here.
Create a Sek using of, generate or the empty method and then chain operations into the pipeline until you call a terminal operation like forEach or reduce. For the full list of supported methods check Sek.java or the official Kotlin documentation about Sequence.
Sek<String> songs = Sek.of(
new Song("505", "Alternative"),
new Song("Amsterdam", "Alternative"),
new Song("Mural","Hip-Hop")
)
.filterNot(song -> song.name().startsWith("A"))
.map(Song::name);
Sek<String> artists = Sek.of(
new Artist("Arctic Monkeys", "band"),
new Artist("Nothing But Thieves", "band"),
new Artist("Lupe Fiasco", "solo-artist")
)
.distinctBy(Artist::type)
.map(Artist::name);
songs.zip(artists, (song, artist) -> String.format("%s by %s", song, artist))
.forEach(System.out::println);
// Output
// 505 by Arctic Monkeys
// Mural by Lupe Fiasco
You can also add user-defined operations to your pipeline by using the
then method,
even using Kotlin's extension methods. Let's say you have a Extensions.kt
file with the following definition:
fun <T> Sequence<T>.oddIndexes() = sequence<T> {
var isOdd = false
for (item in this@oddIndexes) {
if(isOdd) yield(item)
isOdd = !isOdd
}
}
You could use it with Sek like this:
Sek.of("a", "b", "c", "d", "f", "e")
.then(Extensionskt::oddIndexes)
.forEach(out::println)
// Output
// b
// d
// e
This project is licensed under Apache License, version 2.0