git.iko.soy/typeable/validationt
on master help sign in
Fixed unexpected behaviour
359e531iko
src/Control/Monad/Validation.hs+7−14
@@ -81,25 +81,18 @@ mmAppend (MonoidMap a) (MonoidMap b) = MonoidMap $ M.unionWith (<>) a b
81-- | Convenient for 'vZoom' as first artument. Will prevent generation
81-- | Convenient for 'vZoom' as first artument. Will prevent generation
82-- of map with 'mempty' values
82-- of map with 'mempty' values
83mmSingleton :: (Eq v, Monoid v, Ord k) => k -> v -> MonoidMap k v
83mmSingleton :: (Eq v, Monoid v, Ord k) => k -> v -> MonoidMap k v
84+mmSingleton k v
85+ | v == mempty = mempty
84−mmSingleton k = memptyWrap mempty $ MonoidMap . M.singleton k
86+ | otherwise = MonoidMap . M.singleton k $ v
85
87
86-- | Set given value to 'mempty'
88-- | Set given value to 'mempty'
87setMempty :: (Monoid s) => ASetter' s a -> a -> s
89setMempty :: (Monoid s) => ASetter' s a -> a -> s
88setMempty setter a = set setter a mempty
90setMempty setter a = set setter a mempty
89
91
90−memptyWrap :: (Eq a, Monoid a) => b -> (a -> b) -> a -> b
92+neConcat :: Foldable f => (a -> a -> a) -> f a -> Maybe a
91−memptyWrap b f a
93+neConcat f a
92− | a == mempty = b
94+ | F.null a = Nothing
93− | otherwise = f a
95+ | otherwise = Just $ F.foldl1 f a
94−
95−-- | If given container is not 'mempty', then use given function to
96−-- append all its elements and return 'Just' result
97−neConcat
98− :: (Foldable f, Eq (f a), Monoid a, Monoid (f a))
99− => (a -> a -> a)
100− -> f a
101− -> Maybe a
102−neConcat f = memptyWrap Nothing (Just . F.foldl' f mempty)
103
96
104textErrors :: [Text] -> Maybe Text
97textErrors :: [Text] -> Maybe Text
105textErrors = neConcat (\a b -> a <> ", " <> b)
98textErrors = neConcat (\a b -> a <> ", " <> b)