# Multiplication Partial functions

5 replies
thinkingmind
Joined: 2011-12-29,

Hi,

I'm learning partial functions. And in that process I have two partial
functions as below:

import scala.{PartialFunction => PF}
val s2i: PF[String, Int] = { case s:String if s.matches("[+-]?\\d+")
=> Integer.parseInt(s) }
val mul: PF[(Int, Int), Int] = { case (a:Int, b:Int) => a * b}

With out using implicit conversions is there a way to do
multiplication of string representation of integer tuple to integer
using composition.

For example: mul("-2", "-5") = 10 using s2i partial function.

Thanks,
Ramesh

ichoran
Joined: 2009-08-14,
Re: Multiplication Partial functions
Scala doesn't have an andThenMap method, which is what you would need here for regular functions:

f: A => B
g: B => C
f andThen g: A => C

f: A => B
h: (B,B) => C
f andThenMap h: (A,A) => C

And you would need it to be smarter yet to handle partial functions, since if _either_ argument were undefined, then the whole thing would be undefined.

Generally, one resorts to options in this case (and even then, there isn't a map on tuples, so it won't do exactly what you want).

Better to just write it out each time, or write your own

def pfAndThenMap2[A,B,C](f: PF[A,B], g: PF[(B,B),C]): PF[(A,A),C] = {
case aa if (f.isDefinedAt(aa._1) && f.isDefinedAt(aa._2) && g.isDefinedAt(f(aa._1),f(aa._2))) => g(f(aa._1),f(aa._2))
}

--Rex

On Thu, Dec 29, 2011 at 12:00 PM, thinkingmind <ramesh [dot] mandaleeka [at] gmail [dot] com> wrote:
Hi,

I'm learning partial functions. And in that process I have two partial
functions as below:

import scala.{PartialFunction => PF}
val s2i: PF[String, Int] = { case s:String if s.matches("[+-]?\\d+")
=> Integer.parseInt(s) }
val mul: PF[(Int, Int), Int] = { case (a:Int, b:Int) => a * b}

With out using implicit conversions is there a way to do
multiplication of string representation of integer tuple to integer
using composition.

For example: mul("-2",  "-5") = 10  using s2i partial function.

Thanks,
Ramesh

missingfaktor
Joined: 2010-04-13,
Re: Multiplication Partial functions
(Uses Scalaz.)
scala> type PF[-A, +B] = PartialFunction[A, B] defined type alias PF
scala> val s2i: PF[String, Int] = { case s:String if s.matches("[+-]?\\d+") => s.toInt } s2i: PF[String,Int] = <function1>
scala> val mul: PF[(Int, Int), Int] = { case (a:Int, b:Int) => a * b} mul: PF[(Int, Int),Int] = <function1>
scala> ((_: String => Int) *** (_: String => Int)).curried.join apply s2i apply ("-2", "-5") res58: (Int, Int) = (-2,-5)

On Thu, Dec 29, 2011 at 10:30 PM, thinkingmind <ramesh [dot] mandaleeka [at] gmail [dot] com> wrote:
Hi,

I'm learning partial functions. And in that process I have two partial
functions as below:

import scala.{PartialFunction => PF}
val s2i: PF[String, Int] = { case s:String if s.matches("[+-]?\\d+")
=> Integer.parseInt(s) }
val mul: PF[(Int, Int), Int] = { case (a:Int, b:Int) => a * b}

With out using implicit conversions is there a way to do
multiplication of string representation of integer tuple to integer
using composition.

For example: mul("-2",  "-5") = 10  using s2i partial function.

Thanks,
Ramesh

--
Cheers,missingfaktor.
missingfaktor
Joined: 2010-04-13,
Re: Multiplication Partial functions
Explanation:
(f *** g) = \(a, b) -> (f a, g b) and join f = \x -> f x x
So,join (***) f = \(a1, a2) -> (f a1, f a2)

On Fri, Dec 30, 2011 at 12:27 PM, missingfaktor <rahul [dot] phulore [dot] 999 [at] gmail [dot] com> wrote:
(Uses Scalaz.)
scala> type PF[-A, +B] = PartialFunction[A, B] defined type alias PF
scala> val s2i: PF[String, Int] = { case s:String if s.matches("[+-]?\\d+") => s.toInt } s2i: PF[String,Int] = <function1>
scala> val mul: PF[(Int, Int), Int] = { case (a:Int, b:Int) => a * b} mul: PF[(Int, Int),Int] = <function1>
scala> ((_: String => Int) *** (_: String => Int)).curried.join apply s2i apply ("-2", "-5") res58: (Int, Int) = (-2,-5)

