aboutsummaryrefslogtreecommitdiffstats
path: root/src/Plugin/Factorial.hs
blob: 692d20f51da6695769d84db2c0338adb402a66aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{-# LANGUAGE OverloadedStrings #-}

module Plugin.Factorial
    ( factorial
    ) where

import Text.Regex.TDFA ((=~))
import TextShow (showt)

import qualified Message as M
import Plugin.Base

factorial = Plugin
    { matchRegex  = "^([0-9]+)!$"
    , perform     = factorialAction
    , command     = "<integer>!"
    , description = "Calculate the factorial of <integer>"
    }

factorialAction :: PluginAction
factorialAction message = do
    case M.textStr message =~ matchRegex factorial :: [[String]] of
        []    -> return $ Left "I didn't understand"
        (m:_) -> do
            let number = last m
            return $ Right $ showt $ calculate $ (read number :: Int)

calculate :: (Integral a) => a -> a
calculate n = product [1..n]