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 3787180
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:48:03+00:00 2026-05-19T11:48:03+00:00

Hi How to transform this for (w <- m ){ val w = (w._2.collect

  • 0

Hi
How to transform this

for (w <- m ){ val w = (w._2.collect { case x if (x._2 > 0) => x._2; case x if (x._2 < 0) => x._2 }) // if I add here .sum i got sum of (negative and positive) together }


to get sum of positive and sum of negative values in one collect, it could be List(positive.sum,negative.sum) or two values

edit:
only groupby, slice, collect, sum and yield

I wrote working program, but it' was not acepted, beacause it doing two collect


val m = d.groupBy(_._1.slice(0, 7))
    for (w<- m) {
     val x = (w._2.collect { case x if (x._2>  0) =>  x._2 }).sum
     val y = (w._2.collect { case x if (x._2<  0) =>  x._2 }).sum
      println("%7s %11.2f %11.2f %11.2f" format(w._1 , x , y ,(x+y)))
    }
}


entry data are

val d = List(("2011-01-04", -137.76), ("2011-01-04", 2376.45), ("2011-01-04", -1.70), ("2011-01-04", -1.70), ("2011-01-04", -1.00), ("2011-01-06", 865.70), ("2011-01-07", -734.15), ("2011-01-05", -188.63), ("2011-01-06", -73.50), ("2011-01-07", -200.00), ("2011-01-09", -215.35), ("2011-01-09", -8.86), ("2011-01-09", -300.00), ("2011-01-11", -634.54), ("2011-01-11", -400.00), ("2011-01-12", -92.87), ("2011-01-13", -1839.24), ("2011-01-13", 10000.00), ("2011-01-13", -10000.00), ("2011-01-15", -127.97), ("2011-01-15", -319.02), ("2011-01-19", -549.00), ("2011-01-21", -164.80), ("2011-01-23", -500.00), ("2011-01-25", -377.97), ("2011-01-26", 2158.66), ("2011-01-26", -130.45), ("2011-01-27", -350.00), ("2011-01-29", -500.00), ("2011-02-01", 2376.45), ("2011-02-01", 955.00))

  • 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-19T11:48:04+00:00Added an answer on May 19, 2026 at 11:48 am

    I recognise this homework 🙂

    So it looks like m is a Map, and you don’t much care about the keys in the output (perhaps you already used filterKeys by this point), so probably easiest to just pull out the values then filter – avoid all those tedious tuples and their underscores…

    val values = m.values
    val positives = values filter { _ >= 0 }
    val negatives = values filter { _ < 0 }
    

    Or if you prefer, this can be tidied up (and made more efficient) using the partition method:

    val (positives,negatives) = m.values partition { _ >= 0 }
    

    or even use the co-called “point free” style, but that might be pushing it too far:

    val (positives,negatives) = m.values partition { 0 < }
    

    You shouldn’t have any problem now figuring out what to do with positives and negatives

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

Sidebar

Related Questions

I'm trying to transform this URL: http://localhost/?q=foo&s=search on this: http://localhost/?q=+foo&s=search (i add a plus
Given the following sequence: normal %(b)BOLD% normal I want to transform this into the
I've tried this: CATransform3D rotationTransform = [[self.layer presentationLayer] transform]; This will not work, since
Any idea why this code: extern C __declspec(dllexport) void Transform(double x[], double y[], int
I need to transform my nested sets structure (mysql) into json for this spacetree
Wikipedia's Wavelet article contains this text: The discrete wavelet transform is also less computationally
This code: (self is a subclass of NSView) rotate = [CABasicAnimation animationWithKeyPath:@transform.rotation.z]; rotate.duration =
I'm seeing this exception message coming from XslCompiledTransform.Transform(), but after handling the exception the
I have this Javascript data: [{id:123,type:test},{id:154,type:another}] How would you transform that into something so
I want to transform an XML document. The source XML looks like this: <svc:ElementList>

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.