Commits

Peter Sagerson committed 8a720a5

Fix example to correctly handle rereduce.

  • Participants
  • Parent commits 0aef603

Comments (0)

Files changed (1)

File source/Database/CouchDB/ViewServer.hs

     >\doc -> let net credits shares = let debts = shareAmounts (sumMap credits) (sumMap shares) shares
     >                                 in  M.unionWith (+) credits debts
     >
+    >            shareAmounts totCredit totShares = M.map (\shares -> -(shares / totShares) * totCredit)
     >            sumMap = M.fold (+) 0
-    >            shareAmounts totCredit totShares = M.map (\shares -> -(shares / totShares) * totCredit)
     >
     >        in  do date <- doc .: "date" :: ViewMap T.Text
     >               what <- doc .:? "what" :: ViewMap (Maybe T.Text) -- Optional field
     >
     >               emit date $ object ["net" .= net credits shares, "what" .= what]
 
-    >\_ values _ -> do objects <- parseJSONList values :: ViewReduce [J.Object]
-    >                  nets <- mapM (.: "net") objects :: ViewReduce [M.Map T.Text Double]
-    >                  return $ L.foldl' (M.unionWith (+)) M.empty nets
+    >\_ values rereduce -> L.foldl' (M.unionWith (+)) M.empty <$>
+    >    case rereduce of
+    >        False -> mapM (.: "net") =<< parseJSONList values :: ViewReduce [(M.Map T.Text Double)]
+    >        True  -> parseJSONList values :: ViewReduce [(M.Map T.Text Double)]
 
     Map results: