Can I convert a Kotlin KFunction1 to a KFunction0 by applying the argument? - reflection

I have a reference to a functionthat needs a parameter.
fun foo(x: Int) = 2 * x
val f: KFunction1<Int, Int> = ::foo
Is there any way to write applyArgument where
val f2: KFunction0<Int> = f1.applyArgument(42)
assertEquals("foo", f2.name)
assertEquals(84, f2())
I don't want to use a callable reference, as I need access to the name property.

hope it helps you:
fun foo(x: Int) = 2 * x
val f1 = ::foo
val f0 = { -> f1(42) }
f0() //84

KFunctions are intented to represent functions that are explicitly decleared in Kotlin code, but f2 is not declared anywhere in the code. In addition KFunction has lot of reflection properties and functions which are not relevant to the applied function f2. Therefore even if it is possible it is not recommended.
If you want to do it anyway you can simply write an applyArgument function in this way:
fun <T, R> KFunction1<T, R>.applyArgument(value: T): KFunction0<R> {
return object : KFunction<R> by this, KFunction0<R> {
override fun invoke(): R {
return this#applyArgument(value)
}
}
}
But, if what you need is to preserve the name, I would do it in a safe way. One way could be:
data class Named<out T>(val name: String, val value: T)
fun <T, R> Named<T>.map(transform: (T) -> R): Named<R> = Named(name, transform(value))
val <F : KFunction<*>> F.named: Named<F>
get() = Named(name, this)
Then use it:
fun foo(x: Int) = 2 * x
val f: Named<(Int) -> Int> = ::foo.named
val f2: Named<() -> Int> = f.map { fValue -> { fValue(42) } }
assertEquals("foo", f2.name)
assertEquals(84, f2.value())

Partial application is possible.
You may just declare a function for partial application and use it for the :: reference.
Hence, the name would not be the original function. Another approach - create your own classes/interfaces
data class MyFunction1<T, R>(val name: String, val f: (T) -> R) {
operator fun invoke(t: T) = f(t)
}
data class MyFunction0<R>(val name: String, val f: () -> R) {
operator fun invoke() = f()
}
Now define the curring:
fun MyFunction1<T, R>.curry(t: T) = MyFunction0(name){ f(t) }
(it can be a member function too)

Related

Kotlin: higher order function with recursion

I have to create a higher order function which returns a lambda to learn functional programming with Kotlin.
This is the class
class Product (val productName : String, val price : Double, val rating : Int) {
override fun toString () = "$productName, $price, $rating"
}
this is my function
fun productFactory (productName: String , rating : Int) : (Double) -> Product {
val x : (Double) -> Product = productFactory(productName, rating)
return x
}
this is how I call the function
val cheese = productFactory("Gouda", 5)
val product = cheese(4.99)
Although it seems to work with the needed constructors, it causes a StackOverflowError and I don't know, where the problem is. Can anybody help me?
Your function productFactory is recursively calling itself with no way to exit the recursion, so it will always cause a stack overflow.
The function it returns should certainly not be itself because the behavior is different.
You can define the returned function as a lambda:
fun productFactory (productName: String , rating : Int) : (Double) -> Product {
return { price -> Product(productName, price, rating) }
}
or use function syntax and return the function using the :: operator:
fun productFactory (productName: String , rating : Int) : (Double) -> Product {
fun produce(price: Double) = Product(productName, price, rating)
return ::produce
}

Generic function works, but generic class doesn't?

