witherable (empty) → 0.0
raw patch · 4 files changed
+113/−0 lines, 4 filesdep +basedep +containersdep +hashablesetup-changed
Dependencies added: base, containers, hashable, transformers, unordered-containers, vector
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- src/Data/Witherable.hs +56/−0
- witherable.cabal +25/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2014, Fumiaki Kinoshita + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of Fumiaki Kinoshita nor the names of other + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple +main = defaultMain
+ src/Data/Witherable.hs view
@@ -0,0 +1,56 @@+module Data.Witherable where +import qualified Data.Maybe as Maybe +import qualified Data.IntMap.Lazy as IM +import qualified Data.Map.Lazy as M +import qualified Data.Sequence as S +import qualified Data.Vector as V +import qualified Data.HashMap.Strict as HM +import Control.Applicative +import qualified Data.Traversable as T +import qualified Data.Foldable as F +import Data.Hashable +import Data.Functor.Identity + +-- | Like `traverse`, but you can remove elements instead of updating them. +-- @traverse f = wither (fmap Just . f)@ +-- Minimal complete definition: `wither` or `catMaybes`. +class T.Traversable t => Witherable t where + + wither :: Applicative f => (a -> f (Maybe b)) -> t a -> f (t b) + wither f = fmap catMaybes . T.traverse f + + catMaybes :: Witherable t => t (Maybe a) -> t a + catMaybes = runIdentity . wither pure + + witherM :: Monad m => (a -> m (Maybe b)) -> t a -> m (t b) + witherM f = unwrapMonad . wither (WrapMonad . f) + +-- | 'blightM' is 'witherM' with its arguments flipped. +blightM :: (Monad m, Witherable t) => t a -> (a -> m (Maybe b)) -> m (t b) +blightM = flip witherM +{-# INLINE blightM #-} + +instance Witherable Maybe where + wither _ Nothing = pure Nothing + wither f (Just a) = f a + +instance Witherable [] where + wither f = fmap Maybe.catMaybes . T.traverse f + +instance Witherable IM.IntMap where + wither f = fmap IM.fromList . wither (\(i, a) -> fmap ((,) i) <$> f a) . IM.toList + +instance Ord k => Witherable (M.Map k) where + wither f = fmap M.fromList . wither (\(i, a) -> fmap ((,) i) <$> f a) . M.toList + +instance (Eq k, Hashable k) => Witherable (HM.HashMap k) where + wither f = fmap HM.fromList . wither (\(i, a) -> fmap ((,) i) <$> f a) . HM.toList + +instance Witherable (Const r) where + wither _ (Const r) = pure (Const r) + +instance Witherable V.Vector where + wither f = fmap V.fromList . wither f . V.toList + +instance Witherable S.Seq where + wither f = fmap S.fromList . wither f . F.toList
+ witherable.cabal view
@@ -0,0 +1,25 @@+-- Initial witherable.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/ + +name: witherable +version: 0.0 +synopsis: Generalization of catMaybes +-- description: +homepage: https://github.com/fumieval/witherable +license: BSD3 +license-file: LICENSE +author: Fumiaki Kinoshita +maintainer: Fumiaki Kinoshita <fumiexcel@gmail.com> +copyright: Copyright (c) 2014 Fumiaki Kinoshita +category: Data +build-type: Simple +-- extra-source-files: +cabal-version: >=1.10 + +library + exposed-modules: Data.Witherable + -- other-modules: + -- other-extensions: + build-depends: base == 4.*, containers, vector, unordered-containers, hashable, transformers + hs-source-dirs: src + default-language: Haskell2010