Skip to content
Kotlin utility types based on Scala
Branch: develop
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
gradle/wrapper
src
.gitignore
.travis.yml
CONTRIBUTING.md
LICENSE
README.md
RELEASE-NOTES.md
build.gradle
gradle.properties
gradlew
gradlew.bat
secring.gpg.enc
settings.gradle

README.md

Build Status Download Maven Central Sonatype Snapshot

Kotlin Utilities

Supported types based on Scala

EmptyIterator

Iterator based on the result of scala.collection.Iterator.empty, producing no values.

Kotlin defines an internal EmptyIterator, which can only be obtained indirectly from empty collections, e.g.:

emptyList<Nothing>().iterator()

SingletonIterator

Iterator based on the result of scala.collection.Iterator.single[A](elem: A), producing a single value.

An iterator for a singleton list is defined in Java, in a package-private static method java.util.Collections.singletonIterator(E e). It can only be obtained indirectly from a new instance of a singleton list, e.g.:

java.util.Collections.singletonList(element).iterator()

Option

Scala documentation Scala sources

Implementation differences:

  • exists has been replaced with any – Kotlin convention
  • forall has been replaced with all – Kotlin convention
  • foreach has been replaced with forEach – Kotlin convention
  • orNull has been replaced with getOrNull – Kotlin convention
  • implemented additional functions: filterNotNull, filterIsInstance – Kotlin convention

Kotlin introduces its own null-safety mechanisms. Most of the times, Options can be replaced by nullable types in Kotlin, e.g.:

var number: Option<Int> = …

number.map { it.toString() }.getOrElse { "" }

gives the same result as:

var number: Int? = …

number?.let { it.toString() }.orEmpty()

However, Options might be useful whenever null values are not allowed, e.g. RxJava 2.x Observable.

Either

Scala documentation Scala sources

Implementation differences:

  • exists has been replaced with any – Kotlin convention
  • forall has been replaced with all – Kotlin convention
  • foreach has been replaced with forEach – Kotlin convention
  • implemented additional functions: filterNot, filterNotNull, filterIsInstance – Kotlin convention
  • implemented additional functions: filterToOption, filterNotToOption, filterNotNullToOption, filterIsInstanceToOption
  • implemented additional function getOrNull in addition to toOption – Kotlin uses its own null-safety mechanisms

Unlike in Scala, this implementation is not right-biased, i.e. it is not possible to use Either.map {} instead of Either.right.map {}. However, the Scala convention that “dictates that Left is used for failure and Right is used for success” (Scala Standard Library Documentation) is still a preferred way of using this class.

Try

Scala documentation Scala sources

Implementation differences:

  • foreach has been replaced with forEach – Kotlin convention
  • implemented additional functions: filterNot, filterNotNull, filterIsInstance – Kotlin convention
  • implemented additional function getOrNull in addition to toOption – Kotlin uses its own null-safety mechanisms

Build configuration

Maven

<dependency>
  <groupId>it.czerwinski</groupId>
  <artifactId>kotlin-util</artifactId>
  <version>1.2</version>
</dependency>

Gradle

implementation 'it.czerwinski:kotlin-util:1.2'
You can’t perform that action at this time.