I would like a class that is a generic for KProperty1, I can do this for a function, but not a class:
import kotlin.reflect.KProperty1
data class Dog(val name: String, val age: Int)
fun <P: KProperty1<*, *>> reflectionHelper(input: P) = input.name
fun <P: KProperty1<*, *>> getReflectionHelper(clazz: P) = ReflectionHelper<P>()
class ReflectionHelper<P: KProperty1<*, *>> {
}
fun main(args : Array<String>) {
println(reflectionHelper(Dog::age)) // Works
val helper1 = getReflectionHelper(Dog::age) // Also Works
val helper2 = ReflectionHelper<Dog::age>() // Error: Type inference failed
}
Dog::age is a value (of type KProperty1<Dog, String>), not a type. In between < and > you need to put a type, or you need to omit them entirely and the type will be inferred (that's what happens in the first two lines).
So the equivalent to your funs would be
class ReflectionHelper<P: KProperty1<*, *>>(input: P) { ... }
val helper2 = ReflectionHelper(Dog::age)
If you don't need input: P as a parameter, you'll have to specify P explicitly both for fun and for class.

Kotlin's reduce() function with different types

I was looking through array extension functions and found reduce() one
inline fun <S, T: S> Array<out T>.reduce(operation: (acc: S, T) -> S): S {
if (isEmpty())
throw UnsupportedOperationException("Empty array can't be reduced.")
var accumulator: S = this[0]
for (index in 1..lastIndex) {
accumulator = operation(accumulator, this[index])
}
return accumulator
}
here the accumulator variable of type S assigned with first element from the array with type T.
Can't wrap my head around the real use case of reduce() function with two data types. Here synthetic example which actually doesn't make any sense.
open class A(var width: Int = 0)
class B(width: Int) : A(width)
val array = arrayOf(A(7), A(4), A(1), A(4), A(3))
val res = array.reduce { acc, s -> B(acc.width + s.width) }
Seems most real life use cases with this function use this signature:
inline fun <T> Array<out T>.reduce(operation: (acc: T, T) -> T): T
Can you help with providing some examples, where reduce() function can be useful with different types.
Here is an example:
interface Expr {
val value: Int
}
class Single(override val value: Int): Expr
class Sum(val a: Expr, val b: Expr): Expr {
override val value: Int
get() = a.value + b.value
}
fun main(args: Array<String>) {
val arr = arrayOf(Single(1), Single(2), Single(3));
val result = arr.reduce<Expr, Single> { a, b -> Sum(a, b) }
println(result.value)
}

How to reverse a Map in Kotlin?

I am trying to reverse a Map in Kotlin. So far, I have come up with:
mapOf("foo" to 42)
.toList()
.map { (k, v) -> v to k }
.toMap()
Is there any better way of doing this without using a middleman(middlelist)?
Since the Map consists of Entrys and it is not Iterable you can use Map#entries instead. It will be mapped to Map#entrySet to create a backed view of Set<Entry>, for example:
val reversed = map.entries.associateBy({ it.value }) { it.key }
OR use Iterable#associate, which will create additional Pairs.
val reversed = map.entries.associate{(k,v)-> v to k}
OR using Map#forEach:
val reversed = mutableMapOf<Int, String>().also {
// v-- use `forEach` here
map.forEach { (k, v) -> it.put(v, k) }
}.toMap()
// ^--- you can add `toMap()` to create an immutable Map.
Here is a simple extension function that reverse a map - without generating unneeded garbage (like pairs, intermediate data structures and unnecessary closures )
fun <K, V> Map<K, V>.reversed() = HashMap<V, K>().also { newMap ->
entries.forEach { newMap.put(it.value, it.key) }
}
note that apply is inlined, and entries.forEach is also inlined (which is not the same for Map::forEach)
In case your map is not a 1-1 mapping and you want the inversion to be a list of values:
mapOf(1 to "AAA", 2 to "BBB", 3 to "BBB").toList()
.groupBy { pair -> pair.second } // Pair<Int, String>
.mapValues { entry ->
entry.value.map { it.first } // Entry<String, List<Pair<Int, String>>
}
If you need to reverse a multimap like m: Map<K, List<V>> to a Map<V, List<K>> you can do
m
.flatMap { it.value.map { oneValue -> oneValue to it.key } }
.groupBy({ it.first }, { it.second })
.toMap()
In sequence,
mapOf('a' to listOf('b', 'c'), 'd' to listOf('b'))
gets flat mapped to a sequence like
listOf('b' to 'a', 'c' to 'a', 'b' to 'd') which gets grouped to
listOf('b' to listOf('a', 'd'), 'c' to listOf('a')) which then gets converted to a map.
This probably creates intermediate objects.
I'm still learning the ins and outs of Kotlin, but I had the same requirement and as of Kotlin 1.2 it appears that you can iterate over a Map and so map() it directly like this:
#Test
fun testThatReverseIsInverseOfMap() {
val intMap = mapOf(1 to "one", 2 to "two", 3 to "three")
val revMap = intMap.map{(k,v) -> v to k}.toMap()
assertTrue(intMap.keys.toTypedArray() contentEquals revMap.values.toTypedArray())
assertTrue(intMap.values.toTypedArray() contentEquals revMap.keys.toTypedArray())
}
This is my take on a 1:1 map
private fun <K, V> Map<K, V>.reverseOneToOneMap(): Map<V, K> {
val result = this.entries.associateBy({ it.value }) { it.key }
if (result.size != this.size) {
throw RuntimeException("Map must be 1:1")
}
return result
}

Kotlin - How to recursively call a lambda function

I'm trying to re implement the linrec function from here in Kotlin. Here is what it currently looks like in Kotlin:
fun <A, B> linrec(indivisible: (List<A>) -> Boolean,
value: (List<A>) -> B,
divide: (List<A>) -> List<List<A>>,
combine: (A, B) -> B
) : (List<A>) -> B {
val myfunc: (List<A>) -> B = { input ->
if (indivisible(input)) {
value(input)
} else {
val split = divide(input)
val left = split[0][0]
val right = myfunc(split[1]) // Error
combine(left, right)
}
}
return myfunc
}
IntelliJ gives me the following errors, when I try to run the code:
Error:(40, 19) Kotlin: Unresolved reference: myfunc
My question is: How do I make a lambda function call itself?
You don't call a lambda ("anonymous function") from inside itself. That's what functions are for:
fun <A, B> linrec(indivisible: (List<A>) -> Boolean,
value: (List<A>) -> B,
divide: (List<A>) -> List<List<A>>,
combine: (A, A) -> B
) : (List<A>) -> B {
fun myfunc(input: List<A>): B { // rearranged things here
return if (indivisible(input)) { // added `return`
value(input)
} else {
val split = divide(input)
val left = split[0][0]
val right = myfunc(split[1])
combine(left, right) // *
}
}
return ::myfunc
}
Now this is exactly the code you've wrote, but it does not compile. On the line I marked with * kotlinc says Type mismatch: inferred type is B but A was expected.
P.S. I have no idea what that code is doing, so I only fixed the compilation error you've asked about.

Resources