summaryrefslogtreecommitdiffstats
path: root/src/utils.hs
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2022-01-31 11:08:30 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2022-01-31 11:27:29 +0200
commit7390af67e18e6d17934541dd3c2d6d46cfad84d0 (patch)
tree72231d88788752b443c4622041693327579f5fa0 /src/utils.hs
parentdd46dafe65e93b687ce5051288ab3f5cbfb7e09a (diff)
Pop words type-safely from stack
Diffstat (limited to 'src/utils.hs')
-rw-r--r--src/utils.hs10
1 files changed, 9 insertions, 1 deletions
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