From 7390af67e18e6d17934541dd3c2d6d46cfad84d0 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Mon, 31 Jan 2022 11:08:30 +0200 Subject: Pop words type-safely from stack --- src/utils.hs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/utils.hs') diff --git a/src/utils.hs b/src/utils.hs index 5b78682..4ffdf3b 100644 --- a/src/utils.hs +++ b/src/utils.hs @@ -1,5 +1,6 @@ module Utils where +import Control.Monad.Except import qualified Data.Bifunctor as B (.>) = flip (.) @@ -42,4 +43,11 @@ breakOn cond xs = break cond xs $> B.second (drop 1) mix :: [a] -> [a] -> [a] mix (x : xs) (y : ys) = x : y : mix xs ys mix x [] = x -mix [] y = y \ No newline at end of file +mix [] y = y + +safeBreak cond ex = safeBreak' cond ex [] + +safeBreak' _ ex _ [] = throwError ex +safeBreak' cond ex acc lst@(x : xs) + | cond x = pure (acc, lst) + | otherwise = safeBreak' cond ex (x : acc) xs -- cgit v1.3