module Lambdabot.Plugin.Reference.OEIS (oeisPlugin) where
import Lambdabot.Plugin
import Math.OEIS
import Data.Char
oeisPlugin :: Module ()
oeisPlugin :: Module ()
oeisPlugin = Module ()
forall st. Module st
newModule
{ moduleCmds :: ModuleT () LB [Command (ModuleT () LB)]
moduleCmds = [Command (ModuleT () LB)]
-> ModuleT () LB [Command (ModuleT () LB)]
forall (m :: * -> *) a. Monad m => a -> m a
return
[ (String -> Command Identity
command "oeis")
{ aliases :: [String]
aliases = ["sequence"]
, help :: Cmd (ModuleT () LB) ()
help = String -> Cmd (ModuleT () LB) ()
forall (m :: * -> *). Monad m => String -> Cmd m ()
say "oeis <sequence>. Look up a sequence in the Online Encyclopedia of Integer Sequences"
, process :: String -> Cmd (ModuleT () LB) ()
process = IO String -> Cmd (ModuleT () LB) ()
forall (m :: * -> *). MonadIO m => IO String -> Cmd m ()
ios80 (IO String -> Cmd (ModuleT () LB) ())
-> (String -> IO String) -> String -> Cmd (ModuleT () LB) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO String
lookupOEIS'
}
]
}
lookupOEIS' :: String -> IO String
lookupOEIS' :: String -> IO String
lookupOEIS' a :: String
a = do
let a' :: String
a' = String -> String
commas (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
forall a. [a] -> [a]
reverse (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
forall a. [a] -> [a]
reverse (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
a
Maybe OEISSequence
x <- String -> IO (Maybe OEISSequence)
searchSequence_IO String
a'
case Maybe OEISSequence
x of
Nothing -> String -> IO String
forall (m :: * -> *) a. Monad m => a -> m a
return "Sequence not found."
Just s :: OEISSequence
s -> String -> IO String
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> IO String) -> String -> IO String
forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines [
[String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ("https://oeis.org/" String -> [String] -> [String]
forall a. a -> [a] -> [a]
: Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
take 1 (OEISSequence -> [String]
catalogNums OEISSequence
s)) String -> String -> String
forall a. [a] -> [a] -> [a]
++
' ' Char -> String -> String
forall a. a -> [a] -> [a]
: OEISSequence -> String
description OEISSequence
s,
SequenceData -> String
forall a. Show a => a -> String
show (SequenceData -> String) -> SequenceData -> String
forall a b. (a -> b) -> a -> b
$ OEISSequence -> SequenceData
sequenceData OEISSequence
s]
where
commas :: String -> String
commas [] = []
commas (x :: Char
x:' ':xs :: String
xs) | Char -> Bool
isDigit Char
x = Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: ',' Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
commas String
xs
commas (x :: Char
x:xs :: String
xs) = Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
commas String
xs