Chapter 2, Exercise 3: Implement partial1?

203 views
Skip to first unread message

Harit Himanshu

unread,
Jul 5, 2015, 8:28:34 PM7/5/15
to scala-fu...@googlegroups.com
Hello

I am learning Functional Programming and Scala both for the very first time. I encountered the exercise 3 which says

def partial1[A,B,C](a:A, f:(A,B) => C):B=>C

EXERCISE 3 (hard): Implement partial1 and write down a concrete usage of it. There is only one possible implementation that compiles.


I am not sure what approach I need to take, can someone please help?

Thanks
+ Harit 

para...@gmail.com

unread,
Jul 5, 2015, 8:45:57 PM7/5/15
to scala-fu...@googlegroups.com
Hi Harit,

It might bend your mind a bit if you are just starting out with FP, but you'll get a hang of it.

Let's start with the function definition:

def partial1[A,B,C](a:A, f:(A,B) => C):B=>C 

We need to return a function that takes a B and returns a C. Let's type it out

def partial1[A,B,C](a:A, f:(A,B) => C):B=>C  = {
  // return a function that takes a B and returns a C
  (b: B) => /* Somehow get a value of type C here */
}

How can we get a value of type C? To help us out, we can use the function f that takes (A,B) and returns a C.

def partial1[A,B,C](a:A, f:(A,B) => C):B=>C  = {
  // return a function that takes a B and returns a C
  (b: B) => f(a, b)
}

This can be shortened to 

def partial1[A,B,C](a:A, f:(A,B) => C):B=>C = f(a, _)

But if you find the last definition hard to comprehend, stick to the previous one till you get more comfortable with Scala.

Thanks
Param

--
You received this message because you are subscribed to the Google Groups "scala-functional" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-function...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Harit Himanshu

unread,
Jul 5, 2015, 11:45:49 PM7/5/15
to scala-fu...@googlegroups.com
Thanks Param

I tried to use it as  
class ex03 {
def partial1[A,B,C](a:A, f:(A,B) => C): B => C = f(a,_)
}
and make it run as  
class ex03Test extends UnitSpec {

behavior of "partial1"
it should "return 6" in {
val e = new ex03
val multiplyByTwo = e.partial1(2, (a:Int, b:Int) => a * b)
multiplyByTwo(3) mustBe 6
}
}
Thanks

On Sun, Jul 5, 2015 at 5:45 PM, Parambir Singh <para...@gmail.com> wrote:
Hi Harit,

It might bend your mind a bit if you are just starting out with FP, but you'll get a hang of it.

Let's start with the function definition:
def partial1[A,B,C](a:A, f:(A,B) => C):B=>C 

We need to return a function that takes a B and returns a C. Let's type it out

def partial1[A,B,C](a:A, f:(A,B) => C):B=>C  = {
  // return a function that takes a B and returns a C
  (b: B) => /* Somehow get a value of type C here */
}

How can we get a value of type C? To help us out, we can use the function f that takes (A,B) and returns a C.

def partial1[A,B,C](a:A, f:(A,B) => C):B=>C  = {
  // return a function that takes a B and returns a C
  (b: B) => f(a, b)
}

This can be shortened to 

def partial1[A,B,C](a:A, f:(A,B) => C):B=>C = f(a, _)

But if you find the last definition hard to comprehend, stick to the previous one till you get more comfortable with Scala.

Thanks
Param

--
You received this message because you are subscribed to a topic in the Google Groups "scala-functional" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-functional/tfLo14qU72E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-function...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages