Is there a name for this patter in functional prog...
# functional
g
Is there a name for this patter in functional programming? What would you name the function?
Copy code
fun ((A, B) -> Unit).whatShouldThisBeNamed(b: B): (A) -> Unit {
        return { a -> this(a, b) }
    }
(A function that returns a simpler function by providing one of the parameters to the first function)
e
partial application (e.g. https://docs.python.org/3/library/functools.html#functools.partial) or bind (e.g. https://en.cppreference.com/w/cpp/utility/functional/bind), but typically you bind the arguments starting from the left side
although I suppose it's quite unlikely that you'd be using this on a lambda expression, as that would be fairly pointless
g
Thanks. My use case is I have a container class
Copy code
class Container(
    val container : ContainerModel,
    val onItemClick : (Container, Item -> Unit)
) {
    val items = container.items
        .map { Item(it, onItemClick.partialApply(container) }
}
and an item class
Copy code
class Item(
    val item : ItemModel,
    val onItemClick : (Item -> Unit)
)
So I want to provide the container parameter to the lambda passed into the items
Now I don’t really know if I should turn the extension function above into an operator invoke function
a
I would likely take Roman's advice and avoid, but in any case, I think this is known as "currying"
t
Yes, the concept is called partial application. Currying is turning a function with 2 (or more) arguments into 2 functions with 1 argument (e.g: plus(a,b) becomes plus(a)(b), where plus(a) returns a function with 1 argument where a is partially applied