The funKTionale library includes methods that implement partial applications. Learn about their use with a code sample.
- [Instructor] A partial application takes a function that accepts some number of arguments, binding values to one or more of those arguments, and returning a new function that only accepts the remaining unbound arguments. Let's go ahead and add another import. And we're going to bring in our funktionale library. This time we want the partials and very specifically we would like partially1 and partially3.
And import, bring in partials, and we're going to bring in partially3. And we'll come all the way down here to the end and we're going to create a method called prefixAndPosfix which returns a function that takes three strings and returns a string.
And once again we got ourselves a lambda and we're going to take an x which is a string, postfix, which is another string, and the results of what we're going to do is going to be. Now this is just going to return back a string and that string is going to have a couple of values in it and the first value is going to be prefix, the second value is going to be x, and the third value is going to be postfix.
And now we're going to create another method and this is going to be called prefixAndBang and this one is going to take two strings and it's going to return a string. And how we get this method is we're going to take it from the prefixAndPosFix and we're going to call partially on it.
So if you remember from the definition of prefixAndPosFix, it wants three parameters, and we're going to tell it, hey, you're only going to get part of what you want. And we're going to supply the missing parameter since prefixAndBang only gives it two. And finally we're going to create a method called hello. And this method returns a function that takes one string and returns a string.
And we're going to call prefixAndBang on it and we're going to do a partially here as well supplying one parameter and that parameter that we're going to supply is going to be "Hello, ". And finally we'll print out the results and we'll call hello, and we'll pass to it, let's call it Funktionale, which is the name of the library and let's run this.
And we can see that it prints out Hello, Funktionale. Partial applications and currying give us the ability to create specialized functions from general ones without introducing code duplication.
- Kotlin as a better Java
- Setting up a Kotlin programming environment
- Val vs. var
- Understanding basic Kotlin programming concepts
- Object-oriented programming
- Using Java from Kotlin
- Using Kotlin from Java
- Annotations, reflection, and DSL construction
- Functional programming in Kotlin