Skip to content

Commit

Permalink
Initial Commit for Kotlin Demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradhan Yash Sharma committed Jun 28, 2019
1 parent 8716557 commit 1987037
Show file tree
Hide file tree
Showing 23 changed files with 305 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Class/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Car {
var yearOfRegistration = 2010
// set(value) {
// if(value > 2017)
// throw RuntimeException("can't register into the future")
// field = value
// }

val yearsSinceRegistration
get() = 2017 - yearOfRegistration
}

val car = Car()

println(car.yearOfRegistration)

car.yearOfRegistration = 2014

println(car.yearOfRegistration)

println(car.yearsSinceRegistration)

//car.yearOfRegistration = 2019
13 changes: 13 additions & 0 deletions ComplileAndRun/src/sample.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fun main(args: Array<String>) {
println("Hello ${args[0]}")
}

/*
kotlinc sample.kt -include-runtime -d sample.jar
java -classpath sample.jar SampleKt World
or
kotlinc sample.kt -d classes
kotlin -classpath classes SampleKt World
*/
24 changes: 24 additions & 0 deletions DSL/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Robot {
val left = "Left"
val right = "Right"
val fast = "Fast"

infix fun turns(direction: String) {
println("turns $direction")
}

infix fun runs(speed: String) {
println("runs $speed")
}
}

fun operate(func: Robot.(theRobot: Robot) -> Unit) {
val robot = Robot()
robot.func(robot)
}

operate {
it turns left
it turns right
it runs fast
}
7 changes: 7 additions & 0 deletions DataClasses/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data class Person(val first: String, val last: String)

val peter = Person("Peter", "Parker")

println(peter)
println(peter.first)
println(peter.component1())
6 changes: 6 additions & 0 deletions DefaultArgs/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fun greet(name: String, msg: String = "Hi ${name.length}") = "$msg $name"

println(greet("Tom", "Hello"))
println(greet("Jerry"))
println(greet(msg = "Howdy", name = "Spike"))
println(greet("Tyke", msg = "Yo"))
3 changes: 3 additions & 0 deletions ExtentionMethod/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun String.shout() = toUpperCase()

println("hello".shout())
35 changes: 35 additions & 0 deletions For/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
for(x in 1..10) {
println(x)
}
println("------")

for(x in 1 until 10) {
println(x)
}
println("------")

for(x in 1..10 step 2) {
println(x)
}
println("------")

for(x in 10 downTo 1) {
println(x)
}
println("------")

for(x in 10 downTo 1 step 2) {
println(x)
}
println("------")

val names = listOf("Jack", "Jill", "Tom", "Kate")

for(name in names) {
println(name)
}
println("------")

for(index in names.indices) {
println("$index -- ${names.get(index)}")
}
15 changes: 15 additions & 0 deletions Function/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fun greet(name: String) = "Hello $name"

println(greet("Bob"))

fun greet2(name: String) : String {
return "Howdy $name"
}

println(greet2("Jane"))

fun printGreet(name: String) {
println("Hi $name")
}

printGreet("Mike")
9 changes: 9 additions & 0 deletions FunctionalType/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
val double = { e: Int -> e * 2 }
println(double(2))

println("----")

listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.filter { it % 2 == 0}
.map(double)
.forEach { println(it) }
13 changes: 13 additions & 0 deletions Infix/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Pizza {
infix fun spread(item: String) {
println("spreading $item")
}
}

val pizza = Pizza()

pizza.spread("Sauce")
pizza.spread("Cheese")

pizza spread "Sauce"
pizza spread "Cheese"
24 changes: 24 additions & 0 deletions Inheritance/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
open class Person(val first: String, val last: String) {
open fun work() { println("working...") }

override fun toString() = "$first $last"
}

class CoolPerson(first : String, last: String) : Person(first, last) {
var coolnessIndex = 5

constructor(first: String, last: String, index: Int) : this(first, last) {
coolnessIndex = index
}

override fun work() {
println("overridden work")
super.work()
}

override fun toString() = "${super.toString()} ${coolnessIndex}"
}

val coolPerson = CoolPerson("Alan", "Turing", 10)
println(coolPerson)
coolPerson.work()
22 changes: 22 additions & 0 deletions Lazy/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
fun compute(n: Int) : Int {
println("called...")
return n
}

val x = 4

if(x > 4 && compute(4) > 2)
println("result")

println("----")

val temp = compute(4)
if(x > 4 && temp > 2)
println("result")

println("----")

val temp2 by lazy { compute(4) }
if(x > 4 && temp2 > 2)
println("result")

13 changes: 13 additions & 0 deletions MultilineString/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
val name = "Job"

val memo = """
|This message was created by $name and
|is being delivered
|securty to...
"""

println(memo)

println(memo.trimMargin())

println(memo.trimMargin("|")) //replace | with ~, for example.
18 changes: 18 additions & 0 deletions NullTypeParam/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fun receive1(name: String?) {
//println(name.length) //ERROR

if(name != null)
println("Size is ${name.length}") //auto casting
else
println("Size is 0")
}

fun receive2(name: String?) {
println("Size is ${name?.length ?: 0}")
}

receive1("Joe")
receive1(null)

receive2("Joe")
receive2(null)
7 changes: 7 additions & 0 deletions Repel/src/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
On the command prompt type kotlinc, which also works as REPL

>kotlinc
Welcome to Kotlin ...
Type :help for help, :quit for quit
=>>> println("hi")
>hi
8 changes: 8 additions & 0 deletions ReturnNull/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fun nickName(name: String) : String? {
if(name == "Robert")
return "Bob";
return null;
}

println(nickName("Robert"))
println(nickName("Venkat"))
3 changes: 3 additions & 0 deletions RunAsScript/src/sample.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//println("Hello World")

//kotlinc -script sample.kts World
5 changes: 5 additions & 0 deletions Statements/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
val age = 17

val canVote = if(age > 17) "Please vote" else "Not so fast, kid"
println(canVote)

21 changes: 21 additions & 0 deletions StaticMethodAndCompanion/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
object Util {
fun getNumberOfCores() = Runtime.getRuntime().availableProcessors()
}

class Car {
companion object { //May optionally take a name after object, for example CarKind
fun kind() = "Electric"
}

fun drive() { println("driving") }
}

println(Util.getNumberOfCores())

val car = Car()
car.drive()

println(Car.kind())

val companion = Car.Companion //If CarKind is used above, then Car.CarKind
println(companion)
3 changes: 3 additions & 0 deletions String/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
val name = "Bob";

println("Hello $name, how are you?")
10 changes: 10 additions & 0 deletions ValAndVar/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
val number1 = 4
println(number1)

//number1 = 4 //ERROR

var number2 = 4
println(number2)

number2 = 5
println(number2)
7 changes: 7 additions & 0 deletions VarArgs/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fun max(vararg numbers: Int) = numbers.reduce { max, e -> if(max < e) e else max }

println(max(1, 2))
println(max(7, 3, 11))

val values = intArrayOf(1, 22, 4)
println(max(4, 5, *values, 8))
16 changes: 16 additions & 0 deletions When/src/sample.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fun process(input: Any) {
when(input) {
1 -> println("That's one")
7, 8 -> println("7 or 8")
in 13..19 -> println("teen")
is String -> println("got a String of length ${input.length}")
else -> println("no clue")
}
}

process(1)
process(7)
process(8)
process(16)
process("hello") //notice automatic casting
process(StringBuilder())

0 comments on commit 1987037

Please sign in to comment.