FindBy / FindByLens for Array / List
This release adds a lens for finding elements in an Array
or List
.
Either directly, if you can provide the full item to find
Lens.set(
Lens.List.find(5) >>- Lens.Option.orExn,
200,
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
)
|> expect
|> toEqual([0, 1, 2, 3, 4, 200, 6, 7, 8, 9, 10])
Or by providing an additional lens to find something by a record.
[@pancake]
type user = {
id: int,
username: string,
};
let users = [
{id: 0, username: "0"},
{id: 1, username: "1"},
{id: 2, username: "2"},
{id: 3, username: "3"},
];
Lens.view(Lens.List.findByLens(0, userIdLens), users)
|> expect
|> toEqual(Some({id: 0, username: "0"}))
Checkout the tests for lists, arrays, or this arbitrarily complicated nested thing to see some more examples (and how they compose into Lens.Option
to add fallback values, or throw exceptions when they're not found