我不清楚makeLense和makePrisms之间的区别吗? 我知道,当我们要访问一个嵌套的结构/数据,然后使用makeLense这样的: data Point = Point { _x :: Int, _y :: Int}
data Test= Test {_name :: String, _position :: Point}
makeLenses ''Point
makeLense
我能以恒定的指标,像这样创建列表的IndexedTraversal: constIndex :: i -> Lens.AnIndexedLens i a b a b
constIndex index f = Lens.indexed f index
constIndexedList :: i -> Lens.AnIndexedTraversal i [a] [b] a b
constInd
我想根据记录中的特定值执行排序。因此,我正在考虑将镜头传递到lensSort函数,但我一直无法使其工作。 理想的情况下,我可以做这样的事情 lensSort :: HasLens a => Lens' a b -> a -> a -> -> Ordering
lensSort lens x y | x ^. lens > y ^. lens = GT
| x ^. lens <
假设我有一个列表[A]。如果满足某个谓词,我想更新列表的特定元素。但是如果没有这样的元素,我想先添加元素列表。我目前的解决方案是手动编写函数,以便将元素插入列表中,如果它不存在,然后使用遍历来更新元素。像这样: -- inserts element to list if there's no a single element
-- which satisfies given predicate