Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. val data = List(("PO123", "P001", "Item 1", 10),
  2. ("PO123", "P001", "Item 1", 10),
  3. ("PO123", "P002", "Item 2", 30),
  4. ("PO123", "P002", "Item 2", 10))
  5.  
  6. data.groupBy( product => (product._1, product._2, product._3))
  7. .map {
  8. case (productInfo, products) => {
  9. val total = products.foldLeft(0)((sum,elt) => sum + elt._4)
  10. (productInfo._1,productInfo._2,productInfo._3,total)
  11. }
  12. }
  13.  
  14. scala.collection.immutable.Iterable[(String, String, String, Int)] =
  15. List((PO123,P001,Item 1,20), (PO123,P002,Item 2,40))
  16.  
  17. case class Document(poID: String, productID: String, name: String, qty: Int)
  18.  
  19. var list2 = list.groupBy(doc => doc.productID)
  20.  
  21. list2.map(stringListTuple => stringListTuple._2.foldLeft(Document("","","",0)){
  22. (acc: Document,curr: Document) => Document(curr.poID, curr.productID, curr.name,curr.qty + acc.qty)
  23. }).toList.sortBy(el => el.productID)
  24.  
  25. val in = Array(("PO123", "P001", "Item 1", 10), ("PO123", "P001", "Item 1", 10),
  26. ("PO123", "P002", "Item 2", 30), ("PO123", "P002", "Item 2", 10))
  27.  
  28. in groupBy (p=>(p._1,p._2,p._3)) map { case(k,v) =>
  29. (k._1, k._2, k._3, (v map (_._4) sum )) }
  30.  
  31. List((PO123,P001,Item 1,20), (PO123,P002,Item 2,40))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement