Created
August 17, 2016 00:37
-
-
Save schrepfler/e1742709f1f2f9020cdcc2865ad16a0d to your computer and use it in GitHub Desktop.
GCounter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Data.Foldable (fold) | |
import Data.Map (Map) | |
import Data.Map as Map | |
import Data.Maybe (Maybe(Just, Nothing)) | |
import Data.Monoid (mempty, class Monoid) | |
import Prelude | |
data GCounter n = GCounter String (Map String n) | |
increment :: forall n | |
. (Monoid n, Ord n) | |
=> n | |
-> GCounter n | |
-> Maybe (GCounter n) | |
increment amt (GCounter shardId payload) | |
| amt < mempty = Nothing | |
| otherwise = let sum = amt <> fold (Map.lookup shardId payload) | |
in Just $ GCounter shardId (Map.insert shardId sum payload) | |
value :: forall n. (Monoid n) => GCounter n -> n | |
value (GCounter _ payload) = fold payload | |
merge :: forall n. String -> Int | |
merge n = if true then 0 else 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment