Skip to content

Instantly share code, notes, and snippets.

@schrepfler
Created August 17, 2016 00:37
Show Gist options
  • Save schrepfler/e1742709f1f2f9020cdcc2865ad16a0d to your computer and use it in GitHub Desktop.
Save schrepfler/e1742709f1f2f9020cdcc2865ad16a0d to your computer and use it in GitHub Desktop.
GCounter
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