Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename RunBot to GetAction #98

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/Telegram/Bot/Simple/Eff.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,30 @@ newtype Eff action model = Eff { _runEff :: Writer [BotM (Maybe action)] model }
-- If you don't want to return action use 'Nothing' instead.
--
-- See "Telegram.Bot.Simple.Instances" for more commonly useful instances.
-- - @RunBot a a@ - for simple making finite automata of
-- - @GetAction a a@ - for simple making finite automata of
-- BotM actions. (For example you can log every update
-- and then return new 'action' to answer at message/send sticker/etc)
-- - @RunBot () a@ - to use @pure ()@ instead of dealing with @Nothing@.
-- - @RunBot Text a@ - to add some sugar over the 'replyText' function.
-- - @GetAction () a@ - to use @pure ()@ instead of dealing with @Nothing@.
-- - @GetAction Text a@ - to add some sugar over the 'replyText' function.
-- 'OverloadedStrings' breaks type inference,
-- so we advise to use @replyText \"message\"@
-- instead of @pure \@_ \@Text \"message\"@.
class RunBot ret action where
runBot :: BotM ret -> BotM (Maybe action)
class GetAction return action where
getNextAction :: BotM return -> BotM (Maybe action)

instance Bifunctor Eff where
bimap f g = Eff . mapWriter (bimap g (map . fmap . fmap $ f)) . _runEff

runEff :: Eff action model -> (model, [BotM (Maybe action)])
runEff = runWriter . _runEff

eff :: RunBot a b => BotM a -> Eff b ()
eff e = Eff (tell [runBot e])
eff :: GetAction a b => BotM a -> Eff b ()
eff e = Eff (tell [getNextAction e])

withEffect :: RunBot a action => BotM a -> model -> Eff action model
withEffect :: GetAction a action => BotM a -> model -> Eff action model
withEffect effect model = eff effect >> pure model

(<#) :: RunBot a action => model -> BotM a -> Eff action model
(<#) :: GetAction a action => model -> BotM a -> Eff action model
(<#) = flip withEffect

-- | Set a specific 'Telegram.Update' in a 'BotM' context.
Expand Down
14 changes: 7 additions & 7 deletions src/Telegram/Bot/Simple/Instances.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import Data.Text (Text)
import Telegram.Bot.Simple.Eff
import Telegram.Bot.Simple.Reply (replyText)

instance RunBot a a where
runBot effect = Just <$> effect
instance GetAction a a where
getNextAction effect = Just <$> effect

instance RunBot () a where
runBot effect = Nothing <$ effect
instance GetAction () a where
getNextAction effect = Nothing <$ effect

instance RunBot Text a where
runBot effect = runBot do
instance GetAction Text a where
getNextAction effect = getNextAction do
t <- effect
replyText t
replyText t