sloane (empty) → 0.1
raw patch · 5 files changed
+209/−0 lines, 5 filesdep +HTTPdep +basedep +cmdargssetup-changed
Dependencies added: HTTP, base, cmdargs, url
Files
- LICENSE +30/−0
- README.md +65/−0
- Setup.hs +2/−0
- sloane.cabal +23/−0
- sloane.hs +89/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2012, Anders Claesson++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 Anders Claesson 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,65 @@+sloane+======++A command line interface to Sloane's On-Line Encyclopedia of Integer Sequences (OEIS)++Install+-------++ $ cabal install sloane++Usage+-----++ $ sloane --help+ Search Sloane's On-Line Encyclopedia of Integer Sequences++ sloane [OPTIONS] SEARCH-TERMS++ Common flags:+ -k --keys=KEYS Keys of fields to print, http://oeis.org/eishelp1.html+ (default: SN)+ -a --all Print all fields+ -n --limit=INT Limit the number of entries retrieved (default: 5)+ -? --help Display help message+ -V --version Print version information++Examples+--------++Search for entries matching a sequence:++ $ sloane 1,3,19,183,2371,38703++ 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:++ $ sloane -kSNFC id:A006531++ S A006531 1,1,3,19,183,2371,38703,763099,17648823,468603091,14050842303,+ N A006531 Semiorders on n elements.+ 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 -n3 "(2+2)-free posets"++ S A079144 1,1,3,19,207,3451,81663,2602699,107477247,5581680571,356046745023,+ N A079144 Number of labeled interval orders on n elements: (2+2)-free posets.++ S A022493 1,1,2,5,15,53,217,1014,5335,31240,201608,1422074,10886503,89903100,+ N A022493 Number of linearized chord diagrams of degree n; also number of nonisomorphic interval orders on n unlabeled points.++ S A006531 1,1,3,19,183,2371,38703,763099,17648823,468603091,14050842303,+ N A006531 Semiorders on n elements.++Caveat+------++Please use this program with moderation as not to put too much of a burden on the OEIS-server; see+[OEIS' policy on searching the database](http://oeis.org/wiki/Welcome#Policy_on_Searching_the_Database).
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ sloane.cabal view
@@ -0,0 +1,23 @@+Name: sloane+Version: 0.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>.+Homepage: http://github.com/akc/sloane+License: BSD3+License-file: LICENSE+Author: Anders Claesson+Maintainer: anders.claesson@gmail.com+Category: Math+Build-type: Simple+Extra-Source-Files: README.md++Cabal-version: >=1.6++source-repository head+ type: git+ location: git://github.com/akc/sloane.git++Executable sloane+ Main-is: sloane.hs+ Build-depends: base >= 3 && < 5, cmdargs, HTTP, url
+ sloane.hs view
@@ -0,0 +1,89 @@+{-# LANGUAGE DeriveDataTypeable #-}++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 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)]++oeisURL = fromJust $ importURL "http://oeis.org/search?fmt=text"+oeisKeys = "ISTUVWXNDHFYAOEeptoKC"++data Sloane = Sloane { keys :: String+ , all :: Bool+ , limit :: Int+ , terms :: String+ }+ 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"+ } + &= 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++-- 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 ]++-- Returns a list of matching entries in OEIS+lookupOEIS :: String -> IO [OEISEntry]+lookupOEIS s = do+ n <- getMaxResults+ parseOEISEntries `fmap` (simpleHTTP (getRequest (url n)) >>= getResponseBody)+ where+ url n = 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+ when (nonempty es) $ putStrLn ""+ let ks = if all args then oeisKeys else keys args+ mapM_ putStrLn [ unlines $ fields ks e | e <- es ]