This file contains hidden or 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
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( |
This file contains hidden or 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
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) { |
This file contains hidden or 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
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") |
This file contains hidden or 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
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") |
This file contains hidden or 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
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( |
This file contains hidden or 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
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 |
This file contains hidden or 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
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") | |
} |
This file contains hidden or 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
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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
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" |