From 41f0b8d511ee53dbd2f298ff00cfcf9b30ee40cb Mon Sep 17 00:00:00 2001 From: George Thomas Date: Tue, 6 Jan 2026 22:32:11 +0000 Subject: [PATCH] Move some utilities out of `Day4.hs` --- haskell/Pre.hs | 11 +++++++++++ haskell/Puzzles/Day4.hs | 8 -------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/haskell/Pre.hs b/haskell/Pre.hs index e03416b..28ab845 100644 --- a/haskell/Pre.hs +++ b/haskell/Pre.hs @@ -39,6 +39,9 @@ module Pre ( module Text.Megaparsec.Char.Lexer, module Text.Pretty.Simple, Puzzle (..), + (<<$>>), + (<<&>>), + takeUntil, digit, digitsToInt, listIndex, @@ -138,6 +141,14 @@ data Puzzle = forall input outputs. (KnownNat (Length outputs), NFData input) => , extraTests :: Bool -> FilePath -> [TestTree IO (input, HList outputs)] } +(<<$>>) :: (Functor f1, Functor f2) => (a -> b) -> f1 (f2 a) -> f1 (f2 b) +(<<$>>) = fmap . fmap +(<<&>>) :: (Functor f1, Functor f2) => f1 (f2 a) -> (a -> b) -> f1 (f2 b) +(<<&>>) = flip (<<$>>) + +takeUntil :: (Foldable t) => (a -> Bool) -> t a -> [a] +takeUntil p = foldr (\x xs -> x : if p x then [] else xs) [] + digit :: (Token s ~ Char, Num b, MonadParsec e s f) => f b digit = fromIntegral . digitToInt <$> digitChar diff --git a/haskell/Puzzles/Day4.hs b/haskell/Puzzles/Day4.hs index 03dc718..d449ce7 100644 --- a/haskell/Puzzles/Day4.hs +++ b/haskell/Puzzles/Day4.hs @@ -123,13 +123,5 @@ noneAccessible (Grid g) = not $ any (elem OutAccessible . fmap snd) g countRolls :: Grid InTile -> Int countRolls (Grid g) = length $ concatMap (filter (== InRoll) . toList . fmap snd) g -(<<$>>) :: (Functor f1, Functor f2) => (a -> b) -> f1 (f2 a) -> f1 (f2 b) -(<<$>>) = fmap . fmap -(<<&>>) :: (Functor f1, Functor f2) => f1 (f2 a) -> (a -> b) -> f1 (f2 b) -(<<&>>) = flip (<<$>>) - -takeUntil :: (Foldable t) => (a -> Bool) -> t a -> [a] -takeUntil p = foldr (\x xs -> x : if p x then [] else xs) [] - unfoldMutual :: (a -> b) -> (b -> a) -> a -> Stream (a, b) unfoldMutual f g a = let b = f a in (a, b) :> unfoldMutual f g (g b)