Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 262963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:33:42+00:00 2026-05-11T22:33:42+00:00

Starting with a list of objects containing two parameters notional and currency, how can

  • 0

Starting with a list of objects containing two parameters notional and currency, how can I aggregate the total notional per currency?

Given:

case class Trade(name: String, amount: Int, currency: String)

val trades = List(
  Trade("T150310", 10000000, "GBP"),
  Trade("T150311", 10000000, "JPY"),
  Trade("T150312", 10000000, "USD"),
  Trade("T150313", 100, "JPY"),
  Trade("T150314", 1000, "GBP"),
  Trade("T150315", 10000, "USD")
)

How can I get:

Map(JPY -> 10000100, USD -> 10010000, GBP -> 10001000)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:33:43+00:00Added an answer on May 11, 2026 at 10:33 pm

    I wrote a simple group-by operation (actually a Groupable trait with an implicit conversion from an Iterable) which would allow you to group your trades by their currency:

    trait Groupable[V] extends Iterable[V] {
      def groupBy(f: V => K): MultiMap[K, V] = {
        val m = new mutable.HashMap[K, Set[V]] with mutable.MultiMap[K, V]
        foreach { v => m add (f(v), v) } //add is defined in MultiMap
        m
      }
    }
    implicit def it2groupable(it: Iterable[V]): Groupable[V] = new Groupable[V] {
      def elements = it.elements
    }
    

    So Groupable is simply providing a way to extract a key from each item in an Iterable and then grouping all such items which have the same key. So, in your case:

    //mm is a MultiMap[Currency, Trade]
    val mm = trades groupBy { _.currency } 
    

    You can now do a quite simple mapElements (mm is a Map) and a foldLeft (or /: – well worth understanding the foldLeft operator as it enables extremely concise aggregations over collections) to get the sum:

    val sums: Map[Currency, Int] = mm mapElements { ts => 
        (0 /: ts) { (sum,t) => sum + t.notional } 
    }
    

    Apologies if I’ve made some mistakes in that last line. ts are the values of mm, which are (of course) Iterable[Trade].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list in C#. Given two number - the starting position and
Im trying to return a list to a display template containing data from two
I have two identically-sized numpy.array objects (both one-dimensional), one of which contains a list
The short version: How can I get a list of all the objects (including
Starting with a simple list: <ul> <li>Item 1</li> <li style=display: none;>Item 2</li> <li>Item 3</li>
I'd like to list the items in a tuple in Python starting with the
I am starting out with Silverlight. I want to display a list of messages
I'm .net newbie starting one project for fun. I'm using list box filled with
I'm just starting with programming. I have a list of a few strings and
I am starting with this object, public class myTO { private String id; private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.