Skip to content

Commit

Permalink
Merge pull request #98 from fizruk/rename-runbot
Browse files Browse the repository at this point in the history
Rename `RunBot` to `GetAction`
  • Loading branch information
swamp-agr committed Feb 4, 2022
2 parents 7b11abc + 735dfc6 commit 24800d3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
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

0 comments on commit 24800d3

Please sign in to comment.