Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- val data = List(("PO123", "P001", "Item 1", 10),
- ("PO123", "P001", "Item 1", 10),
- ("PO123", "P002", "Item 2", 30),
- ("PO123", "P002", "Item 2", 10))
- data.groupBy( product => (product._1, product._2, product._3))
- .map {
- case (productInfo, products) => {
- val total = products.foldLeft(0)((sum,elt) => sum + elt._4)
- (productInfo._1,productInfo._2,productInfo._3,total)
- }
- }
- scala.collection.immutable.Iterable[(String, String, String, Int)] =
- List((PO123,P001,Item 1,20), (PO123,P002,Item 2,40))
- case class Document(poID: String, productID: String, name: String, qty: Int)
- var list2 = list.groupBy(doc => doc.productID)
- list2.map(stringListTuple => stringListTuple._2.foldLeft(Document("","","",0)){
- (acc: Document,curr: Document) => Document(curr.poID, curr.productID, curr.name,curr.qty + acc.qty)
- }).toList.sortBy(el => el.productID)
- val in = Array(("PO123", "P001", "Item 1", 10), ("PO123", "P001", "Item 1", 10),
- ("PO123", "P002", "Item 2", 30), ("PO123", "P002", "Item 2", 10))
- in groupBy (p=>(p._1,p._2,p._3)) map { case(k,v) =>
- (k._1, k._2, k._3, (v map (_._4) sum )) }
- List((PO123,P001,Item 1,20), (PO123,P002,Item 2,40))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement