packages feed

continued-fraction 0.1.0.8 → 0.1.0.9

raw patch · 4 files changed

+27/−16 lines, 4 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

Files

cabal.project.local view
@@ -2,3 +2,5 @@ tests: true benchmarks: true optimization: 1+constraints: recursion-schemes -template-haskell+           , comonad -test-doctests
continued-fraction.cabal view
@@ -1,6 +1,6 @@ cabal-version: 1.18 name: continued-fraction-version: 0.1.0.8+version: 0.1.0.9 license: BSD3 license-file: LICENSE copyright: Copyright: (c) 2018 Vanessa McHale
src/Num/ContinuedFraction.hs view
@@ -10,9 +10,11 @@     , prettyFracM     ) where -import           Data.Functor.Foldable (ListF (..), apo)+import           Data.Foldable+import           Data.Functor.Base+import           Data.Functor.Foldable (ListF (..), apo, cata) import           Data.List             (intersperse)-import           Data.List.NonEmpty    (NonEmpty (..), fromList)+import           Data.List.NonEmpty    (NonEmpty (..)) import           Data.Maybe            (fromJust) import           Data.Ratio            (Ratio, denominator, (%)) @@ -28,7 +30,7 @@         idem = ((==) <*>)         down = realToFrac . (floor :: (RealFrac a) => a -> Integer) --- | This take a number and returns its continued fraction expansion as a list of `Integer`s.+-- | This take a number and returns its continued fraction expansion as a list of integers. -- -- >>> continuedFraction 2 -- [2]@@ -41,12 +43,12 @@                         go    = Cons (floor x)  prettyFrac :: (Show a) => NonEmpty a -> String-prettyFrac (x :| xs) = "[" ++ show x ++ "; " ++ mconcat (intersperse ", " (show <$> xs)) ++ "]"+prettyFrac (x :| xs) = "[" ++ show x ++ "; " ++ fold (intersperse ", " (show <$> xs)) ++ "]"  -- | Print a list as a continued fraction. -- -- >>> prettyFracM (take 5 $ continuedFraction (sqrt 2))--- "[1; 2, 2, 2, 2]"+-- Just "[1; 2, 2, 2, 2]" prettyFracM :: (Show a) => [a] -> Maybe String prettyFracM []     = Nothing prettyFracM (x:xs) = Just (prettyFrac (x :| xs))@@ -57,7 +59,7 @@ -- Nothing -- >>> collapseFractionM [1,2,2,2] -- Just (17 % 12)-collapseFractionM :: (Integral a) => [Integer] -> Maybe (Ratio a)+collapseFractionM :: (Integral a, Integral b) => [a] -> Maybe (Ratio b) collapseFractionM []     = Nothing collapseFractionM (x:xs) = Just $ collapseFraction (x:|xs) @@ -66,19 +68,22 @@ -- >>> collapseFraction (1 :| [2,2,2]) -- 17 % 12 collapseFraction :: (Integral a, Integral b) => NonEmpty b -> Ratio a-collapseFraction (x:|[]) = fromIntegral x % 1-collapseFraction (x:|xs) = (fromIntegral x % 1) + 1 / collapseFraction (fromList xs)+collapseFraction = cata a+    where a (NonEmptyF x xs) = go xs $ fromIntegral x % 1+          go = maybe id ((+) . (1/))  -- | Find a given convergent of the continued fraction expansion of a number -- -- >>> convergent 4 $ sqrt 2 -- 17 % 12 convergent :: (RealFrac a, Integral b) => Int -> a -> Ratio b-convergent n x = fromJust . collapseFractionM $ take n (continuedFraction x)+convergent n x = fromJust . (collapseFractionM :: Integral a => [Integer] -> Maybe (Ratio a)) $+    take n (continuedFraction x)  -- | Find the best rational approximation to a number such that the denominator is bounded by a given value. -- -- >>> approximate pi 100 -- 22 % 7 approximate :: (RealFrac a, Integral b) => a -> b -> Ratio b-approximate x d = last . takeWhile ((<= d) . denominator) $ fmap (flip convergent x) [1..]+approximate x d = last . takeWhile ((<= d) . denominator) $+    fmap (flip convergent x) [1..]
stack.yaml view
@@ -1,8 +1,12 @@-resolver: lts-11.3+---+resolver: lts-11.7 packages:-- '.'+  - '.' extra-deps: flags:-    continued-fraction:-        development: true-extra-package-dbs: []+  continued-fraction:+    development: true+  comonad:+    test-doctests: false+  recursion-schemes:+    template-haskell: false