fraction (empty) → 0.0.0.0
raw patch · 4 files changed
+118/−0 lines, 4 filesdep +algebradep +basesetup-changed
Dependencies added: algebra, base
Files
- LICENSE +25/−0
- Setup.lhs +4/−0
- fraction.cabal +25/−0
- src/Data/Fraction.hs +64/−0
+ LICENSE view
@@ -0,0 +1,25 @@+Copyright © 2009 Wolfgang Jeltsch+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 the copyright holders nor the names of the 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 HOLDERS 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.lhs view
@@ -0,0 +1,4 @@+#!/usr/bin/env runghc++> import Distribution.Simple+> main = defaultMain
+ fraction.cabal view
@@ -0,0 +1,25 @@+Name: fraction+Version: 0.0.0.0+Cabal-Version: >= 1.2+Build-Type: Simple+License: BSD3+License-File: LICENSE+Copyright: © 2009 Wolfgang Jeltsch+Author: Wolfgang Jeltsch+Maintainer: jeltsch@informatik.tu-cottbus.de+Stability: provisional+Homepage: http://community.haskell.org/~jeltsch/fraction/+Package-URL: http://hackage.haskell.org/packages/archive/fraction/0.0.0.0/fraction-0.0.0.0.tar.gz+Synopsis: Fractions+Description: This package provides a data type whose values represent fractions.+ .+ A fraction can be seen as a real number from the closed interval [0,1]. It can also+ be seen as a percentage.+Category: Data+Tested-With: GHC == 6.10.1++Library+ Build-Depends: algebra >= 0.0 && < 0.1,+ base >= 3.0 && < 4.1+ Exposed-Modules: Data.Fraction+ HS-Source-Dirs: src
+ src/Data/Fraction.hs view
@@ -0,0 +1,64 @@+{-|+ This module is about fractions.++ A fraction can be seen as a real number from the closed interval [0,1]. It can also be seen as a+ percentage. A typical example of a fraction is the extend of a progress bar.+-}+module Data.Fraction (++ -- * Fraction type+ Fraction,++ -- * Conversion+ fromFactor,+ fromPercentage,+ toFactor,+ toPercentage++) where++ -- Data+ import Data.Semigroup as Semigroup+ import Data.Monoid as Monoid++ -- * Fraction type+ -- |A fraction.+ newtype Fraction = Fraction Double++ instance Semigroup Fraction where++ Fraction factor1 `append` Fraction factor2 = Fraction (factor1 * factor2)++ instance Monoid Fraction where++ mempty = Fraction 1++ mappend = append++ -- * Conversion+ {-|+ Converts a factor into its corresponding fraction.++ If the factor is not from the interval [0,1], a runtime error occurs.+ -}+ fromFactor :: Double -> Fraction+ fromFactor factor | factor >= 0 && factor <= 1 = Fraction factor+ | otherwise = error "fraction: factor out of bounds"++ {-|+ Converts a percentage into its corresponding fraction.++ If the percentage is not from the interval [0,100], a runtime error occurs.+ -}+ fromPercentage :: Double -> Fraction+ fromPercentage perc | perc >= 0 && perc <= 100 = Fraction (perc / 100)+ | otherwise = error "fraction: percentage out of bounds"++ -- |Converts a fraction into its corresponding factor.+ toFactor :: Fraction -> Double+ toFactor (Fraction factor) = factor++ -- |Converts a fraction into its corresponding percentage.+ toPercentage :: Fraction -> Double+ toPercentage (Fraction factor) = factor * 100+