Tamriel
Tamriel

Reputation: 1427

Add and delete List items

The university example explains how to add and delete items of a map:

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

This does not work with Lists, though:

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

Adding does nothing, deleting throws a compilation error.

Edit: By now, I use quicklens, which is able to modify sequences.

Upvotes: 4

Views: 439

Answers (1)

Since there is no explicit question in the OP, I will try answering a couple of possible questions:

  1. Why do the first pair of lines work, and the second pair does not?

The answer is given in "What is the difference between at and index? When should I use one or the other?" halfway across the page:

In other words, index can update any existing values while at can also insert and delete.

  1. How can I add/remove items to/from a List?

Just below the text quoted above:

Since index is weaker than at, we can implement an instance of Index on more data structure than At. For instance, List or Vector only have an instance of Index because there is no way to insert an element at an arbitrary index of a sequence.

So it may not be possible... I have no Monocle here to test a few things, though.

Upvotes: 1

Related Questions