我有这样的代码来转换一些数据:
newtype XXX = XXX
{ xxx :: [ Text ]
} deriving (Eq, Show)
oldToNew
:: [XXX] -> [(Int, [(Int, Text)])]
-> [[(Int, Int, Text)]]
oldToNew xs old = map go1 xs
where
go1 (XXX eqs) = foldr go2 [] eqs
go2 l acc = foldr (go3 l) acc old
go3 l (i, w) acc = foldr (go4 i l) acc w
go4 idx1 label1 (idx2, label2) acc =
if label1 == label2
then (idx1, idx2, label2):acc
else acc
字符串
注意两件事:
1.它将一个累加器(最初是空的)穿过所有折叠,
1.每个折叠从前一个折叠中拾取一个参数传递到下一个“go”
如何使用控制透镜和/或光学来完成上述操作。
测试用例:
oldToNewSpec :: Spec
oldToNewSpec =
it "oldToNew" $
oldToNew [ XXX ["this", "that", "also"]
, XXX ["here", "there"] ]
[ (0, [(4, "this")])
, (0, [(1, "here")])
, (1, [(5, "that")])
, (2, [(2, "also")])
, (2, [(3, "there")]) ]
`shouldBe`
[ [(0,4,"this"),(1,5,"that"),(2,2,"also")]
, [(0,1,"here"),(2,3,"there")] ]
型
1条答案
按热度按时间nr7wwzry1#
我不认为你需要透镜来使这个更可读。列表理解应该让你在你需要去的地方没有太多的麻烦。
字符串
使用适当的数据结构将使事情更有效,并且可能更具可读性。
型