Skip to content

Instantly share code, notes, and snippets.

@yasuabe
Created May 12, 2017 12:43
Show Gist options
  • Save yasuabe/9dc12ccf3e0b0af2cd564ec60bcf575b to your computer and use it in GitHub Desktop.
Save yasuabe/9dc12ccf3e0b0af2cd564ec60bcf575b to your computer and use it in GitHub Desktop.
haskell implementation of simpath: CounterMap
module Simpath.CounterMap where
import Data.Map (Map)
import qualified Data.Map as Map
import qualified Data.Foldable as Foldable
import Data.Function
import qualified Simpath.Frontier as F
import Simpath.Frontier (Frontier)
import Simpath.Border (Border)
type CounterMap = Map Frontier Integer
merge :: Integer -> CounterMap -> Frontier -> CounterMap
merge cnt cm fr = Map.insertWith (+) fr cnt cm
proceedAll :: Border -> CounterMap -> CounterMap
proceedAll b = Foldable.foldl f Map.empty . Map.toList
where f cm (fr, cnt) = on (.) recount lo hi cm
where (hi, lo) = F.proceed b fr
recount = flip $ Foldable.foldl $ merge cnt
countPaths :: [Border] -> Integer
countPaths = headCount . foldl (flip proceedAll) initialMap
where initialMap = Map.singleton F.initial 1
headCount = head . Map.elems
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment