I am new to scala, I have a use case where I want to define a partial function to add three numbers in which one number is constant and two numbers can be passed as inputs and define another method which can take the partial function as input and gives its cube as result.
Asked
Active
Viewed 161 times
-2
-
2So why this function should by partial? And what is your trouble with implementing it? It seems easy. – SergGr Dec 18 '17 at 06:23
-
@SergGr I forgot to mention I am a beginner in scala and was having a difficulty in understanding one of use case in which we have to do above operation and then take that partial function as input to another function and find the cube of it – R.Gold Dec 18 '17 at 11:45
-
My best guess is that you mix two different terms: "_partial function_" - which means function that is defined not for all parameters like square root that is defined only for positive values; and "_partially applied function_" which means that you had a function of many parameters and then bound some of them to get a function of fewer parameters. – SergGr Dec 18 '17 at 18:00
-
What I exactly wanted to ask is, is there any work around to take the partially defined function as an input to some other function, just like we do in higher order functions – R.Gold Dec 19 '17 at 05:16
-
The most recent edit of your original question is even more confusing. What is the cube of a function? You appear to be using words that you don't fully understand and asking questions that don't make sense. – jwvh Dec 19 '17 at 05:18
-
@jwvh, It clearly says CUBE AS RESULT. Cube means, if x is the number then x * x * x will be its cube, there should be two functions- i. partial function,ii. Second function which takes the partial function itself as input and gives its cube(x*x*x) as the result. – R.Gold Dec 19 '17 at 05:24
-
@R.Gold, the problem with your definition is that it implies that the higher-order function can not take any ternary function. To produce cube it has to take only function that does `(a,b,c) => a*b*c` which kind of defeats the whole idea of the higher-order function being able to convert any function in the same way. P.S. If your addition is a new question that you want to get answer for - you should create a new question on this site. People rarely answer already answered question. – SergGr Dec 19 '17 at 05:32
2 Answers
2
Well... That depends on where is your constant coming from?
Choice 1 - Your function forms a closure with a constant present in scope.
val yourConstant = 10
val pf: PartialFunction[(Int, Int), Int] = {
case (x, y) => x + y + yourConstant
}
pf((5, 10))
Choice 2 - Your function has a local constant.
val pf: PartialFunction[(Int, Int), Int] = {
case (x, y) => x + y + 10
}
pf((5, 10))
Also, as many others pointed out - this does not look like a use case of partial function. Are you sure that you want a Partial Function
and not a partially applied function
?
if you were looking for a partially applied function then,
// first you need a curried function
// Curries function are function which can take parameters in steps to build intermidatary functions.
def normalDef(c: Int)(x: Int, y: Int): Int = c + y + x
// normalDef: normalDef[](val c: Int)(val x: Int,val y: Int) => Int
// now you can "partially apply" this "curried" function to your partially applied function
val addTo10PartiallyApplied = normalDef(10) _
// addTo10PartiallyApplied: (Int, Int) => Int = $Lambda$1240/1924827254@46202553
val total = addTo10PartiallyApplied(1, 2)
// total: Int = 13

sarveshseri
- 13,738
- 28
- 47
1
The following partial function adds 12345 to each number in the tuple passed to it
scala> val addConstantTo: PartialFunction[(Int, Int), Int] = {
| case (a, b) => a + b + 12345
| }
addConstantTo: PartialFunction[(Int, Int),Int] = <function1>
scala> addConstantTo((12, 34))
res4: Int = 12391
This expands on the concept, by programmatically defining a partial function which adds any number to the elements of a tuple:
scala> def addTo(c: Int): PartialFunction[(Int, Int), Int] = {
| case (a, b) => a + b + c
| }
addTo: (c: Int)PartialFunction[(Int, Int),Int]
scala> val pf = addTo(3)
pf: PartialFunction[(Int, Int),Int] = <function1>
scala> pf((1, 2))
res5: Int = 6
Let that sink in for a bit :)

Mike Slinn
- 7,705
- 5
- 51
- 85
-
2Yeah, but as there is no `(Int,Int)` domain for which this is _not_ defined, it's more than a little pointless to make it a `PartialFunction`. I wonder if the OP didn't read/understand the homework assignment and what they are _actually_ looking for is a partially applied function (i.e. currying). – jwvh Dec 18 '17 at 06:31
-
1No idea what OP's motivation was, none of my business. `PartialFunction`s can be total functions, at least from an OO point of view, because `PartialFunction` extends `(A) ⇒ B`; see http://www.scala-lang.org/api/current/scala/PartialFunction.html. There is no requirement that `PartialFunction`s be undefined anywhere. – Mike Slinn Dec 18 '17 at 06:56