'Is there a less ugly way to return function in Kotlin?

This declaration works, but is not the most beautiful code. Is there a way to return functions less ugly? I tried (s: String) -> writer.println(s) but this didn't work.

val writeStuff: (PrintWriter) -> (String) -> Unit = {
    val writer = it
    val f: (String) -> Unit = {
        writer.println(it)
    }
    f
}
PrintWriter("test").use { writeStuff(it)("TEST") }

EDIT: a bit more concrete example:

val writeStuff: (PrintWriter) -> (String) -> Unit = { writer ->
    { writer.println(it) }
}

val sendStuff: (Any) -> (String) -> Unit = { sender ->
    { sender.equals(it) }
}

@Test fun test1() {
    val li = listOf("a", "b", "c")

    val process: List<(String) -> Unit> = 
        listOf(writeStuff(PrintWriter("a")), sendStuff(Object()))

    process.map { li.map(it) }
}


Solution 1:[1]

I stumbled across this question while trying to figure out how to return a Function (the java interface) in Kotlin. While this doesn't directly answer the question, hopefully it'll help someone else who has the same query:

override fun myFun(param1: Object): Function<in Object, out String?> {
    if (!param1.meetsCriteria()) 
        return Function { obj -> null }
    return Function { obj ->
        "success"
    }
}

In this case, I was overriding a method in a java interface that required me to return a Function instance. (Note that since the param is not used in my particular implementation above, I could remove it and just have the return result. eg return Function { null })

Edit: After some research, it turns out Kotlin covers this subject with their discussion on "SAM (single abstract method) conversions" here and here, though it may not be the most intuitive thing to look up when figuring out how to return Functions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 takanuva15