{-# LANGUAGE NoMonoLocalBinds #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
module Main (main) where
import Bluefin.Internal
import Bluefin.Internal.DslBuilder
import Control.Monad (when)
import Data.Foldable (for_)
import Test.GeneralBracket (test_generalBracket)
import Test.SpecH (SpecH, assertEqual, runSpecH)
import Prelude hiding (break, read)
main :: IO ()
main = runEff_ $ \io -> do
runSpecH io $ \y -> do
let assertEqual' = assertEqual y
assertEqual' "oddsUntilFirstGreaterThan5" oddsUntilFirstGreaterThan5 [1, 3, 5, 7]
assertEqual' "index 1" ([0, 1, 2, 3] !? 2) (Just 2)
assertEqual' "index 2" ([0, 1, 2, 3] !? 4) Nothing
assertEqual'
"Exception 1"
(runPureEff (try (eitherEff (Left True))))
(Left True :: Either Bool ())
assertEqual'
"Exception 2"
(runPureEff (try (eitherEff (Right True))))
(Right True :: Either () Bool)
assertEqual'
"State"
(runPureEff (runState 10 (stateEff (\n -> (show n, n * 2)))))
("10", 20)
assertEqual'
"List"
(runPureEff (yieldToList (listEff ([20, 30, 40], "Hello"))))
([20, 30, 40], "Hello")
test_localInHandler y
test_generalBracket io y
(!?) :: [a] -> Int -> Maybe a
xs !? i = runPureEff $
withEarlyReturn $ \ret -> do
evalState 0 $ \s -> do
for_ xs $ \a -> do
i' <- get s
when (i == i') (returnEarly ret (Just a))
put s (i' + 1)
pure Nothing
oddsUntilFirstGreaterThan5 :: [Int]
oddsUntilFirstGreaterThan5 =
fst $
runPureEff $
yieldToList $ \y -> do
withJump $ \break -> do
for_ [1 .. 10] $ \i -> do
withJump $ \continue -> do
when (i `mod` 2 == 0) $
jumpTo continue
yield y i
when (i > 5) $
jumpTo break
-- | Inverse to 'try'
eitherEff :: (e1 :> es) => Either e r -> Exception e e1 -> Eff es r
eitherEff eith ex = case eith of
Left e -> throw ex e
Right r -> pure r
-- | Inverse to 'runState'
stateEff :: (e1 :> es) => (s -> (a, s)) -> State s e1 -> Eff es a
stateEff f st = do
s <- get st
let (a, s') = f s
put st s'
pure a
-- | Inverse to 'yieldToList'
listEff :: (e1 :> es) => ([a], r) -> Stream a e1 -> Eff es r
listEff (as, r) y = do
for_ as (yield y)
pure r
test_localInHandler :: (e :> es) => SpecH e -> Eff es ()
test_localInHandler y = runReader "global" $ \re ->
forEach
(\y2 -> local re (const "local") (yield y2 ()))
(\() -> assertEqual y "Reader local" "local" =<< ask re)