如何结合过滤条件 [英] How do you combine filter conditions
问题描述
函数的过滤器类采用条件(a-> Bool)并在过滤时应用该条件.
The filter class of functions takes a condition (a -> Bool) and applies it when filtering.
在多种情况下使用过滤器的最佳方法是什么?
What is the best way to use a filter on when you have multiple conditions?
使用了应用功能 liftA2而不是liftM2,因为我出于某种原因不了解liftM2是如何在纯代码中工作的.
Used the applicative function liftA2 instead of liftM2 because I for some reason didn't understand how liftM2 worked within pure code.
推荐答案
可以在Reader monad中使用liftM2组合器,以更实用"的方式进行此操作:
The liftM2 combinator can be used in the Reader monad to do this in a 'more functional' way:
import Control.Monad
import Control.Monad.Reader
-- ....
filter (liftM2 (&&) odd (> 100)) [1..200]
请注意,进口很重要; Control.Monad.Reader提供Monad(e->)实例,以使所有这些工作正常进行.
Note that the imports are important; Control.Monad.Reader provides the Monad (e ->) instance that makes this all work.
之所以如此,是因为读者monad在某些环境e下才有效(e->).因此,布尔谓词是一个0元单子函数,在与其自变量相对应的环境中返回布尔值.然后,我们可以使用liftM2在两个这样的谓词上分布环境.
The reason this works is the reader monad is just (e ->) for some environment e. Thus, a boolean predicate is a 0-ary monadic function returning bool in an environment corresponding to its argument. We can then use liftM2 to distribute the environment over two such predicates.
或者,用更简单的术语来说,当类型确定时,liftM2会像这样工作:
Or, in simpler terms, liftM2 will act a bit like this when the types work out:
liftM2 f g h a = f (g a) (h a)
如果您希望能够轻松地将它们链接起来,并且/或者不希望与liftM2发生冲突,那么还可以定义一个新的组合器:
You can also define a new combinator if you want to be able to chain these easily, and/or don't want to mess with liftM2:
(.&&.) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool)
(.&&.) f g a = (f a) && (g a)
-- or, in points-free style:
(.&&.) = liftM2 (&&)
filter (odd .&&. (> 5) .&&. (< 20)) [1..100]
这篇关于如何结合过滤条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!