On Thu, Dec 29, 2011 at 10:30 PM, thinkingmind <ramesh [dot] mandaleeka [at] gmail [dot] com> wrote:
Hi,

I'm learning partial functions. And in that process I have two partial
functions as below:

import scala.{PartialFunction => PF}
val s2i: PF[String, Int] = { case s:String if s.matches("[+-]?\\d+")
=> Integer.parseInt(s) }
val mul: PF[(Int, Int), Int] = { case (a:Int, b:Int) => a * b}

With out using implicit conversions is there a way to do
multiplication of string representation of integer tuple to integer
using composition.

For example: mul("-2",  "-5") = 10  using s2i partial function.

Thanks,
Ramesh

--
Cheers,missingfaktor.

--
Cheers,missingfaktor.
Anwar Rizal
Joined: 2011-07-05,
Re: Multiplication Partial functions
I don't find a composition that will work for this problem either.
But I find this solution interesting:
scala> for {     |     x <- s2i.lift("2")      |     y <- s2i.lift("10")     | }     | yield x * yres1: Option[Int] = Some(20)
scala> for {     |     x <- s2i.lift("P")      |     y <- s2i.lift("10")     | }     | yield x * yres2: Option[Int] = None

With the same spirit, we can also use scalaz applicative builder as follows:
import scalaz._import Scalaz._
( s2i.lift("2")  |@| s2i.lift("5") ) { _ + _ } // Option(10)
Cheers,Anwar Rizal.

On Thu, Dec 29, 2011 at 6:00 PM, thinkingmind <ramesh [dot] mandaleeka [at] gmail [dot] com> wrote:
Hi,

I'm learning partial functions. And in that process I have two partial
functions as below:

import scala.{PartialFunction => PF}
val s2i: PF[String, Int] = { case s:String if s.matches("[+-]?\\d+")
=> Integer.parseInt(s) }
val mul: PF[(Int, Int), Int] = { case (a:Int, b:Int) => a * b}

With out using implicit conversions is there a way to do
multiplication of string representation of integer tuple to integer
using composition.

For example: mul("-2",  "-5") = 10  using s2i partial function.

Thanks,
Ramesh

Chris Marshall
Joined: 2009-06-17,
RE: Multiplication Partial functions
scala> import scalaz._; import Scalaz._import scalaz._import Scalaz._
scala> lazy val f: Int => String = i => i.toStringf: Int => String = <lazy>
scala> lazy val h: ((String, String)) => Double = t2 => (t2._1 + t2._2).toDoubleh: (String, String) => Double = <lazy>

And so...
scala> (f *** f) andThen hres0: (Int, Int) => Double = <function1>

scala> res0((1, 2))res1: Double = 12.0

Date: Thu, 29 Dec 2011 14:07:46 -0500
Subject: Re: [scala-user] Multiplication Partial functions
From: ichoran [at] gmail [dot] com
To: ramesh [dot] mandaleeka [at] gmail [dot] com
CC: scala-user [at] googlegroups [dot] com

Scala doesn't have an andThenMap method, which is what you would need here for regular functions:

f: A => B
g: B => C
f andThen g: A => C

f: A => B
h: (B,B) => C
f andThenMap h: (A,A) => C

And you would need it to be smarter yet to handle partial functions, since if _either_ argument were undefined, then the whole thing would be undefined.

Generally, one resorts to options in this case (and even then, there isn't a map on tuples, so it won't do exactly what you want).

Better to just write it out each time, or write your own

def pfAndThenMap2[A,B,C](f: PF[A,B], g: PF[(B,B),C]): PF[(A,A),C] = {
case aa if (f.isDefinedAt(aa._1) && f.isDefinedAt(aa._2) && g.isDefinedAt(f(aa._1),f(aa._2))) => g(f(aa._1),f(aa._2))
}

--Rex

On Thu, Dec 29, 2011 at 12:00 PM, thinkingmind <ramesh [dot] mandaleeka [at] gmail [dot] com> wrote:
Hi,

I'm learning partial functions. And in that process I have two partial
functions as below:

import scala.{PartialFunction => PF}
val s2i: PF[String, Int] = { case s:String if s.matches("[+-]?\\d+")
=> Integer.parseInt(s) }
val mul: PF[(Int, Int), Int] = { case (a:Int, b:Int) => a * b}

With out using implicit conversions is there a way to do
multiplication of string representation of integer tuple to integer
using composition.

For example: mul("-2",  "-5") = 10  using s2i partial function.

Thanks,
Ramesh