Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More puzzlers #23

Open
dkandalov opened this issue Aug 6, 2017 · 2 comments
Open

More puzzlers #23

dkandalov opened this issue Aug 6, 2017 · 2 comments

Comments

@dkandalov
Copy link

FYI I created this repo https://github.com/dkandalov/kotlin-puzzlers which includes some of the puzzlers from this repo and some new puzzlers. In particular these two might be interesting:

        fun printClassOf(x: X) = when (x) {
            is X.A -> println("is A")
            is X.B -> println("is B")
        }.exhaustive

        val Unit.exhaustive get() = this
    enum class Color {
        Red, Green, Blue;
        companion object
    }

    fun Color.Companion.from(s: String) = when (s) {
        "#FF0000" -> Red
        "#00FF00" -> Green
        else -> null
    }
@dkandalov
Copy link
Author

dkandalov commented Oct 9, 2017

Here is one more puzzler from kotlin slack https://github.com/dkandalov/kotlin-puzzlers/blob/master/puzzlers/x-child-apply.kts:

open class Node(val name: String) {
    fun lookup() = "lookup in: $name"
}

class Example : Node("container") {
    fun createChild(name: String): Node? = Node(name)

    val child1 = createChild("child1")?.apply {
        println("child1 ${lookup()}")
    }
    val child2 = createChild("child2").apply {
        println("child2 ${lookup()}")
    }
}

Example()

@Kvest
Copy link

Kvest commented Oct 23, 2018

fun process(num: Int) = if (num % 2 == 0) {
    print("$num,")
    true
} else {
    false
}

val arr = arrayOf(0, 1, 2, 3, 4, 5)
val result = arr.fold(true) { acc, num ->
    acc && process(num)
}
println(result)

What will be printed?
a) 0,2,4,false
b) 0,2,4,true
c) 0,false
d) 0,1,2,3,4,5,true

angryziber added a commit that referenced this issue Sep 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants