Skip to content

Instantly share code, notes, and snippets.

@vlas-ilya
Created October 15, 2015 11:32
Show Gist options
  • Save vlas-ilya/892472450be3f68ccb90 to your computer and use it in GitHub Desktop.
Save vlas-ilya/892472450be3f68ccb90 to your computer and use it in GitHub Desktop.
import Prelude hiding (lookup)
class MapLike m where
empty :: m k v
lookup :: Ord k => k -> m k v -> Maybe v
insert :: Ord k => k -> v -> m k v -> m k v
delete :: Ord k => k -> m k v -> m k v
fromList :: Ord k => [(k,v)] -> m k v
newtype ArrowMap k v = ArrowMap { getArrowMap :: k -> Maybe v }
instance MapLike ArrowMap where
empty = ArrowMap (\_ -> Nothing)
lookup k (ArrowMap f) = f k
insert k v (ArrowMap f) = ArrowMap (\k' -> if k' == k then Just v else f k')
delete k (ArrowMap f) = ArrowMap (\k' -> if k' == k then Nothing else f k')
fromList [] = empty
fromList ((k,v):xs) = insert k v (fromList xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment