Add new files
This commit is contained in:
@@ -5,6 +5,12 @@ module FoldableHelp
|
||||
where
|
||||
import Data.Foldable
|
||||
|
||||
-- TODO check up whether and how it is necessary to specialise this
|
||||
-- | A version of 'minimum' where the comparison is done on some extracted value.
|
||||
-- Returns Nothing if the list is empty. Only calls the function once per element.
|
||||
--
|
||||
-- > safeMinimumOn id [] == Nothing
|
||||
-- > safeMinimumOn length ["test","extra","a"] == Just "a"
|
||||
safeMinimumOn :: (Foldable t,Ord b) => (a -> b) -> t a -> Maybe a
|
||||
safeMinimumOn f = foldl' g Nothing
|
||||
where
|
||||
@@ -12,3 +18,11 @@ safeMinimumOn f = foldl' g Nothing
|
||||
| f x < f y = Just x
|
||||
| otherwise = Just y
|
||||
g Nothing y = Just y
|
||||
--{- | Partial. -}
|
||||
--minimumOn :: Ord b => (a -> b) -> [a] -> a
|
||||
--minimumOn f [] = error "tried to take minimumOn of empty list"
|
||||
--minimumOn f (x:xs) = go x xs
|
||||
-- where
|
||||
-- go x (y:xs) | f x < f y = go x xs
|
||||
-- | otherwise = go y xs
|
||||
-- go x [] = x
|
||||
|
||||
Reference in New Issue
Block a user