Skip to content

Instantly share code, notes, and snippets.

View automationhacks's full-sized avatar

Gaurav Singh automationhacks

View GitHub Profile
@automationhacks
automationhacks / Traverse.kt
Created June 16, 2019 12:27
Initial example for a recursive logic to walk in a nested structure and get text values from a leaf node
package _04_traversingHierarchies
abstract class Element
class Container(vararg val children: Element) : Element()
class Text(val text: String) : Element()
fun main(args: Array<String>) {
val root = Container(
Text("a"),
Container(
@automationhacks
automationhacks / TraverseWithLocalFunc.kt
Created June 16, 2019 12:28
Moving function as a local func.
fun Element.extractText(): String {
val sb = StringBuilder()
fun extractText(e: Element): StringBuilder {
if (e is Text) {
val text = e
sb.append(text.text)
} else if (e is Container) {
val container = e
for (child in container.children) {
@automationhacks
automationhacks / TraverseWithWhen.kt
Created June 16, 2019 12:28
Converting if else to a when
fun Element.extractText(): String {
val sb = StringBuilder()
fun extractText(e: Element): StringBuilder {
when (e) {
is Text -> sb.append(e.text)
is Container -> for (child in e.children) {
extractText(child)
}
else -> error("Unrecognized element : $e")
@automationhacks
automationhacks / TraverseWithUseOfForEach.kt
Created June 16, 2019 12:30
Traverse code with use forEach and method references
fun Element.extractText(): String {
val sb = StringBuilder()
fun extractText(e: Element) {
when (e) {
is Text -> sb.append(e.text)
is Container -> {
e.children.forEach(::extractText)
}
else -> error("Unrecognized element : $e")
@automationhacks
automationhacks / TraverseFinal.kt
Created June 16, 2019 12:31
Final code with all of kotlin idioms use
package _04_traversingHierarchies
sealed class Element
class Container(vararg val children: Element) : Element()
class Text(val text: String) : Element()
fun main() {
val root = Container(
Text("a"),
Container(
@automationhacks
automationhacks / WithClause.kt
Created June 18, 2019 14:16
Initial example of working with val var and with
package _05_expressions
class Robot(val name: String, val function: String, val weight: Int)
fun main() {
var chappie = Robot("Chappie", "Fight", 25)
val name = chappie.name
val function = chappie.function
val weight = chappie.weight
@automationhacks
automationhacks / UsingWith.kt
Created June 18, 2019 15:08
Use of with func in kotlin
package _05_expressions
class Robot(val name: String, val function: String, val weight: Int)
fun main() {
val chappie = Robot("Chappie", "Fight", 25)
with(chappie) {
println("Robot is $name who does $function with weight $weight")
}
@automationhacks
automationhacks / LiftingIfOutsideBefore.kt
Created July 3, 2019 16:45
Demonstrates how we can lift assignments out of if elses
package _05_expressions
// Simple HTTP response class
class Response(val statusCode : Int)
fun main() {
// A sample response
val response = Response(200)
// Conditional statement which decides and gives a value out for further consumption
@automationhacks
automationhacks / AfterLiftingIfStatement.kt
Created July 3, 2019 16:50
Result of If can be assigned to the variable and the last statement in conditional is automagically returned
package _05_expressions
// Simple HTTP response class
class Response(val statusCode: Int)
fun main() {
// A sample response
val response = Response(200)
// In Kotlin, After lifting the assignment out of if else block
@automationhacks
automationhacks / WhenClause.kt
Created July 3, 2019 17:02
Example of using When as an expression and some other capabilities
package _05_expressions
fun apiResponses(response : Response) : String {
when(response.statusCode) {
200 -> return "Success"
201 -> return "Success"
204 -> return "Success"
300 -> return "Redirection"
400 -> return "Bad request"
401 -> return "Unauthorized"