Samples updated to latest syntax
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
enum class Color(val r, g, b : Int) {
|
||||
enum class Color(val r : Int , val g :Int , val b : Int) {
|
||||
RED : Color(255, 0, 0)
|
||||
GREEN : Color(0, 255, 0)
|
||||
BLUE : Color(0, 0, 255)
|
||||
|
||||
@@ -3,10 +3,10 @@ class Foo(bar : Bar) {
|
||||
private val cache = HashMap<Bar, Foo>()
|
||||
|
||||
// Factory method:
|
||||
Foo() = Foo(null);
|
||||
fun Foo() = Foo(null);
|
||||
|
||||
// Factory method:
|
||||
Foo(bar : Bar) = cache.cachedLookup(bar, this(bar))
|
||||
fun Foo(bar : Bar) = cache.cachedLookup(bar, this(bar))
|
||||
}
|
||||
|
||||
fun doFoo() {
|
||||
@@ -16,7 +16,7 @@ class Foo(bar : Bar) {
|
||||
|
||||
// To create an object of Foo
|
||||
val foo = Foo()
|
||||
or
|
||||
// or
|
||||
val foo = Foo(Bar())
|
||||
|
||||
// Subclass (Foo must be marked as extendable/open, i.e. ont sealed)
|
||||
@@ -24,7 +24,7 @@ class FooSub(S : String) : Foo() { // Delegates to Foo created as a result of th
|
||||
|
||||
}
|
||||
|
||||
extension Map<K, V>.cachedLookup(key : K, lazy value : V) : V {
|
||||
fun Map<K, V>.cachedLookup(key : K, lazy value : V) : V {
|
||||
var v = this[key]
|
||||
if (v == null) {
|
||||
v = value
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
interface class IIterable<out T> {
|
||||
virtual class IIterable<out T> {
|
||||
fun iterator() : IIterator<T>
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
interface class IIterator<out T> {
|
||||
virtual class IIterator<out T> {
|
||||
fun next() : T
|
||||
val hasNext : Boolean
|
||||
|
||||
@@ -14,7 +14,7 @@ interface class IIterator<out T> {
|
||||
if (len == 0) return 0
|
||||
|
||||
var count = 0;
|
||||
for (i in [from .. from + length - 1]) {
|
||||
for (i in from .. from + length - 1) {
|
||||
if (!hasNext)
|
||||
return count
|
||||
buffer[i] = next()
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
interface class IList<out T> : IIterable<T>, ISized {
|
||||
virtual class IList<out T> : IIterable<T>, ISized {
|
||||
[operator] fun get(index : Int) : T
|
||||
val isEmpty : Boolean
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
interface class IMutableIterable<out T> : IIterable<T> {
|
||||
virtual class IMutableIterable<out T> : IIterable<T> {
|
||||
fun mutableIterator() : IMutableIterator<T>
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
interface class IMutableIterator<out T> : IIterator<T> {
|
||||
virtual class IMutableIterator<out T> : IIterator<T> {
|
||||
fun remove() : T
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
interface class IMutableList<T> : IList<T>, IMutableIterable<T> {
|
||||
virtual class IMutableList<T> : IList<T>, IMutableIterable<T> {
|
||||
fun set(index : Int, value : T) : T
|
||||
fun add(index : Int, value : T)
|
||||
fun remove(index : Int) : T
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
interface class IMutableSet<T> : ISet<T>, IMutableIterable<T> {
|
||||
virtual class IMutableSet<T> : ISet<T>, IMutableIterable<T> {
|
||||
fun add(item : T) : Boolean
|
||||
fun remove(item : T) : Boolean
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
interface class ISet<T> : IIterable<T>, ISized {
|
||||
virtual class ISet<T> : IIterable<T>, ISized {
|
||||
fun contains(item : T) : Boolean
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
interface class ISized {
|
||||
virtual class ISized {
|
||||
val size : Int
|
||||
}
|
||||
@@ -6,7 +6,7 @@ class LinkedList<T> : IMutableList<T> {
|
||||
|
||||
private var head : Item = null
|
||||
private var tail : Item = null
|
||||
override var size { get; private set; }
|
||||
override var size get private set;
|
||||
|
||||
override fun add(index : Int, value : T) {
|
||||
size++
|
||||
@@ -29,7 +29,7 @@ class LinkedList<T> : IMutableList<T> {
|
||||
}
|
||||
|
||||
private fun checkIndex(index : Int) {
|
||||
if (!index in [0..size-1]) {
|
||||
if (index !in 0..size-1) {
|
||||
throw IndexOutOfBoundsException(index)
|
||||
}
|
||||
}
|
||||
@@ -63,7 +63,7 @@ class LinkedList<T> : IMutableList<T> {
|
||||
|
||||
private fun itemAt(index : Int) {
|
||||
var result = head
|
||||
for (i in [1..index]) {
|
||||
for (i in 1..index) {
|
||||
result = result.next
|
||||
}
|
||||
return result
|
||||
|
||||
@@ -3,13 +3,11 @@ enum class List<out T>(theSize : Int) : IList<T> {
|
||||
|
||||
Cons<T>(val value : T, val tail : List<T>) : List<T>(1 + tail.size)
|
||||
|
||||
override val size : Int {
|
||||
override val size : Int
|
||||
get() = theSize
|
||||
}
|
||||
|
||||
override val isEmpty : Boolean {
|
||||
override val isEmpty : Boolean
|
||||
get() = this == Nil
|
||||
}
|
||||
|
||||
override fun iterator() = new IIterator() {
|
||||
private var current = List.this
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
interface class IAdder<in T> {
|
||||
virtual class IAdder<in T> {
|
||||
fun add(item : T) : Boolean
|
||||
}
|
||||
|
||||
interface class ICloseable {
|
||||
virtual class ICloseable {
|
||||
fun close()
|
||||
}
|
||||
|
||||
@@ -24,20 +24,19 @@ class FileInput : IIterator<Byte>, JavaCloseableWrapper {
|
||||
override fun next() {
|
||||
if (!nextUsed) {
|
||||
nextUsed = true
|
||||
return next.as<Byte>
|
||||
return next as Byte
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
override val hasNext {
|
||||
|
||||
override val hasNext
|
||||
get() { // implicitly throws IOException
|
||||
if (nextUsed && next != -1) {
|
||||
nextUsed = false
|
||||
next = stream.read() // throws IOException
|
||||
}
|
||||
return next != -1
|
||||
return next != -1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class FileOutput throws IOException : IAdder<Byte>, JavaCloseableWrapper {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
interface class IMap<in K, out V> {
|
||||
virtual class IMap<in K, out V> {
|
||||
|
||||
|
||||
}
|
||||
@@ -95,12 +95,11 @@ class BinaryHeap<T> : IPriorityQueue<T> {
|
||||
|
||||
}
|
||||
|
||||
extension fun IMutableList<T>.swap(a : Int, b : Int) {
|
||||
fun IMutableList<T>.swap(a : Int, b : Int) {
|
||||
val t = this[a]
|
||||
this[a] = this[b]
|
||||
this[b] = t
|
||||
}
|
||||
|
||||
extension val IList<T>.lastIndex : Int
|
||||
val IList<T>.lastIndex : Int
|
||||
get() = this.size - 1
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
interface class IPriorityQueue<T> {
|
||||
virtual class IPriorityQueue<T> {
|
||||
fun extract() : T
|
||||
fun add(item : T)
|
||||
val isEmpty : Boolean
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
class PriorityQueueAsPushPop<T> wraps(wrapped : IPriorityQueue<T>) : IPushPop<T> {
|
||||
override fun pop() = wrapped.extract()
|
||||
override fun push(item : T) = wrapped.add(item)
|
||||
override val isEmpty {
|
||||
override val isEmpty
|
||||
get() = wrapped.isEmpty
|
||||
}
|
||||
}
|
||||
@@ -2,15 +2,15 @@ type Comparison<in T> = {(T, T) : Int}
|
||||
|
||||
fun naturalOrder<in T : IComparable<T>>(a : T, b : T) : Int = a.compareTo(b)
|
||||
|
||||
fun castingNaturalOrder(a : Object, b : Object) : Int = a.as<Comparable<Object>>.compareTo(b.as<Comparable<Object>>)
|
||||
fun castingNaturalOrder(a : Object, b : Object) : Int = (a as Comparable<Object>).compareTo(b as Comparable<Object>)
|
||||
|
||||
enum class ComparisonResult {
|
||||
LS,EQ, GR
|
||||
LS, EQ, GR
|
||||
}
|
||||
|
||||
type MatchableComparison<in T> = {(T, T) : ComparisonResult}
|
||||
|
||||
fun asMatchableComparison<T>(cmp : Comparison<T>) : MatchableComparison<T> = {a, b =>
|
||||
fun asMatchableComparison<T>(cmp : Comparison<T>) : MatchableComparison<T> = {(a, b) =>
|
||||
val res = cmp(a, b)
|
||||
if (res == 0) return ComparisonResult.EQ
|
||||
if (res < 0) return ComparisonResult.LS
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
interface class IComparable<in T> {
|
||||
virtual class IComparable<in T> {
|
||||
fun compareTo(other : T) : Int
|
||||
}
|
||||
Reference in New Issue
Block a user