{-# language InstanceSigs #-}
{-# language ScopedTypeVariables #-}
module Data.Map.NonEmpty(
NonEmptyMap(..)
, singleton
, fromList
, insert
, insertWith
, insertWithKey
, insertLookupWithKey
, delete
, adjust
, update
, alter
, alterF
, lookup
, (!?)
, findWithDefault
, member
, notMember
, size
, toList
) where
import qualified Data.Map as Map
import Data.Maybe (fromMaybe, isJust)
import Prelude hiding (lookup)
data NonEmptyMap k a = NonEmptyMap (k, a) (Map.Map k a)
instance Functor (NonEmptyMap k) where
fmap :: (a -> b) -> NonEmptyMap k a -> NonEmptyMap k b
fmap f (NonEmptyMap (k, v) map) = NonEmptyMap (k, f v) (fmap f map)
singleton :: (k, a) -> NonEmptyMap k a
singleton tup = NonEmptyMap tup Map.empty
fromList :: Ord k => [(k, a)] -> Maybe (NonEmptyMap k a)
fromList [] = Nothing
fromList (x : xa) = Just $ NonEmptyMap x (Map.fromList xa)
insert :: Ord k => k -> a -> NonEmptyMap k a -> NonEmptyMap k a
insert = insertWith const
insertWith :: Ord k => (a -> a -> a) -> k -> a -> NonEmptyMap k a -> NonEmptyMap k a
insertWith f key value (NonEmptyMap (k, a) m) | key == k = NonEmptyMap (key, f value a) m
insertWith f key value (NonEmptyMap (k, a) m) = NonEmptyMap (k, a) (Map.insertWith f key value m)
insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> NonEmptyMap k a -> NonEmptyMap k a
insertWithKey f key value (NonEmptyMap (k, a) m) =
if k == key then NonEmptyMap (key, f key value a) m
else NonEmptyMap (k, a) (Map.insertWithKey f key value m)
insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> NonEmptyMap k a -> (Maybe a, NonEmptyMap k a)
insertLookupWithKey f key value (NonEmptyMap (k, a) m) =
if k == key then (Just a, NonEmptyMap(key, f key value a) m)
else fmap (NonEmptyMap (k, a)) (Map.insertLookupWithKey f key value m)
delete :: Ord k => k -> NonEmptyMap k a -> Map.Map k a
delete key (NonEmptyMap (k, a) m) | key == k = m
delete key (NonEmptyMap (k, a) m) = Map.insert k a (Map.delete k m)
adjust :: Ord k => (a -> a) -> k -> NonEmptyMap k a -> NonEmptyMap k a
adjust f key (NonEmptyMap (k, a) m) | key == k = NonEmptyMap (key, f a) m
adjust f key (NonEmptyMap (k, a) m) = NonEmptyMap (k, a) (Map.adjust f key m)
update :: Ord k => (a -> Maybe a) -> k -> NonEmptyMap k a -> Map.Map k a
update f key (NonEmptyMap (k, a) m) | key == k = case f a of
Just a -> Map.insert k a m
Nothing -> m
update f key (NonEmptyMap (k, a) m) = Map.insert k a (Map.update f key m)
alter :: Ord k => (Maybe a -> Maybe a) -> k -> NonEmptyMap k a -> Map.Map k a
alter f key (NonEmptyMap (k, a) m) | key == k = case f (Just a) of
Just a -> Map.insert k a m
Nothing -> m
alter f key (NonEmptyMap (k, a) m) = Map.insert k a (Map.alter f key m)
alterF :: forall f k a. (Functor f, Ord k) => (Maybe a -> f (Maybe a)) -> k -> NonEmptyMap k a -> f (Map.Map k a)
alterF f key (NonEmptyMap (k, a) m) | key == k = insideF <$> f (Just a)
where
insideF :: Maybe a -> Map.Map k a
insideF (Just a) = Map.insert k a m
insideF Nothing = m
alterF f key (NonEmptyMap (k, a) m) = Map.insert k a <$> Map.alterF f key m
lookup :: Ord k => k -> NonEmptyMap k a -> Maybe a
lookup key (NonEmptyMap (k, a) m) | key == k = Just a
lookup key (NonEmptyMap _ m) = Map.lookup key m
(!?) :: Ord k => NonEmptyMap k a -> k -> Maybe a
(!?) nem k = lookup k nem
findWithDefault :: Ord k => a -> k -> NonEmptyMap k a -> a
findWithDefault a key nem = fromMaybe a (lookup key nem)
member :: Ord k => k -> NonEmptyMap k a -> Bool
member key nem = isJust (lookup key nem)
notMember :: Ord k => k -> NonEmptyMap k a -> Bool
notMember k nem = not $ member k nem
size :: NonEmptyMap k a -> Int
size (NonEmptyMap _ m) = 1 + Map.size m
toList :: NonEmptyMap k a -> [(k, a)]
toList (NonEmptyMap tup m) = tup : Map.toList m