Class ListLens
java.lang.Object
com.jnape.palatable.lambda.optics.lenses.ListLens
Lenses that operate on
List
s.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <X> Lens.Simple
<List<X>, List<X>> asCopy()
Convenience static factory method for creating a lens over a copy of a list.static <X> Lens.Simple
<List<X>, Maybe<X>> elementAt
(int index) Convenience static factory method for creating a lens that focuses on an element in a list at a particular index.static <X> Lens.Simple
<List<X>, X> elementAt
(int index, X defaultValue) Convenience static factory method for creating a lens that focuses on an element in a list at a particular index, returningdefaultValue
if there is no value at that index.
-
Constructor Details
-
ListLens
private ListLens()
-
-
Method Details
-
asCopy
Convenience static factory method for creating a lens over a copy of a list. Useful for composition to avoid mutating a list reference.- Type Parameters:
X
- the list element type- Returns:
- a lens that focuses on copies of lists
-
elementAt
Convenience static factory method for creating a lens that focuses on an element in a list at a particular index. Wraps result in aMaybe
to handle null values or indexes that fall outside of list boundaries.- Type Parameters:
X
- the list element type- Parameters:
index
- the index to focus on- Returns:
- Maybe the element at the index
-
elementAt
Convenience static factory method for creating a lens that focuses on an element in a list at a particular index, returningdefaultValue
if there is no value at that index.Note that this lens is NOT lawful, since "putting back what you got changes nothing" fails for any value
B
whereS
is the empty list- Type Parameters:
X
- the list element type- Parameters:
index
- the index to focus ondefaultValue
- the value to use if there is no element at index- Returns:
- the element at the index, or defaultValue
-