容器中有什么东西可以让我获得Data.Set的子集吗?

时间:2014-04-03 19:53:59

标签: haskell containers

我找不到任何给定min和max的函数返回容器中任何结构的该间隔中的所有值。当然必须有......?

2 个答案:

答案 0 :(得分:6)

inRange :: Ord a => a -> a -> Set a -> Set a
inRange min max s = fst $ S.split max gt
  where (_, gt) = S.split min s

答案 1 :(得分:1)

您可以使用Data.Set.filter

轻松编写此内容
import Data.Set (Set)
import qualified Data.Set as S

between :: Ord a => a -> a -> Set a -> Set a
between min_ max_ = S.filter (\x -> min_ <= x && x <= max_)

然后你可以用它作为

> between 3 7 $ S.fromList [1..10]
fromList [3, 4, 5, 6, 7]