diff --git a/pom.xml b/pom.xml index fe769d1..02a5294 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 fi.solita.utils functional-utils - 0.12.47 + 0.12.48 diff --git a/src/main/java/fi/solita/utils/functional/lens/Lens.java b/src/main/java/fi/solita/utils/functional/lens/Lens.java index ef5b222..647f474 100644 --- a/src/main/java/fi/solita/utils/functional/lens/Lens.java +++ b/src/main/java/fi/solita/utils/functional/lens/Lens.java @@ -67,6 +67,32 @@ public Option apply(Option c) { ); } + public static final Lens> eachOption(final Lens> lens, final Lens lens2) { + return new Lens>( + new Apply>() { + @Override + public Option apply(D t) { + return t == null ? null : lens.apply(t).map(lens2); + } + }, + new Function2,Option>, D>() { + @Override + public D apply(D d, final Apply, Option> f) { + return lens.modify(d, new Apply,Option>() { + public Option apply(Option c) { + return c == null ? null : !c.isDefined() ? c : Some(lens2.modify(c.get(), new Apply() { + @Override + public S apply(S t) { + return f.apply(Some(t)).get(); + } + })); + }; + }); + } + } + ); + } + /** * @return a setter targetting each member of the collection behind {@code setter}. */