java.lang.Object
com.jnape.palatable.lambda.optics.lenses.ListLens

public final class ListLens extends Object
Lenses that operate on Lists.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <X> Lens.Simple<List<X>,List<X>>
    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, returning defaultValue if there is no value at that index.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ListLens

      private ListLens()
  • Method Details

    • asCopy

      public static <X> Lens.Simple<List<X>,List<X>> 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

      public 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. Wraps result in a Maybe 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

      public 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, returning defaultValue 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 where S is the empty list

      Type Parameters:
      X - the list element type
      Parameters:
      index - the index to focus on
      defaultValue - the value to use if there is no element at index
      Returns:
      the element at the index, or defaultValue