Last active
May 22, 2017 02:56
-
-
Save DarinM223/138d782d9bc41742d1673995b3ff5cff to your computer and use it in GitHub Desktop.
Kotlin examples
This file contains hidden or 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
| group 'd_m' | |
| version '1.0-SNAPSHOT' | |
| buildscript { | |
| ext.kotlin_version = '1.1.2-2' | |
| repositories { | |
| mavenCentral() | |
| } | |
| dependencies { | |
| classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | |
| } | |
| } | |
| apply plugin: 'java' | |
| apply plugin: 'kotlin' | |
| kotlin { | |
| experimental { | |
| coroutines 'enable' | |
| } | |
| } | |
| sourceCompatibility = 1.8 | |
| repositories { | |
| jcenter() | |
| mavenCentral() | |
| } | |
| dependencies { | |
| compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" | |
| compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.15" | |
| compile "org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:0.15" | |
| compile "org.asynchttpclient:async-http-client:+" | |
| testCompile group: 'junit', name: 'junit', version: '4.12' | |
| } |
This file contains hidden or 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 kotlinx.coroutines.experimental.CommonPool | |
| import kotlinx.coroutines.experimental.async | |
| import kotlinx.coroutines.experimental.future.await | |
| import kotlinx.coroutines.experimental.runBlocking | |
| import org.asynchttpclient.AsyncHttpClient | |
| import org.asynchttpclient.DefaultAsyncHttpClient | |
| suspend fun get(path: String, client: AsyncHttpClient, num: Int) { | |
| println("Starting job $num") | |
| val resp = client.prepareGet(path).execute().toCompletableFuture().await() | |
| println("Job $num completed with ${resp.statusCode}") | |
| } | |
| fun main(args: Array<String>) { | |
| val client = DefaultAsyncHttpClient() | |
| val deferred = (1..100).map { async(CommonPool) { get("http://www.google.com", client, it) } } | |
| runBlocking { deferred.map { it.await() } } | |
| } |
This file contains hidden or 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
| data class LinkedListNode<T>(val data: T, var next: LinkedListNode<T>?) | |
| fun <T> reverseLinkedList(node: LinkedListNode<T>?): Pair<LinkedListNode<T>?, LinkedListNode<T>?> { | |
| val next = node?.next ?: return Pair(node, node) | |
| val (start, end) = reverseLinkedList(next) | |
| end?.next = node | |
| node.next = null | |
| return Pair(start, node) | |
| } | |
| fun main(args: Array<String>) { | |
| val list = LinkedListNode(1, LinkedListNode(2, LinkedListNode(3, null))) | |
| // Prints: LinkedListNode(data=1, next=LinkedListNode(data=2, next=LinkedListNode(data=3, next=null))) | |
| println(list) | |
| val reversed = reverseLinkedList(list).first | |
| // Prints: LinkedListNode(data=3, next=LinkedListNode(data=2, next=LinkedListNode(data=1, next=null))) | |
| println(reversed) | |
| } |
This file contains hidden or 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
| // TFTP packet types | |
| sealed class Packet | |
| class Rrq(val filename: String, val mode: String) : Packet() | |
| class Wrq(val filename: String, val mode: String) : Packet() | |
| class Data(val blockNum: Short, val data: ByteArray) : Packet() | |
| class Error(val code: Short, val msg: String) : Packet() | |
| fun rrqPacketBytes(packet: Rrq): ByteArray = "RRQ packet bytes".toByteArray() | |
| fun wrqPacketBytes(packet: Wrq): ByteArray = "WRQ packet bytes".toByteArray() | |
| fun dataPacketBytes(packet: Data): ByteArray = "DATA packet bytes".toByteArray() | |
| fun errorPacketBytes(packet: Error): ByteArray = "ERROR packet bytes".toByteArray() | |
| fun packetBytes(packet: Packet): ByteArray = when (packet) { | |
| is Rrq -> rrqPacketBytes(packet) | |
| is Wrq -> wrqPacketBytes(packet) | |
| is Data -> dataPacketBytes(packet) | |
| is Error -> errorPacketBytes(packet) | |
| } | |
| fun main(args: Array<String>) { | |
| val packet: Packet = Rrq("blah.txt", "octet") | |
| // Prints: RRQ packet bytes | |
| println(String(packetBytes(packet))) | |
| } |
This file contains hidden or 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
| data class LinkedListNode<T>(val data: T, var next: LinkedListNode<T>?) | |
| tailrec fun <A, B> mapReverse(list: LinkedListNode<A>?, result: LinkedListNode<B>?, fn: (A) -> B): LinkedListNode<B>? { | |
| val (data, next) = list?.let { Pair(it.data, it.next) } ?: return result | |
| return mapReverse(next, LinkedListNode(fn(data), result), fn) | |
| } | |
| fun main(args: Array<String>) { | |
| val list = LinkedListNode(1, LinkedListNode(2, LinkedListNode(3, null))) | |
| val mappedList = mapReverse(list, null, { i -> i + 1 }) | |
| // Prints: LinkedListNode(data=4, next=LinkedListNode(data=3, next=LinkedListNode(data=2, next=null))) | |
| println(mappedList) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment