我正在使用返回 Maybe 元素的链式函数来过滤列表。这部分工作正常。
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, OverlappingInstances #-}
import Control.Monad
import Control.Monad.Trans.Maybe
import Control.Monad.Writer
import Data.Map (Map, alter, empty, unionWith)
------------------------------------------------
main = do
let numberList = [1..6]
let result = filter ((\z -> case z of Just _ -> True; Nothing -> False) . numFilter) numberList
(putStrLn . show) result
{-
[2,3,4]
-}
--- Maybe
bigOne :: Int -> Maybe Int
bigOne n | n > 1 = Just n
| otherwise = Nothing
lessFive :: Int -> Maybe Int
lessFive n | n < 5 = Just n
| otherwise = Nothing
numFilter :: Int -> Maybe Int
numFilter num = bigOne num
>>= lessFive
但是我也想计算不同函数捕获元素的时间。我现在正在使用带有地图的 Writer 来收集点击量。我尝试将其包装在 MaybeT 中,但这会导致整个过滤器在出现不需要的元素并返回和空列表的情况下失败。
-------------------------------
type FunctionName = String
type Count = Int
type CountMap = Map FunctionName Count
instance Monoid CountMap where
mempty = empty :: CountMap
-- default mappend on maps overwrites values with same key,
-- this increments them
mappend x y = unionWith (+) x y
{-
Helper monad to track the filter hits.
-}
type CountWriter = Writer CountMap
incrementCount :: String -> CountMap
incrementCount key = alter addOne key empty
addOne :: Maybe Int -> Maybe Int
addOne Nothing = Just 1
addOne (Just n) = Just (n + 1)
bigOneMW :: Int -> MaybeT CountWriter Int
bigOneMW n | n > 1 = MaybeT $ return (Just n)
| otherwise = do
tell (incrementCount "bigOne")
MaybeT $ return Nothing
lessFiveMW :: Int -> MaybeT CountWriter Int
lessFiveMW n | n < 5 = MaybeT $ return (Just n)
| otherwise = do
tell (incrementCount "lessFive")
MaybeT $ return Nothing
chainMWBool :: Int -> MaybeT CountWriter Bool
chainMWBool n = do
a <- bigOneMW n
b <- lessFiveMW a
return True
chainerMW :: [Int] -> MaybeT CountWriter [Int]
chainerMW ns = do
result <- filterM chainMWBool ns
return result
{-
> runWriter (runMaybeT (chainerMW [1..3]))
(Nothing,fromList [("bigOne",1)])
> runWriter (runMaybeT (chainerMW [2..5]))
(Nothing,fromList [("lessFive",1)])
> runWriter (runMaybeT (chainerMW [2..4]))
(Just [2,3,4],fromList [])
-}
我只是不知道如何让它做我想做的事。我猜我正在寻找的类型签名是[Int] -> CountWriter [Int]
,但是当输入是[1..6]
:
([2,3,4], fromList[("bigOne", 1), ("lessFive", 2)])