Scalaz Map [String, int] "Summation"및 Poldleft가 작동하지 않습니다.

StackOverflow https://stackoverflow.com//questions/20020101

  •  21-12-2019
  •  | 
  •  

문제

Scalaz를 사용하여 다음과 같은 기능이 있습니다. + |운영자 :

  def sumGames(games: List[Map[String, Int]]) =
    games.foldLeft(_ |+| _)
.

두 개의 맵 추가 수동으로 완벽하게 작동합니다 (A :지도 [문자열, int) | + |B : 맵 [String, int]). 그러나 위 함수를 선언하면 3 가지 오류가 발생합니다.

<console>:20: error: missing parameter type for expanded function ((x$1, x$2) =>
 x$1.$bar$plus$bar(x$2))
           games.foldLeft(_ |+| _)
                          ^
<console>:20: error: missing parameter type for expanded function ((x$1: <error>
, x$2) => x$1.$bar$plus$bar(x$2))
           games.foldLeft(_ |+| _)
                                ^
<console>:20: error: missing arguments for method foldLeft in trait LinearSeqOpt
imized;
follow this method with `_' if you want to treat it as a partially applied funct
ion
           games.foldLeft(_ |+| _)
.

이 작업이하지 않는 이유와 어떻게 해결할 수 있습니까?

도움이 되었습니까?

해결책

foldLeft api , 두 개의 인수 목록이 필요합니다.첫 번째는 폴드의 초기 값 인 두 번째로 환원 기능입니다.

아마 당신이 원하는 것 같아

import scalaz._
import Scalaz._

def sumGames(games: List[Map[String, Int]]) = 
  games.foldLeft(Map.empty[String, Int])(_ |+| _)
.

목록이 비어 있지 않다는 것을 알고 있으면 games.reduce(_ |+| _)를 사용할 수도 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top