From 741f99212e6521c1dd4c2e62012028fc17d52ff1 Mon Sep 17 00:00:00 2001 From: Marc Sunet Date: Thu, 9 Aug 2012 13:32:25 +0200 Subject: Added withElement, setElement, and made Store a Functor --- Spear/Sys/Store.hs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Spear/Sys/Store.hs b/Spear/Sys/Store.hs index 65381ca..3c1e720 100644 --- a/Spear/Sys/Store.hs +++ b/Spear/Sys/Store.hs @@ -8,6 +8,8 @@ module Spear.Sys.Store , storeFree , storeFreel , element +, setElement +, withElement ) where @@ -29,6 +31,10 @@ data Store a = Store deriving Show +instance Functor Store where + fmap f (Store objects last) = Store (fmap (fmap f) objects) last + + -- | Create an empty store. emptyStore :: Store a emptyStore = Store V.empty (-1) @@ -135,6 +141,21 @@ element :: Index -> Store a -> Maybe a element index (Store objects _) = objects V.! index +-- | Set the element in the given slot. +setElement :: Index -> a -> Store a -> Store a +setElement index elem s = s { objects = objects s // [(index,Just elem)] } + + +-- | Apply a function to the element in the given slot. +withElement :: Index -> Store a -> (a -> a) -> Store a +withElement index store f = store { objects = objects' } + where + objects' = objects store // [(index, obj')] + obj' = case element index store of + Nothing -> Nothing + Just x -> Just $ f x + + -- test test :: IO () test = evalStateT test' emptyStore -- cgit v1.2.3