sloane 0.1 → 0.1.1
raw patch · 3 files changed
+26/−60 lines, 3 files
Files
- README.md +3/−3
- sloane.cabal +1/−1
- sloane.hs +22/−56
README.md view
@@ -34,16 +34,16 @@ S A006531 1,1,3,19,183,2371,38703,763099,17648823,468603091,14050842303, N A006531 Semiorders on n elements. -Show the sequence, name, formula and comments fields of the sequence whose A-number is A006531:+Show the sequence, name, comments, and formula fields of the sequence whose A-number is A006531: - $ sloane -kSNFC id:A006531+ $ sloane -kSNCF id:A006531 S A006531 1,1,3,19,183,2371,38703,763099,17648823,468603091,14050842303, N A006531 Semiorders on n elements.+ C A006531 Labeled semiorders on n elements: (1+3) and (2+2)-free posets. - Detlef Pauly (dettodet(AT)yahoo.de), Dec 27 2002 F A006531 O.g.f.: Sum_{n>=1} (2*n)!/(n+1)! * x^n / Product_{k=0..n} (1+k*x). [From Paul D. Hanna, Jul 20 2011] F A006531 E.g.f.: C(1-exp(-x)), where C(x) = (1 - sqrt(1 - 4*x)) / (2*x) is the ordinary g.f. for the Catalan numbers A000108. F A006531 a(n) = sum( S(n, k) * k! * M(k-1), k=1..n), S(n, k): Stirling number of the second kind, M(n): Motzkin number, A001006. - Detlef Pauly, Jun 06 2002- C A006531 Labeled semiorders on n elements: (1+3) and (2+2)-free posets. - Detlef Pauly (dettodet(AT)yahoo.de), Dec 27 2002 Return at most 3 results of a free text search:
sloane.cabal view
@@ -1,5 +1,5 @@ Name: sloane-Version: 0.1+Version: 0.1.1 Synopsis: A command line interface to Sloane's On-Line Encyclopedia of Integer Sequences Description: A command line interface to Sloane's On-Line Encyclopedia of Integer Sequences. For usage see <http://github.com/akc/sloane>.
sloane.hs view
@@ -2,17 +2,15 @@ import Prelude hiding (all) import System.Console.CmdArgs-import Data.Maybe (fromJust, isJust)-import Data.Function (on)-import Data.IORef (IORef, newIORef, readIORef, writeIORef)+import Data.List (groupBy)+import Data.Maybe (fromJust) import Control.Monad (when)-import System.IO.Unsafe (unsafePerformIO) import Network.HTTP (simpleHTTP, getRequest, getResponseBody) import Network.URL (importURL, exportURL, add_param)-import Data.List (groupBy, sortBy, intersperse)-import Data.Ord (comparing) -newtype OEISEntry = OEISEntry [(Char, String)]+type OEISEntry = String+type Query = String+type Key = Char oeisURL = fromJust $ importURL "http://oeis.org/search?fmt=text" oeisKeys = "ISTUVWXNDHFYAOEeptoKC"@@ -25,65 +23,33 @@ deriving (Data, Typeable) sloane = cmdArgsMode $ Sloane - { keys = "SN" &= typ "KEYS" - &= help "Keys of fields to print, http://oeis.org/eishelp1.html (default: SN)"- , all = False - &= help "Print all fields"- , limit = 5 &= name "n"- &= help "Limit the number of entries retrieved (default: 5)"- , terms = def &= argPos 0 &= typ "SEARCH-TERMS"- } + { keys = "SN" &= typ "KEYS"+ &= help "Keys of fields to print, http://oeis.org/eishelp1.html (default: SN)"+ , all = False &= help "Print all fields"+ , limit = 5 &= name "n" &= help "Limit the number of entries retrieved (default: 5)"+ , terms = def &= argPos 0 &= typ "SEARCH-TERMS"+ } &= versionArg [summary "sloane 0.1"] &= summary "Search Sloane's On-Line Encyclopedia of Integer Sequences" --- The maximum number of results returned when searching the OEIS.-maxResults :: IORef Int-maxResults = unsafePerformIO $ newIORef 5---- Get the maximum number of results returned when searching OEIS-getMaxResults :: IO Int-getMaxResults = readIORef maxResults---- Set the maximum number of results returned when searching OEIS-setMaxResults :: Int -> IO ()-setMaxResults = writeIORef maxResults- nonempty = not . null -parseOEISEntry :: String -> OEISEntry-parseOEISEntry = OEISEntry- . map merge- . groupBy ((==) `on` fst)- . sortBy (comparing fst)- . map (split . tail) . filter nonempty . lines- where- split xs@(x:_) = (x, xs)- merge kvs = let (k:_, vs) = unzip kvs in (k, init $ unlines vs)--parseOEISEntries :: String -> [OEISEntry]-parseOEISEntries = map parseOEISEntry- . filter nonempty- . map unlines- . groupBy (\_ x -> nonempty x)- . reverse . drop 2 . reverse . drop 5 . lines+parse :: String -> [OEISEntry]+parse = map (unlines . filter nonempty) . groupBy (\_ x -> nonempty x)+ . map (drop 1) . reverse . drop 2 . reverse . drop 5 . lines --- Returns the given fields of an 'OEISEntry'. For instance, @fields "NFA" e@--- would return the name, formula, and auhtor fields of @e@.-fields :: String -> OEISEntry -> [String]-fields ks (OEISEntry e) = [ fromJust d | k<-ks, let d = lookup k e, isJust d ]+select :: [Key] -> OEISEntry -> OEISEntry+select ks = unlines . filter (\(k:_) -> k `elem` ks) . lines --- Returns a list of matching entries in OEIS-lookupOEIS :: String -> IO [OEISEntry]-lookupOEIS s = do- n <- getMaxResults- parseOEISEntries `fmap` (simpleHTTP (getRequest (url n)) >>= getResponseBody)+searchOEIS :: Int -> Query -> IO [OEISEntry]+searchOEIS n s = do+ parse `fmap` (simpleHTTP (getRequest url) >>= getResponseBody) where- url n = exportURL $ oeisURL `add_param` ("n", show n) `add_param` ("q", s)+ url = exportURL $ oeisURL `add_param` ("n", show n) `add_param` ("q", s) main = do args <- cmdArgsRun sloane- setMaxResults $ limit args- es <- lookupOEIS . filter (`notElem` "[]") $ terms args+ es <- searchOEIS (limit args) . filter (`notElem` "[]") $ terms args when (nonempty es) $ putStrLn "" let ks = if all args then oeisKeys else keys args- mapM_ putStrLn [ unlines $ fields ks e | e <- es ]+ mapM_ (putStrLn . select ks) es