-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pradhan Yash Sharma
committed
Jun 28, 2019
1 parent
8716557
commit 1987037
Showing
23 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
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 | ||
*/ |
This file contains 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
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 | ||
} |
This file contains 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
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()) |
This file contains 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
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")) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fun String.shout() = toUpperCase() | ||
|
||
println("hello".shout()) |
This file contains 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
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)}") | ||
} |
This file contains 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
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") |
This file contains 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
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) } |
This file contains 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
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" |
This file contains 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
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() |
This file contains 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
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") | ||
|
This file contains 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
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. |
This file contains 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
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) |
This file contains 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
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 |
This file contains 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
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")) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//println("Hello World") | ||
|
||
//kotlinc -script sample.kts World |
This file contains 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
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) | ||
|
This file contains 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
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) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
val name = "Bob"; | ||
|
||
println("Hello $name, how are you?") |
This file contains 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
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) |
This file contains 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
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)) |
This file contains 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
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()) |