Is there a builtin function to map a single argume...
# announcements
n
Is there a builtin function to map a single argument to many functions that take that same argument? i.e.
Copy code
val myArg = 1
val myFuncs = listOf(::addOne, ::addTwo, ::addThree)

myArg.someCoolMapFunctions(myFuncs)
There’s a stack overflow answer for Haskell to define this function yourself, but I don’t know what the equivalent Kotlin code would be https://stackoverflow.com/a/47501940/7682196
s
I don’t think Kotlin has this. What would the return type of
someCoolMapFunctions
be?
n
ListType<myArg>
I would think
s
you mean
List<Int>
? or
List<() -> Int>
n
List<Int>
in this case
what i’m doing for now is:
Copy code
val listOfOutputs = listOf<Int>()
for(func in myFuncs) {
    listOfOutputs += func(myArg)
}
s
fun Int.someCoolMapFunctions(list: List<(Int)->Int>) : List<Int> = list.map { it(this) }
Or if you want to return a
List<() -> Int>
(a list of deferred results instead of a list of results):
fun Int.someCoolMapFunctions(list: List<(Int) -> Int>) : List<() -> Int> = list.map { { it(this) } }
r
I don't see why you need it.
Copy code
val outputs = myFuncs.map { it(myArg) }
is pretty concise.
👍 1
n
Ohhhhhhhh. Neat! I didn’t realize you could do that with the regular
map
. Thanks!
👍 2