oeis 0.3.9 → 0.3.10
raw patch · 5 files changed
+43/−26 lines, 5 files
Files
- Math/OEIS.hs +17/−5
- Math/OEIS/Internal.hs +11/−10
- Math/OEIS/Types.hs +1/−1
- oeis.cabal +9/−5
- test/Test.hs +5/−5
Math/OEIS.hs view
@@ -8,6 +8,7 @@ , getSequenceByID_IO, lookupSequenceByID_IO , extendSequence_IO, lookupSequence_IO , searchSequence_IO, lookupOEIS+ , searchSequences_IO, lookupSequences_IO -- * Data structures , SequenceData@@ -17,8 +18,10 @@ -------------------------------------------------------------------------------- +import Control.Applicative ((<$>))+import Prelude import Data.Char (isDigit, isSpace)-import Data.List (isPrefixOf, tails)+import Data.List (isPrefixOf, tails, find) import Data.Maybe (listToMaybe, fromMaybe) import Network.URI (escapeURIString, isAllowedInURI) import System.IO.Unsafe (unsafePerformIO)@@ -42,8 +45,13 @@ -- | Look up a sequence in the OEIS using its search function. searchSequence_IO :: String -> IO (Maybe OEISSequence)-searchSequence_IO x = getOEIS (baseSearchURI ++) (escapeURIString isAllowedInURI x)+searchSequence_IO x = listToMaybe <$> searchSequences_IO x +-- | Look up sequences in the OEIS using its search function (returns up to+-- 10 results).+searchSequences_IO :: String -> IO [OEISSequence]+searchSequences_IO x = getOEIS (baseSearchURI ++) (escapeURIString isAllowedInURI x)+ -- | Look up a sequence in the OEIS by its catalog number. Generally this would -- be its A-number, but M-numbers (from the /Encyclopedia of Integer -- Sequences/) and N-numbers (from the /Handbook of Integer Sequences/) can be@@ -88,7 +96,7 @@ -- | The same as 'lookupSequenceByID', but in the 'IO' monad. lookupSequenceByID_IO :: String -> IO (Maybe OEISSequence)-lookupSequenceByID_IO = getOEIS idSearchURI+lookupSequenceByID_IO x = listToMaybe <$> getOEIS idSearchURI x -- | Extend a sequence by using it as a lookup to the OEIS, taking the first -- sequence returned as a result, and using it to augment the original@@ -135,7 +143,7 @@ -- -- > forall xs ext. xs `isPrefixOf` (extend xs ext) extend :: SequenceData -> SequenceData -> SequenceData-extend xs ext = fromMaybe xs . listToMaybe . filter (xs `isPrefixOf`) $ tails ext+extend xs ext = fromMaybe xs . find (xs `isPrefixOf`) $ tails ext -- | Find a matching sequence in the OEIS database, returning a data structure -- containing the entirety of the information the OEIS has on the sequence.@@ -146,7 +154,11 @@ -- | The same as 'lookupSequence', but in the 'IO' monad. lookupSequence_IO :: SequenceData -> IO (Maybe OEISSequence)-lookupSequence_IO = getOEIS seqSearchURI+lookupSequence_IO x = listToMaybe <$> lookupSequences_IO x++-- | Similar to 'lookupSequence_IO', but return up to 10 results.+lookupSequences_IO :: SequenceData -> IO [OEISSequence]+lookupSequences_IO = getOEIS seqSearchURI --------------------------------------------------------------------------------
Math/OEIS/Internal.hs view
@@ -24,15 +24,13 @@ seqSearchURI :: SequenceData -> String seqSearchURI xs = baseSearchURI ++ intercalate "," (map show xs) -getOEIS :: (a -> String) -> a -> IO (Maybe OEISSequence)+getOEIS :: (a -> String) -> a -> IO [OEISSequence] getOEIS toURI key = case parseURI (toURI key) of- Nothing -> return Nothing+ Nothing -> return [] Just uri -> do mbody <- get uri- return $ case mbody of- Nothing -> Nothing- Just body -> parseOEIS body+ return $ maybe [] parseOEIS mbody get :: URI -> IO (Maybe String) get uri = do@@ -76,9 +74,9 @@ addElement ('O', x) c = c { offset = read o , firstGT1 = read f } where (o,f) = second tail . span (/=',') $ x-addElement ('p', x) c = c { programs = (Mathematica, x) :+addElement ('p', x) c = c { programs = (Maple, x) : programs c }-addElement ('t', x) c = c { programs = (Maple, x) :+addElement ('t', x) c = c { programs = (Mathematica, x) : programs c } addElement ('o', x) c = c { programs = (Other, x) : programs c }@@ -88,12 +86,15 @@ addElement ('C', x) c = c { comments = x : comments c } addElement _ c = c -parseOEIS :: String -> Maybe OEISSequence+parseOEIS :: String -> [OEISSequence] parseOEIS x = if "No results." `isPrefixOf` (ls!!3)- then Nothing- else Just . foldl' (flip addElement) emptyOEIS . reverse . parseRawOEIS $ ls'+ then []+ else go . dropWhile ((/= 'I') . fst) . parseRawOEIS $ ls' where ls = lines x ls' = init . drop 5 $ ls+ go [] = []+ go (i:xs) = foldl' (flip addElement) emptyOEIS (reverse (i:ys)) : go zs+ where (ys, zs) = break ((== 'I') . fst) xs parseRawOEIS :: [String] -> [(Char, String)] parseRawOEIS = map parseItem . combineConts
Math/OEIS/Types.hs view
@@ -6,7 +6,7 @@ -- in. The only languages indicated natively by the OEIS database are -- Mathematica and Maple; any other languages will be listed (usually in -- parentheses) at the beginning of the actual code snippet.-data Language = Mathematica | Maple | Other deriving Show+data Language = Mathematica | Maple | Other deriving (Show, Eq) -- | OEIS keywords. For more information on the meaning of each keyword, see -- <http://oeis.org/eishelp2.html#RK>.
oeis.cabal view
@@ -1,10 +1,10 @@ name: oeis-version: 0.3.9+version: 0.3.10 category: Math stability: experimental author: Brent Yorgey-maintainer: Brian Lewis <brian@lorf.org>+maintainer: Bartosz Nitka <niteria@gmail.com> license: BSD3 license-file: LICENSE@@ -14,11 +14,15 @@ Interface to the <http://oeis.org/ Online Encyclopedia of Integer Sequences (OEIS)>. cabal-version: >= 1.10-build-type: Custom+build-type: Simple tested-with: GHC==7.8.4, GHC==7.10.3,- GHC==8.0.2+ GHC==8.0.2,+ GHC==8.2.2,+ GHC==8.4.4,+ GHC==8.6.5,+ GHC==8.8.1 -------------------------------------------------------------------------------- @@ -82,4 +86,4 @@ source-repository head type: git- location: git://github.com/bsl/oeis.git+ location: git://github.com/niteria/oeis.git
test/Test.hs view
@@ -35,17 +35,17 @@ test_parseOEIS = do r0 <- readFileUtf8 "test/data/id_rsp.txt" case parseOEIS r0 of- Nothing -> assertFailure ""- Just r -> do+ [r] -> do check r examples r @?= []+ _ -> assertFailure "" r1 <- readFileUtf8 "test/data/seq_rsp.txt" case parseOEIS r1 of- Nothing -> assertFailure ""- Just r -> do+ [r,_,_,_,_,_,_,s,_,_] -> do check r- assertBool "" $ not $ null $ examples r+ assertBool "" $ not $ null $ examples s+ _ -> assertFailure "" where check r = do catalogNums r @?= ["A000040","M0652","N0241"]