Skip to content

Instantly share code, notes, and snippets.

@toxakktl
Created November 15, 2019 09:46
Show Gist options
  • Save toxakktl/02a2676b69eed034ca454dc7e305c177 to your computer and use it in GitHub Desktop.
Save toxakktl/02a2676b69eed034ca454dc7e305c177 to your computer and use it in GitHub Desktop.
package kz.technodom.merchant.service
//Simple hello world
fun main() {
println("Hello, World")
printMessage("Hello!")
printMessageWithPrefix("Hello", "Toxa")
printMessageWithPrefix("Hello2" )
multiply(2,3)
//infix functions
println("dude ".repeat(3))
infix fun Int.times (str:String) = str.repeat(this)
println(3 times "Bob ")
printAll("test1", "test2", "test3")
//variables
val a : String = "teststring"
val b = "kotlin"
var x : Int = 5
x =4
//null safety
var str : String ? = "Koltin tutorial"
str = null
//safe call ?.
length(str)
//bob?.department?.head?.name
val listWithNulls : List<String?> = mutableListOf("MutTest", null, "Modric")
for ( l in listWithNulls){
l?.let { println(it) }
}
//elvis operator
val be : String ? = null
val l = if (be != null) be.length else -1
val c = be?.length ?: -1
val p = Person("Alice")
println(p.name)
p.name = "New name"
val ani = Car(1, "monkey")
println(ani)
//inheritance
val dog: Animal = Dog()
dog.makeSound()
val list = listOf("1", "2", "3")
list.midElement()
}
fun length (str : String?) : Int? {
return str?.length
}
fun printMessage(message:String){
println(message)
}
fun printMessageWithPrefix(message : String, info : String = "Info"){
println("[$info] $message")
}
fun sum (x:Int, y:Int) : Int{
return x + y
}
fun multiply(x : Int, y: Int) = x * y
//Varargs allow you to pass any number of arguments by separating them with commas.
fun printAll(vararg messages : String){
for (m in messages){
println(m)
}
}
//classes
class Customer
class Person(var name: String){
fun printName(){
println(name)
}
}
data class Car (val id: Long, val name: String)
//Kotlin classes are final by default, in order to make them inheritable mark them as open classes
open class Animal(){
open fun makeSound(){
println("def def")
}
}
class Dog : Animal() {
override fun makeSound() {
println("au au")
}
}
class Cat : Animal() {
override fun makeSound() {
println("miau miau")
}
}
package kz.technodom.merchant.service
import java.lang.StringBuilder
fun Int.abs() : Int = if (this < 0) -this else this
fun List<String>.midElement() : String {
if (this.isEmpty()){
throw NoSuchElementException("List is empty")
}
return this[this.size/2]
}
fun main() {
val sb = StringBuilder()
sb.append("yo")
println("sb is $sb")
cases("Hello")
val cakes = listOf("one", "two", "three")
for (c in cakes){
println(c)
}
for (i in 1..3){
println(i)
}
for (i in 2..8 step 2){
println(i)
}
val authors = setOf("Shakespeare", "Hemingway", "Twain")
val writers = setOf("Twain", "Shakespeare", "Hemingway")
println(authors == writers)
println(authors === writers)
val map = mapOf("One" to 1, "Two" to 2)
val x = -5
println(x.abs())
val colors = listOf("blue", "red", "yellow")
println(colors.midElement() )
}
fun cases(obj:Any){
when(obj) {
1 -> println("One")
"Hello"-> println("Greeting")
is Long -> println("$obj is long")
!is String -> println("Not a string")
else -> println("Unknown")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment