添加和删​​除列表项

时间:2018-02-10 13:01:47

标签: monocle-scala

大学example解释了如何添加和删除地图项目:

(departments composeLens at("Physics")).set(Some(physics))(uni)
(departments composeLens at("History")).set(None)(uni)

但这不适用于列表:

(lecturers composeOptional index(2)).set(Lecturer("New", "Lecturer", 50))(dep)
(lecturers composeOptional index(0)).set(None)(dep)

添加无效,删除会引发编译错误。

1 个答案:

答案 0 :(得分:1)

由于OP中没有明确的问题,我将尝试回答几个可能的问题:

  1. 为什么第一对线不起作用,而第二对线不起作用?

答案在"What is the difference between at and index? When should I use one or the other?" halfway across the page中给出:

  

换句话说,index可以更新任何现有值,而at也可以insertdelete

  1. 如何在列表中添加/删除项目?

就在上面引用的文本下方:

  

由于indexat弱,因此我们可以在比Index更多的数据结构上实现At的实例。例如,ListVector仅具有Index的实例,因为无法在序列的任意索引处插入元素。

所以可能不可能... 我没有Monocle在这里测试一些东西。

相关问题