Learn Kotlin by Printing 01234

Image for post
Image for post

Just for fun, I’m writing different ways of printing 01234 and along the way sharing some Kotlin and Programming knowledge.

Direct Printing

The simplest way to print 01234 is

print("01234")

To make this better, is we could assign to a immutable (unchangeable) variable, then only print it.

val value: String? = "01234"
value?.let{ print(it) } ?: "Nothing"

For the sake of showing Kotlin null safe feature, making value as nullable String (denoted with ?). With that the value? is basically check if value is not null then print the value. If not (denoted by Elvis expression ?:), then print "Nothing".

Next, let’s try something simpler.

val value = "123"
print("1${value}4")

Notice few things.

  • We don’t need to explicit state the type, as it is inferred by value "123"
  • We could embed${variable} together with the string to print. The {} could be omitted if there’s a space between the variable and the other letters.

To have a value mutable (changeable), we declare as var.

var variable = 100
variable += 23
print("0${variable}4")

Notice that, the Int type variable could be concat with the letters without need the %d as per the olden days C language.

What if we have 1234 integer, can we print as 01234?

Well, convert it to String, and pad it with 0.

val value = 1234
print(value.toString().padStart(5, '0'))

Control Flow Printing

The most basic is to loop it, and increment the value.

var i = 0
while (i < 5) {
print(i++)
}

But as we all know, for loop is the preferred usually

for (i in 0..4) {
print(i)
}

Notice there’s no need to initialize i. No need to < or >' and no need ++ as the olden days Java or C. Just use 0..4 will do.

Actually 0..4 is an object type IntRange as below. The .. operator will return the two given value as an IntRange type object.

for (i in IntRange(0, 4)) {
print(i)
}

The 0..4 is a type IntRange, which implements Iterator. That enables it to iterate through all the value in the range. With that it could use the chaining operator i.e. forEach

(0..4).forEach { print(it) }

If this is not English enough, we could also use the until infix extension function of Int

(0 until 5).forEach { print(it) }

If this is still not enough, Kotlin standard library provides the repeat function

repeat(5, { print(it) })

Function Class Printing

Move on to Function, so that we could factorize our logic

fun printme(i: Int) = repeat(i, { print(it) })
printme(5)

For a single line function, we could use = and eliminate {}, as well as the return type.

Of course if for multiline function (like a recursive function below), you’ll need {}.

fun printme(i: Int) {
if (i > 0) printme(i-1)
print(i)
}
printme(4)

Let’s look at class,

class PrintMe(val i: Int = 5){
fun printme() = repeat(i, { print(it) })
}
PrintMe().printme()

Look at Kotlin class, we could have

  • Constructor is like a function argument sent.
  • Default argument could be provided i.e. 5 is set to i.

Function argument — lambda

fun printme(i: Int, fn : (Int)-> Unit) = repeat(i, fn)
printme(5, ::print)

As part of supporting functional programming, Kotlin have lambda support, by sending in the function (e.g. ::print) that match the required signature.

Extension function

fun Int.printme() = repeat(this, { print(it) })
5.printme()

If we want to make any integer number to have a function, we could make extension function to it as above.

Infix function

infix fun Int.till(other: Int) {
for(i in this..other) { print(i) }
}
0 till 4

Infix function is a special way of Kotlin enable to variable linked together in a function that is without . and (). Without infix, it would be written as 0.till(4)

Operator overloading

operator fun String.rangeTo(other: String) {
for(i in this.toInt()..other.toInt()) { print(i) }
}
"0".."4"

I overload the operator of rangeTo (i.e. ..) for String to print the given value incremental. Note I can’t overload the Integer rangeTo operator, as extension is shadowed by a member: public final operator fun rangeTo(other: Int): IntRange

The above definitely hasn’t cover all aspects of Kotlin, there are much much more to it. But hopes this provides some good initial overview of various interesting features of what Kotlin could do.

I hope this post is helpful to you. You could check out my other interesting topics here.

Follow me on medium, Twitter or Facebook for little tips and learning on Android, Kotlin etc related topics. ~Elye~

Written by

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store