Newbie (ish) question .... How do I do what the co...
# getting-started
e
Newbie (ish) question .... How do I do what the comment in that asks (or something similar) ? Updated link: https://pl.kotl.in/QerMFeLbh
maybeMunge is obviously contrived. But I am looking for a way to pass a closure to a mapNotNull()
I tried to do something like.. fun User.maybeMunge(x : String): () -> Pair<String,Int>? { but couldn't figure out how to pass that to
users.mapNotNull()
p
https://pl.kotl.in/o68cqNVIT Is this what you’re looking for?
e
I was trying to do something like this ... which I know won't work....
Copy code
data class User(val name: String, val age: Int)

fun User.maybeMunge(x: String): () -> Pair<String, Int>? {
    return { it ->
         if (it.name == "Ed") null else Pair(x + it.name, it.age)
    }
}

fun main() {
    val users = listOf(User("Ed", 2), User("Sally", 20))
    println(users.mapNotNull(User::maybeMunge("x")))
}

or 

data class User(val name: String, val age: Int)

fun User.maybeMunge(x: String): () -> Pair<String, Int>? {
    return { 
         if (this.name == "Ed") null else Pair(x + this.name, this.age)
    }
}

fun main() {
    val users = listOf(User("Ed", 2), User("Sally", 20))
    println(users.mapNotNull(User::maybeMunge("x")))
}
it doesn't have to be a class method I guess.
basically I have a sequence/list of T and want to provide a closure to mapNotNull for mapping them.
the closure is important because I need to use the variable passed to the closure in the function being called during mapNotNull. I was looking for a way to do that with :: for whatever reason. Maybe that's where I am hung up.
and the way I am doing it is fine w/o a closure.
p
It seems like you’re trying to get currying/partial application to work, which does get a bit awkward (and/or impossible). Easiest thing to do is, yeah, just embrace the lambdas and not worry about the syntax too much
e
I am extending the class with the method because the actual implementation of the mapNotNull is tied very much to that class.
p
Sure, nothing wrong with using an extension method and calling that in the
mapNotNull
. It seems like you’re trying to avoid the “explicit” open lambda body in your
mapNotNull
. AFAIK, the only way to do that is with some kind of function literal like I have in my example
e
thx!
p
You can infer the type on the “partial” if you make your lambda types explicit:
Copy code
val users = listOf(User("Ed", 2), User("Sally", 20))
    val xed = { it: User -> it.maybeMunge("x") }
    println(users.mapNotNull(xed::invoke))
Or you can use a
fun interface
if you want to make the name after the double-colon something other than `invoke`:
Copy code
fun interface UserMunger {
    fun munge(user: User): Pair<String, Int>?
}

fun main() {
    val users = listOf(User("Ed", 2), User("Sally", 20))
    val yed = UserMunger { it.maybeMunge("y") }
    println(users.mapNotNull(yed::munge))
}
e
yeah, at that point I may as well just call
users.mapNotNull { it.maybeMunge("x") }
Really I guess I am trying to do this:
Copy code
data class User(val name: String, val age: Int)

fun maybeMunge(x: String): (User) -> Pair<String, Int>? {
    return { it ->
         if (it.name == "Ed") null else Pair(x + it.name, it.age)
    }
}

fun main() {
    val users = listOf(User("Ed", 2), User("Sally", 20))
    val mm = maybeMunge("x")
    println(users.mapNotNull(mm::invoke))
}
which works
not sure why I got so hung up on the extension way