-- | Look up sequences in the Online Encyclopedia of Integer Sequences
--   Based on the Math.OEIS library
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 String
"oeis")
            { aliases :: [String]
aliases = [String
"sequence"]
            , help :: Cmd (ModuleT () LB) ()
help = String -> Cmd (ModuleT () LB) ()
forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"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' 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
        Maybe OEISSequence
Nothing -> String -> IO String
forall (m :: * -> *) a. Monad m => a -> m a
return String
"Sequence not found."
        Just 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 (String
"https://oeis.org/" String -> [String] -> [String]
forall a. a -> [a] -> [a]
: Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
take Int
1 (OEISSequence -> [String]
catalogNums OEISSequence
s)) String -> String -> String
forall a. [a] -> [a] -> [a]
++
            Char
' ' 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 (Char
x:Char
' ':String
xs) | Char -> Bool
isDigit Char
x = Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: Char
',' Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
commas String
xs
    commas (Char
x:String
xs)                 = Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
commas String
xs