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)
|
RED : Color(255, 0, 0)
|
||||||
GREEN : Color(0, 255, 0)
|
GREEN : Color(0, 255, 0)
|
||||||
BLUE : Color(0, 0, 255)
|
BLUE : Color(0, 0, 255)
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ class Foo(bar : Bar) {
|
|||||||
private val cache = HashMap<Bar, Foo>()
|
private val cache = HashMap<Bar, Foo>()
|
||||||
|
|
||||||
// Factory method:
|
// Factory method:
|
||||||
Foo() = Foo(null);
|
fun Foo() = Foo(null);
|
||||||
|
|
||||||
// Factory method:
|
// Factory method:
|
||||||
Foo(bar : Bar) = cache.cachedLookup(bar, this(bar))
|
fun Foo(bar : Bar) = cache.cachedLookup(bar, this(bar))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun doFoo() {
|
fun doFoo() {
|
||||||
@@ -16,7 +16,7 @@ class Foo(bar : Bar) {
|
|||||||
|
|
||||||
// To create an object of Foo
|
// To create an object of Foo
|
||||||
val foo = Foo()
|
val foo = Foo()
|
||||||
or
|
// or
|
||||||
val foo = Foo(Bar())
|
val foo = Foo(Bar())
|
||||||
|
|
||||||
// Subclass (Foo must be marked as extendable/open, i.e. ont sealed)
|
// 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]
|
var v = this[key]
|
||||||
if (v == null) {
|
if (v == null) {
|
||||||
v = value
|
v = value
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
interface class IIterable<out T> {
|
virtual class IIterable<out T> {
|
||||||
fun iterator() : IIterator<T>
|
fun iterator() : IIterator<T>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
interface class IIterator<out T> {
|
virtual class IIterator<out T> {
|
||||||
fun next() : T
|
fun next() : T
|
||||||
val hasNext : Boolean
|
val hasNext : Boolean
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ interface class IIterator<out T> {
|
|||||||
if (len == 0) return 0
|
if (len == 0) return 0
|
||||||
|
|
||||||
var count = 0;
|
var count = 0;
|
||||||
for (i in [from .. from + length - 1]) {
|
for (i in from .. from + length - 1) {
|
||||||
if (!hasNext)
|
if (!hasNext)
|
||||||
return count
|
return count
|
||||||
buffer[i] = next()
|
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
|
[operator] fun get(index : Int) : T
|
||||||
val isEmpty : Boolean
|
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>
|
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
|
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 set(index : Int, value : T) : T
|
||||||
fun add(index : Int, value : T)
|
fun add(index : Int, value : T)
|
||||||
fun remove(index : Int) : 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 add(item : T) : Boolean
|
||||||
fun remove(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
|
fun contains(item : T) : Boolean
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
interface class ISized {
|
virtual class ISized {
|
||||||
val size : Int
|
val size : Int
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@ class LinkedList<T> : IMutableList<T> {
|
|||||||
|
|
||||||
private var head : Item = null
|
private var head : Item = null
|
||||||
private var tail : 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) {
|
override fun add(index : Int, value : T) {
|
||||||
size++
|
size++
|
||||||
@@ -29,7 +29,7 @@ class LinkedList<T> : IMutableList<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun checkIndex(index : Int) {
|
private fun checkIndex(index : Int) {
|
||||||
if (!index in [0..size-1]) {
|
if (index !in 0..size-1) {
|
||||||
throw IndexOutOfBoundsException(index)
|
throw IndexOutOfBoundsException(index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,7 +63,7 @@ class LinkedList<T> : IMutableList<T> {
|
|||||||
|
|
||||||
private fun itemAt(index : Int) {
|
private fun itemAt(index : Int) {
|
||||||
var result = head
|
var result = head
|
||||||
for (i in [1..index]) {
|
for (i in 1..index) {
|
||||||
result = result.next
|
result = result.next
|
||||||
}
|
}
|
||||||
return result
|
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)
|
Cons<T>(val value : T, val tail : List<T>) : List<T>(1 + tail.size)
|
||||||
|
|
||||||
override val size : Int {
|
override val size : Int
|
||||||
get() = theSize
|
get() = theSize
|
||||||
}
|
|
||||||
|
|
||||||
override val isEmpty : Boolean {
|
override val isEmpty : Boolean
|
||||||
get() = this == Nil
|
get() = this == Nil
|
||||||
}
|
|
||||||
|
|
||||||
override fun iterator() = new IIterator() {
|
override fun iterator() = new IIterator() {
|
||||||
private var current = List.this
|
private var current = List.this
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
interface class IAdder<in T> {
|
virtual class IAdder<in T> {
|
||||||
fun add(item : T) : Boolean
|
fun add(item : T) : Boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
interface class ICloseable {
|
virtual class ICloseable {
|
||||||
fun close()
|
fun close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,20 +24,19 @@ class FileInput : IIterator<Byte>, JavaCloseableWrapper {
|
|||||||
override fun next() {
|
override fun next() {
|
||||||
if (!nextUsed) {
|
if (!nextUsed) {
|
||||||
nextUsed = true
|
nextUsed = true
|
||||||
return next.as<Byte>
|
return next as Byte
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
override val hasNext {
|
override val hasNext
|
||||||
get() { // implicitly throws IOException
|
get() { // implicitly throws IOException
|
||||||
if (nextUsed && next != -1) {
|
if (nextUsed && next != -1) {
|
||||||
nextUsed = false
|
nextUsed = false
|
||||||
next = stream.read() // throws IOException
|
next = stream.read() // throws IOException
|
||||||
}
|
}
|
||||||
return next != -1
|
return next != -1
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class FileOutput throws IOException : IAdder<Byte>, JavaCloseableWrapper {
|
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]
|
val t = this[a]
|
||||||
this[a] = this[b]
|
this[a] = this[b]
|
||||||
this[b] = t
|
this[b] = t
|
||||||
}
|
}
|
||||||
|
|
||||||
extension val IList<T>.lastIndex : Int
|
val IList<T>.lastIndex : Int
|
||||||
get() = this.size - 1
|
get() = this.size - 1
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
interface class IPriorityQueue<T> {
|
virtual class IPriorityQueue<T> {
|
||||||
fun extract() : T
|
fun extract() : T
|
||||||
fun add(item : T)
|
fun add(item : T)
|
||||||
val isEmpty : Boolean
|
val isEmpty : Boolean
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
class PriorityQueueAsPushPop<T> wraps(wrapped : IPriorityQueue<T>) : IPushPop<T> {
|
class PriorityQueueAsPushPop<T> wraps(wrapped : IPriorityQueue<T>) : IPushPop<T> {
|
||||||
override fun pop() = wrapped.extract()
|
override fun pop() = wrapped.extract()
|
||||||
override fun push(item : T) = wrapped.add(item)
|
override fun push(item : T) = wrapped.add(item)
|
||||||
override val isEmpty {
|
override val isEmpty
|
||||||
get() = wrapped.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 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 {
|
enum class ComparisonResult {
|
||||||
LS,EQ, GR
|
LS, EQ, GR
|
||||||
}
|
}
|
||||||
|
|
||||||
type MatchableComparison<in T> = {(T, T) : ComparisonResult}
|
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)
|
val res = cmp(a, b)
|
||||||
if (res == 0) return ComparisonResult.EQ
|
if (res == 0) return ComparisonResult.EQ
|
||||||
if (res < 0) return ComparisonResult.LS
|
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
|
fun compareTo(other : T) : Int
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user