Last active
March 19, 2016 18:35
-
-
Save karlicoss/04962b865dcdf80fda86 to your computer and use it in GitHub Desktop.
Immutable covariant list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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