Use external strict pair package (use strict-tuple-lens also?)

This commit is contained in:
2025-06-20 19:54:56 +01:00
parent d7354f7bc9
commit edcd947c90
+11 -9
View File
@@ -1,13 +1,15 @@
module FoldlHelp module FoldlHelp (
( minimumOn minimumOn,
, premapMaybe premapMaybe,
, module Control.Foldl module Control.Foldl,
) where ) where
import Control.Foldl import Control.Foldl
import MaybeHelp import MaybeHelp
import Data.Strict.Tuple
--data Maybe' a = Just' !a | Nothing' --data Maybe' a = Just' !a | Nothing'
data Pair a b = !a :!: !b --data Pair a b = !a :!: !b
minimumOn :: Ord b => (a -> b) -> Fold a (Maybe a) minimumOn :: Ord b => (a -> b) -> Fold a (Maybe a)
minimumOn f = Fold step Nothing' extract minimumOn f = Fold step Nothing' extract
@@ -16,15 +18,15 @@ minimumOn f = Fold step Nothing' extract
extract (Just' (a :!: _)) = Just a extract (Just' (a :!: _)) = Just a
step Nothing' b = Just' (b :!: f b) step Nothing' b = Just' (b :!: f b)
step (Just' (a :!: x)) b step (Just' (a :!: x)) b
| y < x = Just' (b :!: y) | y < x = Just' (b :!: y)
| otherwise = Just' (a :!: x) | otherwise = Just' (a :!: x)
where where
y = f b y = f b
premapMaybe :: (a -> Maybe b) -> Fold b r -> Fold a r premapMaybe :: (a -> Maybe b) -> Fold b r -> Fold a r
{-# INLINABLE premapMaybe #-} {-# INLINEABLE premapMaybe #-}
premapMaybe f (Fold step initial extract) = Fold step' initial extract premapMaybe f (Fold step initial extract) = Fold step' initial extract
where where
step' x a = case f a of step' x a = case f a of
Nothing -> x Nothing -> x
Just s -> step x s Just s -> step x s