|
| 1 | +package fj.data.optic; |
| 2 | + |
| 3 | +import fj.F; |
| 4 | +import fj.Function; |
| 5 | +import fj.Monoid; |
| 6 | +import fj.data.Either; |
| 7 | +import fj.data.List; |
| 8 | +import fj.data.Option; |
| 9 | + |
| 10 | +/** |
| 11 | + * A {@link Fold} can be seen as a {@link Getter} with many targets or a weaker {@link PTraversal} which cannot modify its |
| 12 | + * target. |
| 13 | + * |
| 14 | + * {@link Fold} is on the top of the Optic hierarchy which means that {@link Getter}, {@link PTraversal}, {@link POptional}, |
| 15 | + * {@link PLens}, {@link PPrism} and {@link PIso} are valid {@link Fold} |
| 16 | + * |
| 17 | + * @param <S> the source of a {@link Fold} |
| 18 | + * @param <A> the target of a {@link Fold} |
| 19 | + */ |
| 20 | +public abstract class Fold<S, A> { |
| 21 | + |
| 22 | + /** |
| 23 | + * map each target to a {@link Monoid} and combine the results underlying representation of {@link Fold}, all {@link Fold} |
| 24 | + * methods are defined in terms of foldMap |
| 25 | + */ |
| 26 | + public abstract <M> F<S, M> foldMap(Monoid<M> m, F<A, M> f); |
| 27 | + |
| 28 | + /** combine all targets using a target's {@link Monoid} */ |
| 29 | + public final F<S, A> fold(final Monoid<A> m) { |
| 30 | + return foldMap(m, Function.identity()); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * get all the targets of a {@link Fold} TODO: Shall it return a Stream as there might be an infinite number of targets? |
| 35 | + */ |
| 36 | + public final List<A> getAll(final S s) { |
| 37 | + return foldMap(Monoid.listMonoid(), List::single).f(s); |
| 38 | + } |
| 39 | + |
| 40 | + /** find the first target of a {@link Fold} matching the predicate */ |
| 41 | + public final F<S, Option<A>> find(final F<A, Boolean> p) { |
| 42 | + return foldMap(Monoid.optionMonoid(), a -> p.f(a) ? Option.some(a) : Option.none()); |
| 43 | + } |
| 44 | + |
| 45 | + /** get the first target of a {@link Fold} */ |
| 46 | + public final Option<A> headOption(final S s) { |
| 47 | + return find(__ -> true).f(s); |
| 48 | + } |
| 49 | + |
| 50 | + /** check if at least one target satisfies the predicate */ |
| 51 | + public final F<S, Boolean> exist(final F<A, Boolean> p) { |
| 52 | + return foldMap(Monoid.disjunctionMonoid, p); |
| 53 | + } |
| 54 | + |
| 55 | + /** check if all targets satisfy the predicate */ |
| 56 | + public final F<S, Boolean> all(final F<A, Boolean> p) { |
| 57 | + return foldMap(Monoid.conjunctionMonoid, p); |
| 58 | + } |
| 59 | + |
| 60 | + /** join two {@link Fold} with the same target */ |
| 61 | + public final <S1> Fold<Either<S, S1>, A> sum(final Fold<S1, A> other) { |
| 62 | + return new Fold<Either<S, S1>, A>() { |
| 63 | + @Override |
| 64 | + public <B> F<Either<S, S1>, B> foldMap(final Monoid<B> m, final F<A, B> f) { |
| 65 | + return s -> s.either(Fold.this.foldMap(m, f), other.foldMap(m, f)); |
| 66 | + } |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + /**********************************************************/ |
| 71 | + /** Compose methods between a {@link Fold} and another Optics */ |
| 72 | + /**********************************************************/ |
| 73 | + |
| 74 | + /** compose a {@link Fold} with a {@link Fold} */ |
| 75 | + public final <B> Fold<S, B> composeFold(final Fold<A, B> other) { |
| 76 | + return new Fold<S, B>() { |
| 77 | + @Override |
| 78 | + public <C> F<S, C> foldMap(final Monoid<C> m, final F<B, C> f) { |
| 79 | + return Fold.this.<C> foldMap(m, other.<C> foldMap(m, f)); |
| 80 | + } |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + /** compose a {@link Fold} with a {@link Getter} */ |
| 85 | + public final <C> Fold<S, C> composeGetter(final Getter<A, C> other) { |
| 86 | + return composeFold(other.asFold()); |
| 87 | + } |
| 88 | + |
| 89 | + /** compose a {@link Fold} with a {@link POptional} */ |
| 90 | + public final <B, C, D> Fold<S, C> composeOptional(final POptional<A, B, C, D> other) { |
| 91 | + return composeFold(other.asFold()); |
| 92 | + } |
| 93 | + |
| 94 | + /** compose a {@link Fold} with a {@link PPrism} */ |
| 95 | + public final <B, C, D> Fold<S, C> composePrism(final PPrism<A, B, C, D> other) { |
| 96 | + return composeFold(other.asFold()); |
| 97 | + } |
| 98 | + |
| 99 | + /** compose a {@link Fold} with a {@link PLens} */ |
| 100 | + public final <B, C, D> Fold<S, C> composeLens(final PLens<A, B, C, D> other) { |
| 101 | + return composeFold(other.asFold()); |
| 102 | + } |
| 103 | + |
| 104 | + /** compose a {@link Fold} with a {@link PIso} */ |
| 105 | + public final <B, C, D> Fold<S, C> composeIso(final PIso<A, B, C, D> other) { |
| 106 | + return composeFold(other.asFold()); |
| 107 | + } |
| 108 | + |
| 109 | + public static final <A> Fold<Either<A, A>, A> codiagonal() { |
| 110 | + return new Fold<Either<A, A>, A>() { |
| 111 | + @Override |
| 112 | + public <B> F<Either<A, A>, B> foldMap(final Monoid<B> m, final F<A, B> f) { |
| 113 | + return e -> e.either(f, f); |
| 114 | + } |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments