Skip to content

Instantly share code, notes, and snippets.

@karlicoss
Last active March 19, 2016 18:35
Show Gist options
  • Save karlicoss/04962b865dcdf80fda86 to your computer and use it in GitHub Desktop.
Save karlicoss/04962b865dcdf80fda86 to your computer and use it in GitHub Desktop.
Immutable covariant list
import java.util.*
abstract class Stream<out T>() {
abstract fun <R> with(value: R): Stream<Pair<T, R>>;
}
сlass ImmutableList<out T>: Stream<T> {
private val data: ArrayList<T>;
constructor(data: ArrayList<T>): super() {
this.data = data;
}
constructor(vararg data: T): this(data.toArrayList())
override fun <R> with(value: R): ImmutableList<Pair<T, R>> {
val result = ArrayList<Pair<T, R>>();
for (d in this.data) {
result.add(Pair(d, value))
}
return ImmutableList(result);
}
}
fun main(args : Array<String>) {
val list = ImmutableList<Int>(1, 2, 3, 4, 5);
val whatever: ImmutableList<Pair<Int, String>> = list.with("alal");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment