An open API service indexing awesome lists of open source software.

https://github.com/tinyield/sek

Sek is a Java wrapper for Kotlin's Sequence. It allows the use of Kotlin's Sequence operations in Java without losing pipeline fluency.
https://github.com/tinyield/sek

fluency java kotlin sequence

Last synced: 16 days ago
JSON representation

Sek is a Java wrapper for Kotlin's Sequence. It allows the use of Kotlin's Sequence operations in Java without losing pipeline fluency.

Awesome Lists containing this project

README

          

# Sek
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=tinyield_sek&metric=alert_status)](https://sonarcloud.io/dashboard?id=tinyield_sek)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=tinyield_sek&metric=coverage)](https://sonarcloud.io/dashboard?id=tinyield_sek)
[![Maven Central](https://img.shields.io/maven-central/v/com.tinyield/sek.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.tinyield%22%20AND%20a:%22sek%22)

_Sek_ is a **Java** wrapper for **Kotlin's**
[_Sequence_](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/-sequence.html).

With _Sek_ you can use the full suite of operations that **Kotlin's**
[_Sequence_](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/-sequence.html) provide without leaving the
**Java** ecosystem, on top of outperforming **Java's** _Stream_ in sequential processing operations in a variety of
use-cases.

## Installation
This project can be installed into yours by adding a maven dependency, like so:
```xml

com.tinyield
sek
1.0.0

```

If you would prefer not to add a dependency to this project, you can also just copy the
[Sek.java](/src/main/java/com/tinyield/Sek.java) file to your project. You will however need to add **Kotlin** to your
project's dependencies, so if you're using maven:
```xml

org.jetbrains.kotlin
kotlin-stdlib
${kotlin.version}

```

For more information check **Kotlin's** official page on using maven [here](https://kotlinlang.org/docs/reference/using-maven.html).

## Usage
Create a _Sek_ using _[of](https://github.com/tinyield/sek/blob/master/src/main/java/org/tinyield/Sek.java#L42)_,
_[generate](https://github.com/tinyield/sek/blob/master/src/main/java/org/tinyield/Sek.java#L66)_ or the
_[empty](https://github.com/tinyield/sek/blob/master/src/main/java/org/tinyield/Sek.java#L73)_ method and then chain
operations into the pipeline until you call a terminal operation like
_[forEach](https://github.com/tinyield/sek/blob/master/src/main/java/org/tinyield/Sek.java#L587)_ or
_[reduce](https://github.com/tinyield/sek/blob/master/src/main/java/org/tinyield/Sek.java#L1088)_. For the full list of
supported methods check [Sek.java](/src/main/java/com/tinyield/Sek.java) or the official Kotlin documentation about
[Sequence](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/-sequence/).

```java
Sek 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 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_](https://github.com/tinyield/sek/blob/master/src/main/java/org/tinyield/Sek.java#L1429) method,
even using **Kotlin's** extension methods. Let's say you have a `Extensions.kt` file with the following definition:
```kotlin
fun Sequence.oddIndexes() = sequence {
var isOdd = false
for (item in this@oddIndexes) {
if(isOdd) yield(item)
isOdd = !isOdd
}
}
```
You could use it with _Sek_ like this:
```java
Sek.of("a", "b", "c", "d", "f", "e")
.then(Extensionskt::oddIndexes)
.forEach(out::println)

// Output
// b
// d
// e
```

## License

This project is licensed under [Apache License,
version 2.0](https://www.apache.org/licenses/LICENSE-2.0)