ord-adhoc (empty) → 0.0.0.1
raw patch · 5 files changed
+146/−0 lines, 5 filesdep +basedep +voidsetup-changed
Dependencies added: base, void
Files
- Data/Ord/Ala.hs +33/−0
- Data/Ord/Bounded.hs +61/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- ord-adhoc.cabal +20/−0
+ Data/Ord/Ala.hs view
@@ -0,0 +1,33 @@+{-| +Module : Data.Ord.Ala +Copyright : (C) 2013 Fumiaki Kinoshita +License : BSD-style (see the file LICENSE) +Maintainer : Fumiaki Kinsohita <fumiexcel@gmail.com> + +Creating Ord instance by a key function +-} +module Data.Ord.Ala (Ala(..), ordBy, unAla, keyAla, mapAla) where + +-- | Also known as store comonad +newtype Ala k a = Ala (a -> k, a) + +instance Eq k => Eq (Ala k a) where + a == b = keyAla a == keyAla b + +instance Ord k => Ord (Ala k a) where + a `compare` b = keyAla a `compare` keyAla b + +-- | Make 'Ord' instance from the key function. +ordBy :: Ord k => (a -> k) -> a -> Ala k a +ordBy f x = Ala (f, x) + +-- | Extract the original value from 'Ala'. +unAla :: Ala k a -> a +unAla (Ala (_, x)) = x + +-- | Extract the key from 'Ala'. +keyAla :: Ala k a -> k +keyAla (Ala (f, x)) = f x + +mapAla :: (j -> k) -> Ala j a -> Ala k a +mapAla f (Ala (k, a)) = Ala (f . k, a)
+ Data/Ord/Bounded.hs view
@@ -0,0 +1,61 @@+{-| +Module : Data.Ord.Bounded +Copyright : (C) 2013 Fumiaki Kinoshita +License : BSD-style (see the file LICENSE) +Maintainer : Fumiaki Kinsohita <fumiexcel@gmail.com> + +Creating bounded value from any Ord instance +-} +module Data.Ord.Bounded ( + GBounded + ,BoundedMin + ,BoundedMax + ,BoundedBoth + ,minimumBound + ,maximumBound + ) where + +import Control.Applicative +import Data.Void + +-- | A structure that provides minimum/maximum to any value. +data GBounded min max a = MinimumB min | ValueB a | MaximumB max deriving (Show, Eq) + +type BoundedMin = GBounded () Void +type BoundedMax = GBounded Void () +type BoundedBoth = GBounded () () + +-- | Provided minimum value. +minimumBound :: GBounded () max a +minimumBound = MinimumB () + +-- | Provided maximum value. +maximumBound :: GBounded min () a +maximumBound = MaximumB () + +instance Functor (GBounded min max) where + fmap f (ValueB a) = ValueB (f a) + fmap _ (MinimumB v) = MinimumB v + fmap _ (MaximumB v) = MaximumB v + +instance Applicative (GBounded min max) where + pure = ValueB + ValueB f <*> ValueB x = ValueB (f x) + MinimumB v <*> _ = MinimumB v + MaximumB v <*> _ = MaximumB v + +instance Monad (GBounded min max) where + return = ValueB + ValueB x >>= k = k x + MinimumB v >>= _ = MinimumB v + MaximumB v >>= _ = MaximumB v + +instance (Ord min, Ord max, Ord a) => Ord (GBounded min max a) where + ValueB a `compare` ValueB b = compare a b + MinimumB v `compare` MinimumB w = compare v w + MaximumB v `compare` MaximumB w = compare v w + MinimumB _ `compare` _ = LT + _ `compare` MinimumB _ = GT + MaximumB _ `compare` _ = GT + _ `compare` MaximumB _ = LT +
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2013, 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
+ ord-adhoc.cabal view
@@ -0,0 +1,20 @@+name: ord-adhoc +version: 0.0.0.1 +synopsis: Creating Ord instances instantly +description: A little extension to Ord +homepage: https://github.com/fumieval/ord-adhoc/ +license: BSD3 +license-file: LICENSE +author: Fumiaki Kinoshita +maintainer: Fumiaki Kinoshita <fumiexcel@gmail.com> +copyright: Copyright (C) 2013 Fumiaki Kinoshita +category: Data +build-type: Simple +cabal-version: >=1.8 +source-repository head + type: git + location: https://github.com/fumieval/ord-adhoc.git +library + exposed-modules: Data.Ord.Bounded, Data.Ord.Ala + -- other-modules: + build-depends: base ==4.*, void >=0.5