Skip to content

Commit cda40e3

Browse files
committed
creating singleton instances for all builtin functions
1 parent 03c2949 commit cda40e3

33 files changed

+127
-35
lines changed

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn1/Constantly.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
public final class Constantly<A, B> implements Fn2<A, B, A> {
1313

14+
private static final Constantly INSTANCE = new Constantly();
15+
1416
private Constantly() {
1517
}
1618

@@ -19,8 +21,9 @@ public A apply(A a, B b) {
1921
return a;
2022
}
2123

24+
@SuppressWarnings("unchecked")
2225
public static <A, B> Constantly<A, B> constantly() {
23-
return new Constantly<>();
26+
return INSTANCE;
2427
}
2528

2629
public static <A, B> Fn1<B, A> constantly(A a) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn1/Cycle.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
public final class Cycle<A> implements Fn1<Iterable<A>, Iterable<A>> {
1515

16+
private static final Cycle INSTANCE = new Cycle();
17+
1618
private Cycle() {
1719
}
1820

@@ -21,8 +23,9 @@ public Iterable<A> apply(Iterable<A> as) {
2123
return () -> new CyclicIterator<>(as.iterator());
2224
}
2325

26+
@SuppressWarnings("unchecked")
2427
public static <A> Cycle<A> cycle() {
25-
return new Cycle<>();
28+
return INSTANCE;
2629
}
2730

2831
public static <A> Iterable<A> cycle(Iterable<A> as) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn1/Head.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
public final class Head<A> implements Fn1<Iterable<A>, Optional<A>> {
1515

16+
private static final Head INSTANCE = new Head();
17+
1618
private Head() {
1719
}
1820

@@ -24,8 +26,9 @@ public Optional<A> apply(Iterable<A> as) {
2426
: Optional.empty();
2527
}
2628

29+
@SuppressWarnings("unchecked")
2730
public static <A> Head<A> head() {
28-
return new Head<>();
31+
return INSTANCE;
2932
}
3033

3134
public static <A> Optional<A> head(Iterable<A> as) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn1/Id.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
public final class Id<A> implements Fn1<A, A> {
1111

12-
private static final Id ID = new Id();
12+
private static final Id INSTANCE = new Id();
1313

1414
private Id() {
1515
}
@@ -21,6 +21,6 @@ public A apply(A a) {
2121

2222
@SuppressWarnings("unchecked")
2323
public static <A> Id<A> id() {
24-
return (Id<A>) ID;
24+
return INSTANCE;
2525
}
2626
}

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn1/Last.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public Optional<A> apply(Iterable<A> as) {
2828

2929
@SuppressWarnings("unchecked")
3030
public static <A> Last<A> last() {
31-
return (Last<A>) INSTANCE;
31+
return INSTANCE;
3232
}
3333

3434
public static <A> Optional<A> last(Iterable<A> as) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn1/Repeat.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*/
1111
public final class Repeat<A> implements Fn1<A, Iterable<A>> {
1212

13+
private static final Repeat INSTANCE = new Repeat();
14+
1315
private Repeat() {
1416
}
1517

@@ -18,8 +20,9 @@ public Iterable<A> apply(A a) {
1820
return () -> new RepetitiousIterator<>(a);
1921
}
2022

23+
@SuppressWarnings("unchecked")
2124
public static <A> Repeat<A> repeat() {
22-
return new Repeat<>();
25+
return INSTANCE;
2326
}
2427

2528
public static <A> Iterable<A> repeat(A a) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn1/Reverse.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
public final class Reverse<A> implements Fn1<Iterable<A>, Iterable<A>> {
1313

14+
private static final Reverse INSTANCE = new Reverse();
15+
1416
private Reverse() {
1517
}
1618

@@ -19,8 +21,9 @@ public Iterable<A> apply(Iterable<A> as) {
1921
return () -> new ReversingIterator<>(as.iterator());
2022
}
2123

24+
@SuppressWarnings("unchecked")
2225
public static <A> Reverse<A> reverse() {
23-
return new Reverse<>();
26+
return INSTANCE;
2427
}
2528

2629
public static <A> Iterable<A> reverse(Iterable<A> as) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn1/Tail.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
public final class Tail<A> implements Fn1<Iterable<A>, Iterable<A>> {
1414

15+
private static final Tail INSTANCE = new Tail();
16+
1517
private Tail() {
1618
}
1719

@@ -25,8 +27,9 @@ public Iterable<A> apply(Iterable<A> as) {
2527
};
2628
}
2729

30+
@SuppressWarnings("unchecked")
2831
public static <A> Tail<A> tail() {
29-
return new Tail<>();
32+
return INSTANCE;
3033
}
3134

3235
public static <A> Iterable<A> tail(Iterable<A> as) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/All.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
public final class All<A> implements BiPredicate<Function<? super A, Boolean>, Iterable<A>> {
1717

18+
private static final All INSTANCE = new All();
19+
1820
private All() {
1921
}
2022

@@ -27,8 +29,9 @@ public Boolean apply(Function<? super A, Boolean> predicate, Iterable<A> as) {
2729
return true;
2830
}
2931

32+
@SuppressWarnings("unchecked")
3033
public static <A> All<A> all() {
31-
return new All<>();
34+
return INSTANCE;
3235
}
3336

3437
public static <A> Fn1<Iterable<A>, Boolean> all(Function<? super A, Boolean> predicate) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/Any.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.jnape.palatable.lambda.functions.builtin.fn2;
22

33
import com.jnape.palatable.lambda.functions.Fn1;
4-
import com.jnape.palatable.lambda.functions.Fn2;
54
import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
65

76
import java.util.function.Function;
@@ -16,6 +15,8 @@
1615
*/
1716
public final class Any<A> implements BiPredicate<Function<? super A, Boolean>, Iterable<A>> {
1817

18+
private static final Any INSTANCE = new Any();
19+
1920
private Any() {
2021
}
2122

@@ -28,8 +29,9 @@ public Boolean apply(Function<? super A, Boolean> predicate, Iterable<A> as) {
2829
return false;
2930
}
3031

32+
@SuppressWarnings("unchecked")
3133
public static <A> Any<A> any() {
32-
return new Any<>();
34+
return INSTANCE;
3335
}
3436

3537
public static <A> Fn1<Iterable<A>, Boolean> any(Function<? super A, Boolean> predicate) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/CartesianProduct.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
public final class CartesianProduct<A, B> implements Fn2<Iterable<A>, Iterable<B>, Iterable<Tuple2<A, B>>> {
2323

24+
private static final CartesianProduct INSTANCE = new CartesianProduct();
25+
2426
private CartesianProduct() {
2527
}
2628

@@ -29,8 +31,9 @@ public Iterable<Tuple2<A, B>> apply(Iterable<A> as, Iterable<B> bs) {
2931
return () -> new CombinatorialIterator<>(as.iterator(), bs.iterator());
3032
}
3133

34+
@SuppressWarnings("unchecked")
3235
public static <A, B> CartesianProduct<A, B> cartesianProduct() {
33-
return new CartesianProduct<>();
36+
return INSTANCE;
3437
}
3538

3639
public static <A, B> Fn1<Iterable<B>, Iterable<Tuple2<A, B>>> cartesianProduct(Iterable<A> as) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/Drop.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
public final class Drop<A> implements Fn2<Integer, Iterable<A>, Iterable<A>> {
1717

18+
private static final Drop INSTANCE = new Drop();
19+
1820
private Drop() {
1921
}
2022

@@ -23,8 +25,9 @@ public Iterable<A> apply(Integer n, Iterable<A> as) {
2325
return () -> new DroppingIterator<>(n, as.iterator());
2426
}
2527

28+
@SuppressWarnings("unchecked")
2629
public static <A> Drop<A> drop() {
27-
return new Drop<>();
30+
return INSTANCE;
2831
}
2932

3033
public static <A> Fn1<Iterable<A>, Iterable<A>> drop(int n) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/DropWhile.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
public final class DropWhile<A> implements Fn2<Function<? super A, Boolean>, Iterable<A>, Iterable<A>> {
2020

21+
private static final DropWhile INSTANCE = new DropWhile();
22+
2123
private DropWhile() {
2224
}
2325

@@ -26,8 +28,9 @@ public Iterable<A> apply(Function<? super A, Boolean> predicate, Iterable<A> as)
2628
return () -> new PredicatedDroppingIterator<>(predicate, as.iterator());
2729
}
2830

31+
@SuppressWarnings("unchecked")
2932
public static <A> DropWhile<A> dropWhile() {
30-
return new DropWhile<>();
33+
return INSTANCE;
3134
}
3235

3336
public static <A> Fn1<Iterable<A>, Iterable<A>> dropWhile(Function<? super A, Boolean> predicate) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/Eq.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*/
1111
public final class Eq<A> implements BiPredicate<A, A> {
1212

13+
private static final Eq INSTANCE = new Eq();
14+
1315
private Eq() {
1416
}
1517

@@ -18,8 +20,9 @@ public Boolean apply(A x, A y) {
1820
return x == null ? y == null : x.equals(y);
1921
}
2022

23+
@SuppressWarnings("unchecked")
2124
public static <A> Eq<A> eq() {
22-
return new Eq<>();
25+
return INSTANCE;
2326
}
2427

2528
public static <A> Predicate<A> eq(A x) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/Filter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
public final class Filter<A> implements Fn2<Function<? super A, Boolean>, Iterable<A>, Iterable<A>> {
1818

19+
private static final Filter INSTANCE = new Filter();
20+
1921
private Filter() {
2022
}
2123

@@ -24,8 +26,9 @@ public Iterable<A> apply(Function<? super A, Boolean> predicate, Iterable<A> as)
2426
return () -> new FilteringIterator<>(predicate, as.iterator());
2527
}
2628

29+
@SuppressWarnings("unchecked")
2730
public static <A> Filter<A> filter() {
28-
return new Filter<>();
31+
return INSTANCE;
2932
}
3033

3134
public static <A> Fn1<Iterable<A>, Iterable<A>> filter(Function<? super A, Boolean> predicate) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/Find.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
public final class Find<A> implements Fn2<Function<? super A, Boolean>, Iterable<A>, Optional<A>> {
2222

23+
private static final Find INSTANCE = new Find();
24+
2325
private Find() {
2426
}
2527

@@ -28,8 +30,9 @@ public Optional<A> apply(Function<? super A, Boolean> predicate, Iterable<A> as)
2830
return head(dropWhile(((Predicate<A>) predicate::apply).negate(), as));
2931
}
3032

33+
@SuppressWarnings("unchecked")
3134
public static <A> Find<A> find() {
32-
return new Find<>();
35+
return INSTANCE;
3336
}
3437

3538
public static <A> Fn1<Iterable<A>, Optional<A>> find(Function<? super A, Boolean> predicate) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/InGroupsOf.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
public final class InGroupsOf<A> implements Fn2<Integer, Iterable<A>, Iterable<Iterable<A>>> {
1616

17+
private static final InGroupsOf INSTANCE = new InGroupsOf();
18+
1719
private InGroupsOf() {
1820
}
1921

@@ -22,8 +24,9 @@ public Iterable<Iterable<A>> apply(Integer k, Iterable<A> as) {
2224
return () -> new GroupingIterator<>(k, as.iterator());
2325
}
2426

27+
@SuppressWarnings("unchecked")
2528
public static <A> InGroupsOf<A> inGroupsOf() {
26-
return new InGroupsOf<>();
29+
return INSTANCE;
2730
}
2831

2932
public static <A> Fn1<Iterable<A>, Iterable<Iterable<A>>> inGroupsOf(Integer k) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/Iterate.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
public final class Iterate<A> implements Fn2<Function<? super A, ? extends A>, A, Iterable<A>> {
2020

21+
private static final Iterate INSTANCE = new Iterate();
22+
2123
private Iterate() {
2224
}
2325

@@ -26,8 +28,9 @@ public Iterable<A> apply(Function<? super A, ? extends A> fn, A seed) {
2628
return unfoldr(a -> Optional.of(tuple(a, fn.apply(a))), seed);
2729
}
2830

31+
@SuppressWarnings("unchecked")
2932
public static <A> Iterate<A> iterate() {
30-
return new Iterate<>();
33+
return INSTANCE;
3134
}
3235

3336
public static <A> Fn1<A, Iterable<A>> iterate(Function<? super A, ? extends A> fn) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/Map.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
public final class Map<A, B> implements Fn2<Function<? super A, ? extends B>, Iterable<A>, Iterable<B>> {
1717

18+
private static final Map INSTANCE = new Map();
19+
1820
private Map() {
1921
}
2022

@@ -23,8 +25,9 @@ public Iterable<B> apply(Function<? super A, ? extends B> fn, Iterable<A> as) {
2325
return () -> new MappingIterator<>(fn, as.iterator());
2426
}
2527

28+
@SuppressWarnings("unchecked")
2629
public static <A, B> Map<A, B> map() {
27-
return new Map<>();
30+
return INSTANCE;
2831
}
2932

3033
public static <A, B> Fn1<Iterable<A>, Iterable<B>> map(Function<? super A, ? extends B> fn) {

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/Partial2.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
public final class Partial2<A, B, C> implements Fn2<BiFunction<A, B, C>, A, Fn1<B, C>> {
1818

19+
private static final Partial2 INSTANCE = new Partial2();
20+
1921
private Partial2() {
2022
}
2123

@@ -24,8 +26,9 @@ public Fn1<B, C> apply(BiFunction<A, B, C> fn, A a) {
2426
return b -> fn.apply(a, b);
2527
}
2628

29+
@SuppressWarnings("unchecked")
2730
public static <A, B, C> Partial2<BiFunction<A, B, C>, A, Fn1<B, C>> partial2() {
28-
return new Partial2<>();
31+
return INSTANCE;
2932
}
3033

3134
public static <A, B, C> Fn1<A, Fn1<B, C>> partial2(BiFunction<A, B, C> fn) {

0 commit comments

Comments
 (0)