Skip to content

Commit 82e3773

Browse files
committed
Simplify monomorphic Prism and Optional constructors.
and various small optimizations
1 parent 435cdac commit 82e3773

9 files changed

Lines changed: 167 additions & 38 deletions

File tree

core/src/main/java/fj/data/optic/Fold.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public final F<S, Option<A>> find(final F<A, Boolean> p) {
4444

4545
/** get the first target of a {@link Fold} */
4646
public final Option<A> headOption(final S s) {
47-
return find(__ -> true).f(s);
47+
return find(Function.constant(Boolean.TRUE)).f(s);
4848
}
4949

5050
/** check if at least one target satisfies the predicate */

core/src/main/java/fj/data/optic/Optional.java

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package fj.data.optic;
22

33
import fj.F;
4+
import fj.Function;
5+
import fj.P;
46
import fj.P1;
57
import fj.P2;
68
import fj.control.Trampoline;
79
import fj.control.parallel.Promise;
10+
import fj.control.parallel.Strategy;
811
import fj.data.Either;
912
import fj.data.IO;
13+
import fj.data.IOFunctions;
1014
import fj.data.List;
1115
import fj.data.Option;
1216
import fj.data.Stream;
@@ -166,9 +170,118 @@ public static <S> Optional<S, S> id() {
166170
return new Optional<>(POptional.pId());
167171
}
168172

169-
/** create a {@link Optional} using the canonical functions: getOrModify and set */
170-
public static final <S, A> Optional<S, A> optional(final F<S, Either<S, A>> getOrModify, final F<A, F<S, S>> set) {
171-
return new Optional<>(POptional.pOptional(getOrModify, set));
173+
public static final <S, A> Optional<S, A> optional(final F<S, Option<A>> getOption, final F<A, F<S, S>> set) {
174+
return new Optional<>(new POptional<S, S, A, A>() {
175+
176+
@Override
177+
public Either<S, A> getOrModify(final S s) {
178+
return getOption.f(s).option(Either.left(s), Either.<S, A> right_());
179+
}
180+
181+
@Override
182+
public F<S, S> set(final A a) {
183+
return set.f(a);
184+
}
185+
186+
@Override
187+
public Option<A> getOption(final S s) {
188+
return getOption.f(s);
189+
}
190+
191+
@Override
192+
public <C> F<S, F<C, S>> modifyFunctionF(final F<A, F<C, A>> f) {
193+
return s -> getOrModify(s).either(
194+
Function.constant(),
195+
a -> Function.compose(b -> set.f(b).f(s), f.f(a))
196+
);
197+
}
198+
199+
@Override
200+
public <L> F<S, Either<L, S>> modifyEitherF(final F<A, Either<L, A>> f) {
201+
return s -> getOrModify(s).either(
202+
Either.right_(),
203+
t -> f.f(t).right().map(b -> set.f(b).f(s))
204+
);
205+
}
206+
207+
@Override
208+
public F<S, IO<S>> modifyIOF(final F<A, IO<A>> f) {
209+
return s -> getOrModify(s).either(
210+
IOFunctions::unit,
211+
t -> IOFunctions.<A, S> map(f.f(t), b -> set.f(b).f(s))
212+
);
213+
}
214+
215+
@Override
216+
public F<S, Trampoline<S>> modifyTrampolineF(final F<A, Trampoline<A>> f) {
217+
return s -> getOrModify(s).either(
218+
Trampoline.pure(),
219+
t -> f.f(t).map(b -> set.f(b).f(s))
220+
);
221+
}
222+
223+
@Override
224+
public F<S, Promise<S>> modifyPromiseF(final F<A, Promise<A>> f) {
225+
return s -> getOrModify(s).either(
226+
t -> Promise.promise(Strategy.idStrategy(), P.p(t)),
227+
t -> f.f(t).fmap(b -> set.f(b).f(s))
228+
);
229+
}
230+
231+
@Override
232+
public F<S, List<S>> modifyListF(final F<A, List<A>> f) {
233+
return s -> getOrModify(s).either(
234+
List::single,
235+
t -> f.f(t).map(b -> set.f(b).f(s))
236+
);
237+
}
238+
239+
@Override
240+
public F<S, Option<S>> modifyOptionF(final F<A, Option<A>> f) {
241+
return s -> getOrModify(s).either(
242+
Option.some_(),
243+
t -> f.f(t).map(b -> set.f(b).f(s))
244+
);
245+
}
246+
247+
@Override
248+
public F<S, Stream<S>> modifyStreamF(final F<A, Stream<A>> f) {
249+
return s -> getOrModify(s).either(
250+
Stream.single(),
251+
t -> f.f(t).map(b -> set.f(b).f(s))
252+
);
253+
}
254+
255+
@Override
256+
public F<S, P1<S>> modifyP1F(final F<A, P1<A>> f) {
257+
return s -> getOrModify(s).either(
258+
P.p1(),
259+
t -> f.f(t).map(b -> set.f(b).f(s))
260+
);
261+
}
262+
263+
@Override
264+
public <E> F<S, Validation<E, S>> modifyValidationF(final F<A, Validation<E, A>> f) {
265+
return s -> getOrModify(s).either(
266+
t -> Validation.<E, S> success(t),
267+
t -> f.f(t).map(b -> set.f(b).f(s))
268+
);
269+
}
270+
271+
@Override
272+
public F<S, V2<S>> modifyV2F(final F<A, V2<A>> f) {
273+
return s -> getOrModify(s).either(
274+
t -> V2.p(P.p(t, t)),
275+
t -> f.f(t).map(b -> set.f(b).f(s))
276+
);
277+
}
278+
279+
@Override
280+
public F<S, S> modify(final F<A, A> f) {
281+
return s -> getOrModify(s).either(Function.identity(), a -> set.f(f.f(a)).f(s));
282+
}
283+
284+
});
172285
}
173286

174287
}

core/src/main/java/fj/data/optic/PIso.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public final F<S, T> modify(final F<A, B> f) {
126126

127127
/** set polymorphically the target of a {@link PIso} with a value */
128128
public final F<S, T> set(final B b) {
129-
return __ -> reverseGet(b);
129+
return Function.constant(reverseGet(b));
130130
}
131131

132132
/** pair two disjoint {@link PIso} */

core/src/main/java/fj/data/optic/PLens.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public F<S, T> set(final B b) {
440440

441441
@Override
442442
public <C> F<S, F<C, T>> modifyFunctionF(final F<A, F<C, B>> f) {
443-
return s -> Function.compose(b -> set.f(b).f(s), f.f(get(s)));
443+
return s -> Function.compose(b -> set.f(b).f(s), f.f(get.f(s)));
444444
}
445445

446446
@Override
@@ -495,7 +495,7 @@ public F<S, V2<T>> modifyV2F(final F<A, V2<B>> f) {
495495

496496
@Override
497497
public F<S, T> modify(final F<A, B> f) {
498-
return s -> set(f.f(get.f(s))).f(s);
498+
return s -> set.f(f.f(get.f(s))).f(s);
499499
}
500500
};
501501
}

core/src/main/java/fj/data/optic/POptional.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ public abstract class POptional<S, T, A, B> {
110110
* matching
111111
*/
112112
public final F<S, Option<T>> modifyOption(final F<A, B> f) {
113-
return s -> getOption(s).map(__ -> modify(f).f(s));
113+
return s -> getOption(s).map(Function.constant(modify(f).f(s)));
114114
}
115115

116116
/** set polymorphically the target of a {@link POptional} with a value. return empty if the {@link POptional} is not matching */
117117
public final F<S, Option<T>> setOption(final B b) {
118-
return modifyOption(__ -> b);
118+
return modifyOption(Function.constant(b));
119119
}
120120

121121
/** check if a {@link POptional} has a target */
@@ -385,89 +385,89 @@ public Option<A> getOption(final S s) {
385385

386386
@Override
387387
public <C> F<S, F<C, T>> modifyFunctionF(final F<A, F<C, B>> f) {
388-
return s -> getOrModify(s).either(
388+
return s -> getOrModify.f(s).either(
389389
Function.constant(),
390-
a -> Function.compose(b -> set(b).f(s), f.f(a))
390+
a -> Function.compose(b -> set.f(b).f(s), f.f(a))
391391
);
392392
}
393393

394394
@Override
395395
public <L> F<S, Either<L, T>> modifyEitherF(final F<A, Either<L, B>> f) {
396-
return s -> getOrModify(s).either(
396+
return s -> getOrModify.f(s).either(
397397
Either.right_(),
398-
t -> f.f(t).right().map(b -> set(b).f(s))
398+
t -> f.f(t).right().map(b -> set.f(b).f(s))
399399
);
400400
}
401401

402402
@Override
403403
public F<S, IO<T>> modifyIOF(final F<A, IO<B>> f) {
404-
return s -> getOrModify(s).either(
404+
return s -> getOrModify.f(s).either(
405405
IOFunctions::unit,
406-
t -> IOFunctions.<B, T> map(f.f(t), b -> set(b).f(s))
406+
t -> IOFunctions.<B, T> map(f.f(t), b -> set.f(b).f(s))
407407
);
408408
}
409409

410410
@Override
411411
public F<S, Trampoline<T>> modifyTrampolineF(final F<A, Trampoline<B>> f) {
412-
return s -> getOrModify(s).either(
412+
return s -> getOrModify.f(s).either(
413413
Trampoline.pure(),
414-
t -> f.f(t).map(b -> set(b).f(s))
414+
t -> f.f(t).map(b -> set.f(b).f(s))
415415
);
416416
}
417417

418418
@Override
419419
public F<S, Promise<T>> modifyPromiseF(final F<A, Promise<B>> f) {
420-
return s -> getOrModify(s).either(
420+
return s -> getOrModify.f(s).either(
421421
t -> Promise.promise(Strategy.idStrategy(), P.p(t)),
422-
t -> f.f(t).fmap(b -> set(b).f(s))
422+
t -> f.f(t).fmap(b -> set.f(b).f(s))
423423
);
424424
}
425425

426426
@Override
427427
public F<S, List<T>> modifyListF(final F<A, List<B>> f) {
428-
return s -> getOrModify(s).either(
428+
return s -> getOrModify.f(s).either(
429429
List::single,
430-
t -> f.f(t).map(b -> set(b).f(s))
430+
t -> f.f(t).map(b -> set.f(b).f(s))
431431
);
432432
}
433433

434434
@Override
435435
public F<S, Option<T>> modifyOptionF(final F<A, Option<B>> f) {
436-
return s -> getOrModify(s).either(
436+
return s -> getOrModify.f(s).either(
437437
Option.some_(),
438-
t -> f.f(t).map(b -> set(b).f(s))
438+
t -> f.f(t).map(b -> set.f(b).f(s))
439439
);
440440
}
441441

442442
@Override
443443
public F<S, Stream<T>> modifyStreamF(final F<A, Stream<B>> f) {
444-
return s -> getOrModify(s).either(
444+
return s -> getOrModify.f(s).either(
445445
Stream.single(),
446-
t -> f.f(t).map(b -> set(b).f(s))
446+
t -> f.f(t).map(b -> set.f(b).f(s))
447447
);
448448
}
449449

450450
@Override
451451
public F<S, P1<T>> modifyP1F(final F<A, P1<B>> f) {
452-
return s -> getOrModify(s).either(
452+
return s -> getOrModify.f(s).either(
453453
P.p1(),
454-
t -> f.f(t).map(b -> set(b).f(s))
454+
t -> f.f(t).map(b -> set.f(b).f(s))
455455
);
456456
}
457457

458458
@Override
459459
public <E> F<S, Validation<E, T>> modifyValidationF(final F<A, Validation<E, B>> f) {
460-
return s -> getOrModify(s).either(
460+
return s -> getOrModify.f(s).either(
461461
t -> Validation.<E, T> success(t),
462-
t -> f.f(t).map(b -> set(b).f(s))
462+
t -> f.f(t).map(b -> set.f(b).f(s))
463463
);
464464
}
465465

466466
@Override
467467
public F<S, V2<T>> modifyV2F(final F<A, V2<B>> f) {
468-
return s -> getOrModify(s).either(
468+
return s -> getOrModify.f(s).either(
469469
t -> V2.p(P.p(t, t)),
470-
t -> f.f(t).map(b -> set(b).f(s))
470+
t -> f.f(t).map(b -> set.f(b).f(s))
471471
);
472472
}
473473

core/src/main/java/fj/data/optic/PPrism.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ public final F<S, Option<T>> modifyOption(final F<A, B> f) {
151151

152152
/** set polymorphically the target of a {@link PPrism} with a value */
153153
public final F<S, T> set(final B b) {
154-
return modify(__ -> b);
154+
return modify(Function.constant(b));
155155
}
156156

157157
/** set polymorphically the target of a {@link PPrism} with a value. return empty if the {@link PPrism} is not matching */
158158
public final F<S, Option<T>> setOption(final B b) {
159-
return modifyOption(__ -> b);
159+
return modifyOption(Function.constant(b));
160160
}
161161

162162
/** check if a {@link PPrism} has a target */

core/src/main/java/fj/data/optic/PSetter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fj.data.optic;
22

33
import fj.F;
4+
import fj.Function;
45
import fj.data.Either;
56

67
/**
@@ -96,7 +97,7 @@ public F<S, T> modify(final F<A, B> f) {
9697

9798
@Override
9899
public F<S, T> set(final B b) {
99-
return modify(__ -> b);
100+
return modify.f(Function.constant(b));
100101
}
101102
};
102103
}

core/src/main/java/fj/data/optic/PTraversal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public final F<S, Option<A>> find(final F<A, Boolean> p) {
109109

110110
/** get the first target of a {@link PTraversal} */
111111
public final Option<A> headOption(final S s) {
112-
return find(__ -> true).f(s);
112+
return find(Function.constant(Boolean.TRUE)).f(s);
113113
}
114114

115115
/** check if at least one target satisfies the predicate */

core/src/main/java/fj/data/optic/Prism.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,24 @@ public static <S> Prism<S, S> id() {
9090
return new Prism<>(PPrism.pId());
9191
}
9292

93-
/** create a {@link Prism} using the canonical functions: getOrModify and reverseGet */
94-
public static <S, A> Prism<S, A> prism(final F<S, Either<S, A>> getOrModify, final F<A, S> reverseGet) {
95-
return new Prism<>(PPrism.pPrism(getOrModify, reverseGet));
93+
public static <S, A> Prism<S, A> prism(final F<S, Option<A>> getOption, final F<A, S> reverseGet) {
94+
return new Prism<>(new PPrism<S, S, A, A>() {
95+
96+
@Override
97+
public Either<S, A> getOrModify(final S s) {
98+
return getOption.f(s).option(Either.left(s), Either.<S, A> right_());
99+
}
100+
101+
@Override
102+
public S reverseGet(final A a) {
103+
return reverseGet.f(a);
104+
}
105+
106+
@Override
107+
public Option<A> getOption(final S s) {
108+
return getOption.f(s);
109+
}
110+
});
96111
}
97112

98113
}

0 commit comments

Comments
 (0)