Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 5.27 KB | None | 0 0
  1. package FP.Intro
  2.  
  3. case class CreditCard(Name: String){}
  4.  
  5. class Coffee
  6. {
  7.     val price = 10 //instance variable
  8.  
  9.     override def toString = "Price of Coffee: " + price
  10. }
  11.  
  12. case class Charge(cc: CreditCard, amount: Double)
  13. {
  14.     // combine charges with the same CreditCard
  15.     def combine(other: Charge): Charge =
  16.     {
  17.         if(cc == other.cc)
  18.         {
  19.             Charge(cc, this.amount + other.amount)
  20.         }
  21.         else
  22.         {
  23.         throw new Exception("Credit card does not match.")
  24.         }
  25.     }
  26.     // end of combine method
  27. }
  28. // end of Charge class
  29.  
  30. class Cafe
  31. {
  32.     /* buyCoffee (with side effect)
  33.       def buyCoffee(cc: CreditCard): Coffee =
  34.       {
  35.             val cup = new Coffee()
  36.             // Process the charge
  37.             cc.charge(cup.price)
  38.             cup.
  39.       }
  40.       // end of buyCoffee
  41.       */
  42.  
  43.   /* Non side-effect version */
  44.   def buyCoffee(cc: CreditCard): (Coffee, Charge) =
  45.     {
  46.         val cup = new Coffee()
  47.         (cup, Charge(cc, cup.price))
  48.     }
  49.   // end of buyCoffee
  50.  
  51.   // How does this let us reuse buyCoffee more easily to purchase multiple coffees
  52.   // with a single transaction?
  53.   def buyCoffees(cc: CreditCard, n: Int): (List[Coffee], Charge) =
  54.     {
  55.         val purchases: List[(Coffee, Charge)] = List.fill(n)(buyCoffee(cc))
  56.  
  57.         // unzip splits a list of pairs into a pair of lists
  58.         val (coffeeList, chargeList) = purchases.unzip
  59.  
  60.         // reduces the entire list of Charges to a single Charge, using combine method
  61.         // to combine Charges two at a time.
  62.         (coffeeList, chargeList.reduce((c1, c2) =>
  63.         {
  64.       // I added a tab for better output formatting and made it println() as well
  65.             println("c1-" + c1 + "\tc2-" + c2)
  66.             println(" combine results: " + c1.combine(c2))
  67.             c1.combine(c2)
  68.         }))
  69.     }
  70.   // end of buyCoffees method
  71. }
  72. // end of Cafe class
  73.  
  74. object Service
  75. {
  76.     // Since Charge is first-class, we can write the following function to
  77.     // coalesce any same-card charges in a List[Charge].
  78.     // This function takes a list of Charges, groups them by the credit card used,
  79.     // and then combines them into a single Charge per card.
  80.  
  81.     def coalesce(charges: List[Charge]): List[Charge] =
  82.     {
  83.         // group the Charges by credit card
  84.         // val groupByResult = charges.groupBy(_.cc)
  85.         // println("groupByResult" + groupByResult)
  86.         // println()
  87.  
  88.         // val valuesResult = groupByResult.values
  89.         // println("valuesResult: " + valuesResult)
  90.         // println()
  91.  
  92.         // val mapResult = valuesResult.map(_.reduce(_ combine _))
  93.         // println("mapResult: " + mapResult)
  94.         // println()
  95.  
  96.     charges.groupBy(_.cc)
  97.         .values
  98.         .map(_.reduce(_ combine _))
  99.         .toList
  100.  
  101. //        charges.groupBy(_.cc)
  102. //        .values
  103. //        .map(_.reduce(_ combine _ ))
  104. //        .toList
  105.  
  106.     }
  107.     // end of coalesce method
  108.    
  109. }
  110.  
  111.  
  112. object TestIntro
  113. {
  114.     def main(args: Array[String]): Unit =
  115.     {
  116.         println("buyCoffee running")
  117.         val mycafe = new Cafe()
  118.         val (myCoffee, myCharge) = mycafe.buyCoffee(CreditCard("Visa"))
  119.         println("Result: " + myCoffee.price + " " + myCharge)
  120.         println()
  121.  
  122.         println("combining charge running")
  123.         val combineCharge = Charge(CreditCard("Visa"), 20.0).combine(Charge(CreditCard("Visa"), 50.0))
  124.         println("Result: " +  combineCharge)
  125.         println()
  126.  
  127.         println(" buyCoffes running")
  128.         val mycafe2 = new Cafe()
  129.         println("Result: " + mycafe2.buyCoffees(CreditCard("Visa"), 5))
  130.         println()
  131.  
  132.         println(" coalesce running")
  133.         val visa = CreditCard("Visa")
  134.         val ae = CreditCard("American Express")
  135.         val listOfCharge = List(Charge(visa, 10.0), Charge(ae, 60.0), Charge(visa, 20.0), Charge(ae, 70.0),
  136.                                 Charge(visa, 30.0))
  137.  
  138.         println()
  139.         println("result: " + Service.coalesce(listOfCharge))
  140.         println()
  141.  
  142.         val x = "Hello World"
  143.         val r1 = x.reverse
  144.         val r2 = x.reverse
  145.         println("String before replacing: " + x)
  146.         println("r1: " + r1)
  147.         println("r2: " + r2)
  148.         println()
  149.  
  150.         // suppose we replace all occurrences of the term x with the expression
  151.         // reference by x (its definition) as follows:
  152.         val r3 = "Hello World".reverse
  153.         val r4 = "Hello World".reverse
  154.         println("String after replacing")
  155.         println("r3: " + r3)
  156.         println("r4: " + r4)
  157.         println()
  158.  
  159.         // check StringBuilder
  160.         val x1 = new StringBuilder("Hello")
  161.         val y1 = x1.append(", World")
  162.         val r5 = y1.toString
  163.         val r6 = y1.toString
  164.  
  165.         println("StringBuilder before replacing: " + x1)
  166.         println("r5: " + r5)
  167.         println("r6: " + r6)
  168.         println()
  169.  
  170.         // substitute the result of call with the call
  171.         val x2 = new StringBuilder("Hello")
  172.         val r7 = x2.append(", World").toString // x2 is mutated
  173.         val r8 = x2.append(", World").toString
  174.  
  175.         println("StringBuilder before replacing: " + x2)
  176.         println("r7: " + r7)
  177.         println("r8: " + r8)
  178.         println()
  179.  
  180.    
  181.     }
  182.   // end of main
  183. }
  184. // end of TestIntro
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement