我似乎不明白IxSet
中的文字索引应该如何运作。在这里,我创建了一个单元测试,它按tags
字段对项目进行索引,然后按标签“tag”查询项目。它应该(从我的角度来看)给我回复所有3项(因为它们都有标签“tag”),但似乎他们没有。我不明白为什么。
{-# LANGUAGE DeriveDataTypeable #-}
import Test.HUnit
import Data.IxSet
import Data.List.Split ( splitOn )
import Data.Data ( Data, Typeable )
data TaggedItem = TaggedItem { getTags :: String }
deriving ( Show, Ord, Eq, Data, Typeable )
data Tag = Tag String
deriving ( Show, Ord, Eq, Data, Typeable )
getTagStrings :: TaggedItem -> [Tag]
getTagStrings = map Tag . splitOn "," . getTags
instance Indexable TaggedItem where
empty = ixSet
[ ixFun getTagStrings ]
test3 = TestCase (
assertEqual "ixFun multiple tags test"
3
(size (index @= (Tag "tag"))) )
where items = [ TaggedItem "tag,tag1,tag2"
, TaggedItem "tag,tag1,tag2"
, TaggedItem "tag,tag1,tag2" ]
index = fromList items
tests = TestList [
-- TestLabel "test1" test1,
-- TestLabel "test2" test2,
TestLabel "test3" test3]
main = do
runTestTT tests
更新
嗯,非常有趣。我添加了字段“名称”,并在每个项目名称上做了不同的,现在它可以工作。{-# LANGUAGE DeriveDataTypeable #-}
import Test.HUnit
import Data.IxSet
import Data.List.Split ( splitOn )
import Data.Data ( Data, Typeable )
newtype TaggedItemName = TaggedItemName { unTaggedItemName :: String }
deriving ( Show, Ord, Eq, Data, Typeable )
data TaggedItem = TaggedItem { getName :: TaggedItemName, getTags :: String }
deriving ( Show, Ord, Eq, Data, Typeable )
data Tag = Tag String
deriving ( Show, Ord, Eq, Data, Typeable )
getTagStrings :: TaggedItem -> [Tag]
getTagStrings = map Tag . splitOn "," . getTags
instance Indexable TaggedItem where
empty = ixSet
[ ixFun getTagStrings ]
test3 = TestCase (
assertEqual "ixFun multiple tags test"
3
(size (index @= (Tag "tag"))) )
where items = [ TaggedItem (TaggedItemName "name1") "tag,tag1,tag2"
, TaggedItem (TaggedItemName "name2") "tag,tag1,tag2"
, TaggedItem (TaggedItemName "name3") "tag,tag1,tag2" ]
index = fromList items
tests = TestList [
-- TestLabel "test1" test1,
-- TestLabel "test2" test2,
TestLabel "test3" test3]
main = do
runTestTT tests
作品。所以也许IxSet认为这些项目是相同的并以某种方式合并它们(?)
答案 0 :(得分:4)
是的,似乎IxSet合并了相等的值。我拿了第一个例子并改变了它以便:
where items = [ TaggedItem "tag,tag1,tag2"
, TaggedItem "tag,tag3,tag4"
, TaggedItem "tag,tag5,tag6" ]
现在它也有效。