Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package FP.Intro
- case class CreditCard(Name: String){}
- class Coffee
- {
- val price = 10 //instance variable
- override def toString = "Price of Coffee: " + price
- }
- case class Charge(cc: CreditCard, amount: Double)
- {
- // combine charges with the same CreditCard
- def combine(other: Charge): Charge =
- {
- if(cc == other.cc)
- {
- Charge(cc, this.amount + other.amount)
- }
- else
- {
- throw new Exception("Credit card does not match.")
- }
- }
- // end of combine method
- }
- // end of Charge class
- class Cafe
- {
- /* buyCoffee (with side effect)
- def buyCoffee(cc: CreditCard): Coffee =
- {
- val cup = new Coffee()
- // Process the charge
- cc.charge(cup.price)
- cup.
- }
- // end of buyCoffee
- */
- /* Non side-effect version */
- def buyCoffee(cc: CreditCard): (Coffee, Charge) =
- {
- val cup = new Coffee()
- (cup, Charge(cc, cup.price))
- }
- // end of buyCoffee
- // How does this let us reuse buyCoffee more easily to purchase multiple coffees
- // with a single transaction?
- def buyCoffees(cc: CreditCard, n: Int): (List[Coffee], Charge) =
- {
- val purchases: List[(Coffee, Charge)] = List.fill(n)(buyCoffee(cc))
- // unzip splits a list of pairs into a pair of lists
- val (coffeeList, chargeList) = purchases.unzip
- // reduces the entire list of Charges to a single Charge, using combine method
- // to combine Charges two at a time.
- (coffeeList, chargeList.reduce((c1, c2) =>
- {
- // I added a tab for better output formatting and made it println() as well
- println("c1-" + c1 + "\tc2-" + c2)
- println(" combine results: " + c1.combine(c2))
- c1.combine(c2)
- }))
- }
- // end of buyCoffees method
- }
- // end of Cafe class
- object Service
- {
- // Since Charge is first-class, we can write the following function to
- // coalesce any same-card charges in a List[Charge].
- // This function takes a list of Charges, groups them by the credit card used,
- // and then combines them into a single Charge per card.
- def coalesce(charges: List[Charge]): List[Charge] =
- {
- // group the Charges by credit card
- // val groupByResult = charges.groupBy(_.cc)
- // println("groupByResult" + groupByResult)
- // println()
- // val valuesResult = groupByResult.values
- // println("valuesResult: " + valuesResult)
- // println()
- // val mapResult = valuesResult.map(_.reduce(_ combine _))
- // println("mapResult: " + mapResult)
- // println()
- charges.groupBy(_.cc)
- .values
- .map(_.reduce(_ combine _))
- .toList
- // charges.groupBy(_.cc)
- // .values
- // .map(_.reduce(_ combine _ ))
- // .toList
- }
- // end of coalesce method
- }
- object TestIntro
- {
- def main(args: Array[String]): Unit =
- {
- println("buyCoffee running")
- val mycafe = new Cafe()
- val (myCoffee, myCharge) = mycafe.buyCoffee(CreditCard("Visa"))
- println("Result: " + myCoffee.price + " " + myCharge)
- println()
- println("combining charge running")
- val combineCharge = Charge(CreditCard("Visa"), 20.0).combine(Charge(CreditCard("Visa"), 50.0))
- println("Result: " + combineCharge)
- println()
- println(" buyCoffes running")
- val mycafe2 = new Cafe()
- println("Result: " + mycafe2.buyCoffees(CreditCard("Visa"), 5))
- println()
- println(" coalesce running")
- val visa = CreditCard("Visa")
- val ae = CreditCard("American Express")
- val listOfCharge = List(Charge(visa, 10.0), Charge(ae, 60.0), Charge(visa, 20.0), Charge(ae, 70.0),
- Charge(visa, 30.0))
- println()
- println("result: " + Service.coalesce(listOfCharge))
- println()
- val x = "Hello World"
- val r1 = x.reverse
- val r2 = x.reverse
- println("String before replacing: " + x)
- println("r1: " + r1)
- println("r2: " + r2)
- println()
- // suppose we replace all occurrences of the term x with the expression
- // reference by x (its definition) as follows:
- val r3 = "Hello World".reverse
- val r4 = "Hello World".reverse
- println("String after replacing")
- println("r3: " + r3)
- println("r4: " + r4)
- println()
- // check StringBuilder
- val x1 = new StringBuilder("Hello")
- val y1 = x1.append(", World")
- val r5 = y1.toString
- val r6 = y1.toString
- println("StringBuilder before replacing: " + x1)
- println("r5: " + r5)
- println("r6: " + r6)
- println()
- // substitute the result of call with the call
- val x2 = new StringBuilder("Hello")
- val r7 = x2.append(", World").toString // x2 is mutated
- val r8 = x2.append(", World").toString
- println("StringBuilder before replacing: " + x2)
- println("r7: " + r7)
- println("r8: " + r8)
- println()
- }
- // end of main
- }
- // end of TestIntro
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement