Can I avoid mutating in this scenario?

Naresh Krishnamoorthy

I am trying iterate an list and then doing an range check on each item and cumalating scores accordingly.

Pretty straight forward. I feel like i am doing this in more traditional way and creating lot of "var" variables..

Is there an effective functional/immutable way of achieving this behavior?

  var score_1 = 0
  var score_2 = 0
  var score_3 = 0
  var score_4 = 0
  var score_5 = 0

  val list = List(1,1,1,0.8,0.75,0.7,0.7,0.5,0.4,0.25,0.2,0.15,0.1)

  list.foreach( i => {
      i.toDouble match {
        case x if( x == 1.0 ) => score_1 += 1
        case x if( x >= 0.75 && x < 1 ) => score_2 += 1
        case x if( x >= 0.50 && x < 0.75) => score_3 += 1
        case x if( x >= 0.25 && x < 0.50) => score_4 += 1
        case x if( x >= 0 && x < 0.25 ) => score_5 += 1
        case _ => 
      }
    })

 println(score_1,score_2,score_3,score_4,score_5)
prayagupd

Yes you can achieve immutability using foldLeft,

case class Score(score1: Int,
                 score2: Int,
                 score3: Int,
                 score4: Int,
                 score5: Int,
                 score6: Int,
                 score7: Int,
                 score8: Int)

object Score {
  def empty = new Score(0, 0, 0, 0, 0, 0, 0, 0)
}

val myScore = list.foldLeft(Score.empty) {
  case (score, 1.0)                       => score.copy(score1 = score.score1 + 1)
  case (score, x) if x > 0.75 && x < 1    => score.copy(score2 = score.score2 + 1)
  case (score, 0.75)                      => score.copy(score3 = score.score3 + 1)
  case (score, x) if x > 0.50 && x < 0.75 => score.copy(score4 = score.score4 + 1)
  case (score, 0.50)                      => score.copy(score5 = score.score5 + 1)
  case (score, x) if x > 0.25 && x < 0.50 => score.copy(score6 = score.score6 + 1)
  case (score, 0.25)                      => score.copy(score7 = score.score7 + 1)
  case (score, x) if x >= 0 && x < 0.25   => score.copy(score8 = score.score8 + 1)
  case (score, _)                         => score
}

println(myScore) // Score(3,1,1,2,1,1,1,3)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Avoid mutating a prop : vuejs 2

分類Dev

Same asserts for every scenario can be put in a separate file to avoid duplication in karate?

分類Dev

Can I design any lock-free solution for this scenario

分類Dev

How can I avoid "environment hell" in postman?

分類Dev

How can I avoid losing precision with ftFmtBcd?

分類Dev

How can I avoid a circular reference situation

分類Dev

Can I avoid nested namespaces in forward declaration?

分類Dev

Can I avoid using a kubernetes volume

分類Dev

Django conditional queries: How can I avoid this?

分類Dev

Can I avoid repeat calculation in this query?

分類Dev

How can I avoid duplicate templates in Meteor?

分類Dev

How i can avoid the distinct() of watchdog?

分類Dev

How can I avoid problems with CPU power?

分類Dev

Vue 2 Avoid mutating a prop directly since the value will be overwritten

分類Dev

How can I avoid nested Navigation Bars in SwiftUI?

分類Dev

How can I avoid running some tests in parallel?

分類Dev

Can I avoid `DateTime::__construct` warning on invalid date?

分類Dev

How can I avoid Phusion Passenger running as root?

分類Dev

How can I avoid using 'at' in radial-gradient?

分類Dev

How can i avoid Jquery translate `?` to `%3F`

分類Dev

How can I avoid overwriting dynamodb from two lambdas?

分類Dev

EXC_BAD_ACCESS - How can I avoid it?

分類Dev

How can i avoid or pass over a directory that is access denied?

分類Dev

How can I avoid repeating the css and not using !important?

分類Dev

How can I avoid a black background when fading in an overlay with ffmpeg?

分類Dev

How can I avoid my program to keep running when I close my main frame?

分類Dev

How can I avoid Firefox's "you have chosen to open" dialog when I try to download a file?

分類Dev

How can i avoid a deadlock in Spring when creating a bean while triggering an application event?

分類Dev

How can i avoid a deadlock in Spring when creating a bean while triggering an application event?

Related 関連記事

  1. 1

    Avoid mutating a prop : vuejs 2

  2. 2

    Same asserts for every scenario can be put in a separate file to avoid duplication in karate?

  3. 3

    Can I design any lock-free solution for this scenario

  4. 4

    How can I avoid "environment hell" in postman?

  5. 5

    How can I avoid losing precision with ftFmtBcd?

  6. 6

    How can I avoid a circular reference situation

  7. 7

    Can I avoid nested namespaces in forward declaration?

  8. 8

    Can I avoid using a kubernetes volume

  9. 9

    Django conditional queries: How can I avoid this?

  10. 10

    Can I avoid repeat calculation in this query?

  11. 11

    How can I avoid duplicate templates in Meteor?

  12. 12

    How i can avoid the distinct() of watchdog?

  13. 13

    How can I avoid problems with CPU power?

  14. 14

    Vue 2 Avoid mutating a prop directly since the value will be overwritten

  15. 15

    How can I avoid nested Navigation Bars in SwiftUI?

  16. 16

    How can I avoid running some tests in parallel?

  17. 17

    Can I avoid `DateTime::__construct` warning on invalid date?

  18. 18

    How can I avoid Phusion Passenger running as root?

  19. 19

    How can I avoid using 'at' in radial-gradient?

  20. 20

    How can i avoid Jquery translate `?` to `%3F`

  21. 21

    How can I avoid overwriting dynamodb from two lambdas?

  22. 22

    EXC_BAD_ACCESS - How can I avoid it?

  23. 23

    How can i avoid or pass over a directory that is access denied?

  24. 24

    How can I avoid repeating the css and not using !important?

  25. 25

    How can I avoid a black background when fading in an overlay with ffmpeg?

  26. 26

    How can I avoid my program to keep running when I close my main frame?

  27. 27

    How can I avoid Firefox's "you have chosen to open" dialog when I try to download a file?

  28. 28

    How can i avoid a deadlock in Spring when creating a bean while triggering an application event?

  29. 29

    How can i avoid a deadlock in Spring when creating a bean while triggering an application event?

ホットタグ

アーカイブ