Skip to content

Commit

Permalink
Lens.eachOption
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrimatti committed Mar 28, 2024
1 parent 12e4b38 commit f14ee89
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fi.solita.utils</groupId>
<artifactId>functional-utils</artifactId>
<version>0.12.47</version>
<version>0.12.48</version>
<build>
<resources>
<resource>
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/fi/solita/utils/functional/lens/Lens.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,32 @@ public Option<E> apply(Option<E> c) {
);
}

public static final <D,E,S> Lens<D,Option<S>> eachOption(final Lens<D,Option<E>> lens, final Lens<E,S> lens2) {
return new Lens<D,Option<S>>(
new Apply<D, Option<S>>() {
@Override
public Option<S> apply(D t) {
return t == null ? null : lens.apply(t).map(lens2);
}
},
new Function2<D, Apply<Option<S>,Option<S>>, D>() {
@Override
public D apply(D d, final Apply<Option<S>, Option<S>> f) {
return lens.modify(d, new Apply<Option<E>,Option<E>>() {
public Option<E> apply(Option<E> c) {
return c == null ? null : !c.isDefined() ? c : Some(lens2.modify(c.get(), new Apply<S, S>() {
@Override
public S apply(S t) {
return f.apply(Some(t)).get();
}
}));
};
});
}
}
);
}

/**
* @return a setter targetting each member of the collection behind {@code setter}.
*/
Expand Down

0 comments on commit f14ee89

Please sign in to comment.