work in progress on examples

This commit is contained in:
Alex Tkachman
2011-10-23 13:15:37 +02:00
parent 469ea25b13
commit a62fb23e15
10 changed files with 48 additions and 18 deletions
+4 -4
View File
@@ -1,15 +1,15 @@
open class IEquality {
trait IEquality {
fun equals(other : Any) : Boolean
= (this as java.lang.Object).equals(other as java.lang.Object)
}
open class IHashable : IEquality {
trait IHashable : IEquality {
val hashCode : Integer
get() = (this as java.lang.Object).hashCode()
}
open class IMap<K, V> {
trait IMap<K : IHashable, V> {
fun get(key : K) : V
fun set(key : K, value : V) : V
fun remove(key : K) : V
@@ -21,7 +21,7 @@ class HashableWrapper wraps (val obj : Any) : IHashable
[inline] fun Any.hashable() : HashableWrapper = new HashableWrapper(this)
open class IHashingStrategy<K> {
trait IHashingStrategy<K : IHashable> {
fun equals(a : K, b : K) : Boolean
fun hashCode(a : K) : Integer
}
-3
View File
@@ -1,3 +0,0 @@
open class IIterable<out T> {
fun iterator() : IIterator<T>
}
+9
View File
@@ -0,0 +1,9 @@
namespace jet.collections.iterable
import jet.collections.iterator.IIterator
trait IIterable<out T> {
fun iterator() : IIterator<T>
inline fun foreach(operation: fun(element: T) : Unit) = iterator() foreach operation
}
@@ -1,7 +1,21 @@
open class IIterator<out T> {
namespace jet.collections.iterator
trait IIterator<out T> {
fun next() : T
val hasNext : Boolean
inline fun foreach(operation: fun(element: T) : Unit) = while(hasNext) operation(next())
inline fun <R> map(transform: fun(element: T) : R) : IIterator<R> {
val that = this
return object : IIterator<R> {
override fun next() : R = transform(that.next())
override val hasNext : Boolean
get() = that.hasNext
}
}
/*
fun toArray(buffer : MutableArray<in T>) : Int { // T is still an in-parameter
return fillBuffer(buffer, 0, buffer.size)
}
@@ -22,4 +36,5 @@ open class IIterator<out T> {
}
return count
}
*/
}
-4
View File
@@ -1,4 +0,0 @@
open class IList<out T> : IIterable<T>, ISized {
[operator] fun get(index : Int) : T
val isEmpty : Boolean
}
+3
View File
@@ -0,0 +1,3 @@
trait IList<out T> : IIterable<T>, ISized {
fun get(index : Int) : T
}
-3
View File
@@ -1,3 +0,0 @@
open class ISet<T> : IIterable<T>, ISized {
fun contains(item : T) : Boolean
}
+8
View File
@@ -0,0 +1,8 @@
namespace jet.collections.set
import jet.collections.sized.ISized
import jet.collections.iterable.IIterable
trait ISet<T> : IIterable<T>, ISized {
fun contains(item : T) : Boolean
}
-3
View File
@@ -1,3 +0,0 @@
open class ISized {
val size : Int
}
+8
View File
@@ -1,3 +1,11 @@
namespace jet.collections.sized
trait ISized {
val size : Int
val isEmpty : Boolean
get() = size == 0
val isNonEmpty : Boolean
get() = size != 0
}