both (empty) → 0.1.0.0
raw patch · 4 files changed
+114/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- Data/Both.hs +39/−0
- LICENSE +20/−0
- Setup.hs +2/−0
- both.cabal +53/−0
+ Data/Both.hs view
@@ -0,0 +1,39 @@+{-# LANGUAGE DeriveDataTypeable #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE DeriveTraversable #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}++-- | The 'Both' type and operations. Like 'Maybe', but not.+module Data.Both where++import Control.Applicative+import Control.Monad+import Data.Data+import Data.Foldable+import Data.Maybe+import Data.Monoid+import Data.Traversable+import GHC.Generics++newtype Both a = Both { getBoth :: Maybe a }+ deriving (Eq, Ord, Read, Show, Data, Typeable, Generic, Generic1, Functor, Applicative, Alternative, Monad, MonadPlus, Foldable, Traversable)++-- | The 'mappend' for 'Maybe' is 'Just' if /either/ of the operands+-- are, whereas here /both/ must be.+instance Monoid a => Monoid (Both a) where+ mempty = Both $ Just mempty+ mappend (Both (Just x)) (Both (Just y)) = Both . Just $ x <> y+ mappend _ _ = Both Nothing++-- | The 'both' function takes a default value, a function, and a+-- 'Both' value. If the inner 'Maybe' value is 'Nothing', the function+-- returns the default value. Otherwise, it applies the function to+-- the value inside the 'Just' and returns the result.+both :: b -> (a -> b) -> Both a -> b+both z f = maybe z f . getBoth++-- | The 'fromBoth' function takes a default value and a 'Both'+-- value. If the inner 'Maybe' is 'Nothing', it returns the default+-- value; otherwise, it returns the value contained within.+fromBoth :: a -> Both a -> a+fromBoth z = fromMaybe z . getBoth
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2015, Michael Walker <mike@barrucadu.co.uk>++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be+included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ both.cabal view
@@ -0,0 +1,53 @@+-- Initial both.cabal generated by cabal init. For further documentation, +-- see http://haskell.org/cabal/users-guide/++name: both+version: 0.1.0.0+synopsis: Like Maybe, but with a different Monoid instance.+description:+ The Monoid instance for Maybe behaves like so:+ .+ > instance Monoid a => Monoid (Maybe a) where+ > mappend (Just x) (Just y) = Just $ x <> y+ > mappend (Just x) Nothing = Just x+ > mappend Nothing (Just y) = Just y+ > mappend Nothing Nothing = Nothing+ >+ > mempty = Nothing+ .+ Both is a newtype wrapper around Maybe providing this instance:+ .+ > instance Monoid a => Monoid (Both a) where+ > mappend (Just x) (Just y) = Just $ x <> y+ > mappend _ _ = Nothing+ >+ > mempty = Just mempty++homepage: https://github.com/barrucadu/both+bug-reports: https://github.com/barrucadu/both/issues+license: MIT+license-file: LICENSE+author: Michael Walker+maintainer: mike@barrucadu.co.uk+-- copyright: +category: Data+build-type: Simple+-- extra-source-files: +cabal-version: >=1.10++source-repository head+ type: git+ location: https://github.com/barrucadu/both.git++source-repository this+ type: git+ location: https://github.com/barrucadu/both.git+ tag: 0.1.0.0++library+ exposed-modules: Data.Both+ -- other-modules: + -- other-extensions: + build-depends: base >=4.7 && <4.9+ -- hs-source-dirs: + default-language: Haskell2010