Skip to content

Instantly share code, notes, and snippets.

@yihuang
Created April 17, 2012 01:18
Show Gist options
  • Save yihuang/2402737 to your computer and use it in GitHub Desktop.
Save yihuang/2402737 to your computer and use it in GitHub Desktop.
adjust nested data structure
module Test where
import qualified Data.Vector as V
import qualified Data.HashMap.Strict as M
import Data.Hashable (Hashable)
type MyData k v = V.Vector (M.HashMap k v)
adjustVec :: Int -> (a -> a) -> V.Vector a -> V.Vector a
adjustVec ix f vec =
let v = vec V.! ix
in vec V.// [(ix, f v)]
modify :: (Eq k, Hashable k) => Int -> k -> v -> MyData k v -> MyData k v
modify idx k v = adjustVec idx (M.insert k v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment