126126 * <a href="http://skillsmatter.com/podcast/scala/lenses-compositional-data-access-and-manipulation">about</a>
127127 * <a href="http://r6.ca/blog/20120623T104901Z.html">lenses</a>.
128128 *
129- * @param <S> The type of the "larger" value for reading
130- * @param <T> The type of the "larger" value for putting
131- * @param <A> The type of the "smaller" value that is read
132- * @param <B> The type of the "smaller" update value
129+ * @param <S> the type of the "larger" value for reading
130+ * @param <T> the type of the "larger" value for putting
131+ * @param <A> the type of the "smaller" value that is read
132+ * @param <B> the type of the "smaller" update value
133133 */
134134@ FunctionalInterface
135135public interface Lens <S , T , A , B > extends Functor <T > {
@@ -142,8 +142,8 @@ public interface Lens<S, T, A, B> extends Functor<T> {
142142 * Although the Java type system does not allow enforceability, the functor instance FT should be the same as FB,
143143 * only differentiating in their parameters.
144144 *
145- * @param <FT> The type of the lifted T
146- * @param <FB> The type of the lifted B
145+ * @param <FT> the type of the lifted T
146+ * @param <FB> the type of the lifted B
147147 * @return the lens, "fixed" to the functor
148148 */
149149 default <FT extends Functor <T >, FB extends Functor <B >> Fixed <S , T , A , B , FT , FB > fix () {
@@ -152,7 +152,51 @@ default <FT extends Functor<T>, FB extends Functor<B>> Fixed<S, T, A, B, FT, FB>
152152
153153 @ Override
154154 default <U > Lens <S , U , A , B > fmap (Function <? super T , ? extends U > fn ) {
155- return this .compose (Lens .<S , U , S , T >lens (id (), (s , t ) -> fn .apply (t )));
155+ return compose (Lens .<S , U , S , T >lens (id (), (s , t ) -> fn .apply (t )));
156+ }
157+
158+ /**
159+ * Contravariantly map <code>S</code> to <code>R</code>, yielding a new lens.
160+ *
161+ * @param fn the mapping function
162+ * @param <R> the type of the new "larger" value for reading
163+ * @return the new lens
164+ */
165+ default <R > Lens <R , T , A , B > mapS (Function <? super R , ? extends S > fn ) {
166+ return compose (lens (fn , (r , t ) -> t ));
167+ }
168+
169+ /**
170+ * Covariantly map <code>T</code> to <code>U</code>, yielding a new lens.
171+ *
172+ * @param fn the mapping function
173+ * @param <U> the type of the new "larger" value for putting
174+ * @return the new lens
175+ */
176+ default <U > Lens <S , U , A , B > mapT (Function <? super T , ? extends U > fn ) {
177+ return fmap (fn );
178+ }
179+
180+ /**
181+ * Covariantly map <code>A</code> to <code>C</code>, yielding a new lens.
182+ *
183+ * @param fn the mapping function
184+ * @param <C> the type of the new "smaller" value that is read
185+ * @return the new lens
186+ */
187+ default <C > Lens <S , T , C , B > mapA (Function <? super A , ? extends C > fn ) {
188+ return andThen (lens (fn , (a , b ) -> b ));
189+ }
190+
191+ /**
192+ * Contravariantly map <code>B</code> to <code>Z</code>, yielding a new lens.
193+ *
194+ * @param fn the mapping function
195+ * @param <Z> the type of the new "smaller" update value
196+ * @return the new lens
197+ */
198+ default <Z > Lens <S , T , A , Z > mapB (Function <? super Z , ? extends B > fn ) {
199+ return andThen (lens (id (), (a , z ) -> fn .apply (z )));
156200 }
157201
158202 /**
@@ -184,10 +228,10 @@ default <Q, R> Lens<Q, R, A, B> compose(Lens<Q, R, S, T> g) {
184228 *
185229 * @param getter the getter function
186230 * @param setter the setter function
187- * @param <S> The type of the "larger" value for reading
188- * @param <T> The type of the "larger" value for putting
189- * @param <A> The type of the "smaller" value that is read
190- * @param <B> The type of the "smaller" update value
231+ * @param <S> the type of the "larger" value for reading
232+ * @param <T> the type of the "larger" value for putting
233+ * @param <A> the type of the "smaller" value that is read
234+ * @param <B> the type of the "smaller" update value
191235 * @return the lens
192236 */
193237 static <S , T , A , B > Lens <S , T , A , B > lens (Function <? super S , ? extends A > getter ,
@@ -207,8 +251,8 @@ public <FT extends Functor<T>, FB extends Functor<B>> FT apply(Function<? super
207251 *
208252 * @param getter the getter function
209253 * @param setter the setter function
210- * @param <S> The type of both "larger" values
211- * @param <A> The type of both "smaller" values
254+ * @param <S> the type of both "larger" values
255+ * @param <A> the type of both "smaller" values
212256 * @return the lens
213257 */
214258 @ SuppressWarnings ("unchecked" )
@@ -221,8 +265,8 @@ static <S, A> Lens.Simple<S, A> simpleLens(Function<? super S, ? extends A> gett
221265 * A convenience type with a simplified type signature for common lenses with both unified "larger" values and
222266 * unified "smaller" values.
223267 *
224- * @param <S> The type of both "larger" values
225- * @param <A> The type of both "smaller" values
268+ * @param <S> the type of both "larger" values
269+ * @param <A> the type of both "smaller" values
226270 */
227271 @ FunctionalInterface
228272 interface Simple <S , A > extends Lens <S , S , A , A > {
@@ -244,10 +288,10 @@ default <B> Lens.Simple<S, B> andThen(Lens.Simple<A, B> f) {
244288 /**
245289 * A convenience type with a simplified type signature for fixed simple lenses.
246290 *
247- * @param <S> The type of both "larger" values
248- * @param <A> The type of both "smaller" values
249- * @param <FS> The type of the lifted s
250- * @param <FA> The type of the lifted A
291+ * @param <S> the type of both "larger" values
292+ * @param <A> the type of both "smaller" values
293+ * @param <FS> the type of the lifted s
294+ * @param <FA> the type of the lifted A
251295 */
252296 @ FunctionalInterface
253297 interface Fixed <S , A , FS extends Functor <S >, FA extends Functor <A >>
@@ -259,12 +303,12 @@ interface Fixed<S, A, FS extends Functor<S>, FA extends Functor<A>>
259303 * A lens that has been fixed to a functor. Because the lens is no longer polymorphic, it can additionally be safely
260304 * represented as an Fn2.
261305 *
262- * @param <S> The type of the "larger" value for reading
263- * @param <T> The type of the "larger" value for putting
264- * @param <A> The type of the "smaller" value that is read
265- * @param <B> The type of the "smaller" update value
266- * @param <FT> The type of the lifted T
267- * @param <FB> The type of the lifted B
306+ * @param <S> the type of the "larger" value for reading
307+ * @param <T> the type of the "larger" value for putting
308+ * @param <A> the type of the "smaller" value that is read
309+ * @param <B> the type of the "smaller" update value
310+ * @param <FT> the type of the lifted T
311+ * @param <FB> the type of the lifted B
268312 */
269313 @ FunctionalInterface
270314 interface Fixed <S , T , A , B , FT extends Functor <T >, FB extends Functor <B >>
0 commit comments