public static <X> Lens.Simple<List<X>, Maybe<X>> elementAt(int index) {
        return simpleLens(xs -> maybe(xs.size() > index ? xs.get(index) : null),
                          (xs, maybeX) -> {
                              List<X> updated = new ArrayList<>(xs);
                              return maybeX.fmap(x -> {
                                  int minimumSize = index + 1;
                                  if (minimumSize > xs.size()) {
                                      take(abs(updated.size() - minimumSize), repeat((X) null)).forEach(updated::add);
                                  }
                                  updated.set(index, x);
                                  return updated;
                              }).orElseGet(() -> {
                                  if (updated.size() > index) {
                                      updated.set(index, null);
                                  }
                                  return updated;
                              });
                          });
    }
public static < X > Lens . Simple < List < X > , Maybe < X > > elementAt ( int index ) { return simpleLens ( xs -> maybe ( xs . size ( ) > index ? xs . get ( index ) : null ) , ( xs , maybeX ) -> { List < X > updated = new ArrayList < > ( xs ) ; return maybeX . fmap ( x -> { int minimumSize = index + 1 ; if ( minimumSize > xs . size ( ) ) { take ( abs ( updated . size ( ) - minimumSize ) , repeat ( ( X ) null ) ) . forEach ( updated :: add ) ; } updated . set ( index , x ) ; return updated ; } ) . orElseGet ( ( ) -> { if ( updated . size ( ) > index ) { updated . set ( index , null ) ; } return updated ; } ) ; } ) ; }
Convenience static factory method for creating a lens that focuses on an element in a list at a particular index.
Wraps result in a {@link Maybe} to handle null values or indexes that fall outside of list boundaries.

@param index the index to focus on
@param <X>   the list element type
@return Maybe the element at the index