4

Back2Basics: Understanding Partially Applied Functions

 2 years ago
source link: https://blog.knoldus.com/back2basics-understanding-partially-applied-functions/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Back2Basics: Understanding Partially Applied Functions

Reading Time: 3 minutes

In this blog, we are going to talk about Partially applied functions and its use case. Before starting, first, we will emphasize that though Partial functions and Partially applied functions sound similar they are different from each other. To understand Partial Functions refer.

What is function application?

Applying a function is called calling a function. When we pass all arguments of the function, we can apply the function.   For example, we have a function product.

xxxxxxxxxx
scala> def product(x: Int, y: Int) = x * y
product: (x: Int, y: Int)Int
scala> product(2,3)
res1: Int = 6

But there might be a scenario where the certain argument is repeated and we are calling function many times.  For instance, we always evaluate the doubled value. We can partially apply our function by passing 2 as an argument and skipping another parameter and passing the only type of the second parameter.

xxxxxxxxxx
scala> val double = product(2, _: Int)
double: Int => Int = $$Lambda$1141/1562251195@2eed37f4

This is called Partially applied function of Product because the function is not fully applied yet. We can pass another argument to double apply it fully. The difference between function and partially applied function is that function returns a result but the partially applied function returns another function which again can be applied.

Here double is a function which takes an int value and returns an int value.

xxxxxxxxxx
scala> double(5)
res3: Int = 10

Now double returns result because now function is fully applied. It takes a 5 and multiplies it with 2. We can skip all arguments of a method using _.

xxxxxxxxxx
scala> product _
res16: (Int, Int) => Int = $$Lambda$1271/2134251218@7e5c04a4

Here res16 is a function value which takes two arguments and returns an integer value.

So, what we can do with them?

Partial Functions allows us to create specialized functions from other methods.

xxxxxxxxxx
scala> val double = product(2, _: Int)
double: Int => Int = $$Lambda$1144/95553286@16aed49f
scala> val triple = product(3, _: Int)
triple: Int => Int = $$Lambda$1145/812229472@7235f92b
scala> val quadruple = product(4, _: Int)
quadruple: Int => Int = $$Lambda$1146/1523683407@a146b11
scala> triple(4)
res4: Int = 12
scala> quadruple(4)
res5: Int = 16

So, we can reuse methods by creating a library of specialized functions.  It helps to maintain the DRY principle.  We are not copying and pasting the code to make new functions. Another use of the partially applied function is to create curried functions.

Currying is a technique of creating multiple parameter groups. A function that takes multiple arguments can be translated into a series of function calls that each take a single argument. 

xxxxxxxxxx
scala> def product(x: Int)( y: Int) = x * y
product: (x: Int)(y: Int)Int
scala> val curriedFunction = product _
curriedFunction: Int => (Int => Int) = $$Lambda$1269/2114223493@3d306fd5
scala> res17(4)
res18: Int = 8

We have created a method product as multiple parameters group and treating it as partially applied function. It is a curried function because it returns a function which takes one argument and returns another function which again takes one argument and returns finally result.

The concept of currying and partially applied functions are closely related, but they are not exactly the same.

Every curried function is a partially applied function but vice versa is not true. 

I hope, you will find useful. Thanks for reading.

Feel free to suggest and comment.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK