使用Maybe和Writer过滤列表并跟踪过滤器命中

时间:2013-05-26 17:12:20

标签: haskell filter monads writer maybe

我使用返回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)])

2 个答案:

答案 0 :(得分:4)

当你说:

时,你比你意识到的更近了
  

但输入为[1..6]时如何得到这样的结果:

([2,3,4], fromList[("bigOne", 1), ("lessFive", 2)])

换句话说,你想要一个以列表作为输入并返回列表和地图作为输出的东西:

newtype Filter a = Filter { runFilter :: [a] -> (CountMap, [a]) }

为什么不直接使用您想要的表示法对所有过滤器进行编码:

import Data.List (partition)
import qualified Data.Map as M
import Data.Monoid

newtype CountMap = CountMap (M.Map String Int)

instance Show CountMap where
    show (CountMap m) = show m

instance Monoid CountMap where
    mempty = CountMap M.empty
    mappend (CountMap x) (CountMap y) = CountMap (M.unionWith (+) x y)

filterOn :: String -> (a -> Bool) -> Filter a
filterOn str pred = Filter $ \as ->
    let (pass, fail) = partition pred as
    in  (CountMap (M.singleton str (length fail)), pass)

bigOne :: Filter Int
bigOne = filterOn "bigOne" (> 1)

lessFive :: Filter Int
lessFive = filterOn "lessFive" (< 5)

我们错过了一个难题:如何组合过滤器。好吧,事实证明我们的Filter类型是Monoid

instance Monoid (Filter a) where
    mempty = Filter (\as -> (mempty, as))
    mappend (Filter f) (Filter g) = Filter $ \as0 ->
        let (map1, as1) = f as0
            (map2, as2) = g as1
        in  (map1 <> map2, as2)

有经验的读者会认识到这只是伪装的State monad。

这样可以轻松地使用(<>)(即mappend)组合过滤器,我们只需展开Filter类型即可运行它们:

ghci> runFilter (bigOne <> lessFive) [1..6]
(fromList [("bigOne",1),("lessFive",2)],[2,3,4])

这表明最佳路径是最直接的路径!

答案 1 :(得分:2)

好吧所以这里的问题是短路的使用正在阻碍你构建的CountMap。一个简单的例子

test :: MaybeT (Writer [String]) ()
test = do
       tell ["Blah"] >> mzero
       tell ["Blah"] >> mzero
       tell ["Blah"] >> mzero
       tell ["Blah"] >> mzero


Prelude> runWriter (runMaybeT test)
   (Nothing, ["Blah"])

看到问题?

修复它非常简单,只是不要依赖于短路:)

实施例*:

bigOneMW n | n > 1     = return True
           | otherwise = tell "bigOne" >> return False
lessFiveMW n | n < 5     = return True
             | otherwise = tell "lessFive" >> return False
chainMWBool n = liftM2 (&&) (bigOneMW n) (lessFiveMW n)
chainerMW ns = filterM chainMWBool ns

当然,MaybeT层有点没用,所以我们可以放弃它。

令人高兴的是,这不会影响上述任何代码。

*您会注意到tell只是使用普通字符串,为此,我使用语言扩展OverloadedStrings并定义了类型类{{1}的实例来自IsString。如果你很好奇,那么使这项工作的代码看起来像这样:

Data.String

你是否喜欢这个特殊技巧取决于你:)

毕竟代码已完成:http://hpaste.org/88624