packages feed

cast (empty) → 0.1.0.0

raw patch · 5 files changed

+140/−0 lines, 5 filesdep +basesetup-changed

Dependencies added: base

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Bogdan Neterebskii (c) 2017++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 Bogdan Neterebskii 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.
+ README.md view
@@ -0,0 +1,70 @@+# Haskell pattern: cast++This pattern allows to incapsulate convert from one type of object for another.++## Example++Suppose you want convert different speed units to meter per second:++``` haskell+{-# LANGUAGE MultiParamTypeClasses #-}++import Pattern.Cast++newtype MeterPerSecond   = MeterPerSecond Float+  deriving (Ord, Eq)+newtype KilometerPerHour = KilometerPerHour Float+newtype MilesPerHour     = MilesPerHour Float++instance Cast KilometerPerHour MeterPerSecond where+  cast (KilometerPerHour v) = MeterPerSecond (0.277778 * v)++instance Cast MilesPerHour MeterPerSecond where+  cast (MilesPerHour v) = MeterPerSecond (0.44704 * v)+```++As you see, you have to use `MultiParamTypeClasses` language extension.++Then in every place you can just call one function `cast`:++``` haskell+> cast (KilometerPerHour 100) :: MeterPerSecond+MeterPerSecond 27.7778+> cast (MilesPerHour 100) :: MeterPerSecond+MeterPerSecond 44.704+```++You can type your functions more abstractly. Let's look at this synthetic example:++``` haskell+type Second = Float+type Meter  = Float++data Aircraft = Aircraft { distance :: Meter+                         , time     :: Second+                         }++instance Cast Aircraft MeterPerSecond where+  cast (Aircraft d t) = MeterPerSecond (d / t)+```++Then you can use `Cast` in type of your fuction like this (`FlexibleContexts` extension has to be used):++``` haskell+{-# LANGUAGE FlexibleContexts #-}++slowerThenSound :: Cast a MeterPerSecond => a -> Bool+slowerThenSound x = cast x < MeterPerSecond 340.29+```++And this fuction can be used with every type that can be converted in `MeterPerSecond`:++``` haskell+> slowerThenSound $ MeterPerSecond 200+True+> slowerThenSound $ KilometerPerHour 1000+True+> slowerThenSound $ Aircraft 1200 3+False+```+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ cast.cabal view
@@ -0,0 +1,28 @@+name:                cast+version:             0.1.0.0+synopsis:            Abstact cast pattern +description:+            Generelized pattern that allow cast one type for another.++            Look at README for example.  +homepage:            https://github.com/haskell-patterns/cast#readme+license:             BSD3+license-file:        LICENSE+author:              Bogdan Neterebskii+maintainer:          bog2dan1@gmail.com+copyright:           Copyright: (c) 2016 Bogdan Neterebskii+category:            Pattern+build-type:          Simple+extra-source-files:  README.md+cabal-version:       >=1.10++library+  hs-source-dirs:      src+  exposed-modules:     Pattern.Cast+  build-depends:       base >= 4.7 && < 5+  default-language:    Haskell2010+  ghc-options:       -Wall -O2++source-repository head+  type:     git+  location: https://github.com/haskell-patterns/cast
+ src/Pattern/Cast.hs view
@@ -0,0 +1,10 @@+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE FlexibleInstances #-}++module Pattern.Cast where++class Cast a b where+  cast :: a -> b++instance Cast a a where+  cast = id