Skip to content

Commit d3e99ce

Browse files
committed
Resolved overloaded List methods. Marked numerous list methods as deprecated
1 parent aec491c commit d3e99ce

File tree

14 files changed

+105
-54
lines changed

14 files changed

+105
-54
lines changed

core/src/main/java/fj/data/Array.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
package fj.data;
22

3+
import fj.Equal;
34
import fj.F;
45
import fj.F0;
56
import fj.F2;
7+
import fj.Hash;
68
import fj.P;
7-
import fj.P1;
89
import fj.P2;
9-
import fj.Equal;
1010
import fj.Show;
11-
import fj.Hash;
1211
import fj.Unit;
1312
import fj.function.Effect1;
14-
import static fj.Function.*;
13+
14+
import java.util.AbstractCollection;
15+
import java.util.Collection;
16+
import java.util.Iterator;
17+
import java.util.NoSuchElementException;
18+
19+
import static fj.Function.constant;
20+
import static fj.Function.curry;
21+
import static fj.Function.identity;
1522
import static fj.P.p;
1623
import static fj.P.p2;
1724
import static fj.Unit.unit;
18-
import static fj.data.List.iterableList;
25+
import static fj.data.List.fromIterable;
1926
import static fj.data.Option.none;
2027
import static fj.data.Option.some;
2128
import static java.lang.Math.min;
2229
import static java.lang.System.arraycopy;
2330

24-
import java.util.AbstractCollection;
25-
import java.util.Collection;
26-
import java.util.Iterator;
27-
import java.util.NoSuchElementException;
28-
2931
/**
3032
* Provides an interface to arrays.
3133
*
@@ -800,7 +802,7 @@ public int size() {
800802
* @return An array from the given iterable.
801803
*/
802804
public static <A> Array<A> iterableArray(final Iterable<A> i) {
803-
return iterableList(i).toArray();
805+
return fromIterable(i).toArray();
804806
}
805807

806808
/**

core/src/main/java/fj/data/Conversions.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static <A> F<List<A>, Stream<A>> List_Stream() {
5454
* @return A function that converts lists to options.
5555
*/
5656
public static <A> F<List<A>, Option<A>> List_Option() {
57-
return as -> as.toOption();
57+
return as -> as.headOption();
5858
}
5959

6060
/**
@@ -471,7 +471,7 @@ public static <A> F<F<Unit, A>, SafeIO<A>> F_SafeIO() {
471471
/**
472472
* A function that converts strings to options.
473473
*/
474-
public static final F<String, Option<Character>> String_Option = s -> fromString(s).toOption();
474+
public static final F<String, Option<Character>> String_Option = s -> fromString(s).headOption();
475475

476476
/**
477477
* A function that converts string to eithers.
@@ -519,7 +519,7 @@ public static <A> F<P1<A>, F<String, Either<A, Character>>> String_Either() {
519519
/**
520520
* A function that converts string buffers to options.
521521
*/
522-
public static final F<StringBuffer, Option<Character>> StringBuffer_Option = s -> fromString(s.toString()).toOption();
522+
public static final F<StringBuffer, Option<Character>> StringBuffer_Option = s -> fromString(s.toString()).headOption();
523523

524524
/**
525525
* A function that converts string buffers to eithers.
@@ -562,7 +562,7 @@ public static <A> F<P1<A>, F<StringBuffer, Either<A, Character>>> StringBuffer_E
562562
/**
563563
* A function that converts string builders to options.
564564
*/
565-
public static final F<StringBuilder, Option<Character>> StringBuilder_Option = s -> fromString(s.toString()).toOption();
565+
public static final F<StringBuilder, Option<Character>> StringBuilder_Option = s -> fromString(s.toString()).headOption();
566566

567567
/**
568568
* A function that converts string builders to eithers.

core/src/main/java/fj/data/HashMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public Stream<P2<K, V>> toStream() {
309309
}
310310

311311
public Option<P2<K, V>> toOption() {
312-
return toList().toOption();
312+
return toList().headOption();
313313
}
314314

315315
public Array<P2<K, V>> toArray() {

core/src/main/java/fj/data/Java.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,7 @@ public static <A> F<java.util.List<A>, List<A>> JavaList_List() {
14581458
}
14591459

14601460
public static <A> List<A> JavaList_List(java.util.List<A> list) {
1461-
return List.list(list);
1461+
return List.fromIterable(list);
14621462
}
14631463

14641464
// BEGIN BitSet ->

core/src/main/java/fj/data/List.java

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package fj.data;
22

33
import static fj.Bottom.error;
4-
import fj.F0;
5-
import fj.F2Functions;
4+
65
import fj.Equal;
7-
import fj.F;
8-
import fj.F2;
9-
import fj.Function;
6+
import fj.F2Functions;
107
import fj.Hash;
118
import fj.Monoid;
129
import fj.Ord;
10+
import fj.Ordering;
1311
import fj.P;
1412
import fj.P1;
15-
import fj.P2;
1613
import fj.Show;
1714
import fj.Unit;
15+
import fj.P2;
16+
import fj.F0;
17+
import fj.F;
18+
import fj.F2;
19+
import fj.Function;
1820

1921
import static fj.Function.*;
2022
import static fj.P.p;
@@ -30,7 +32,8 @@
3032
import static fj.function.Booleans.not;
3133
import static fj.Ordering.GT;
3234
import static fj.Ord.intOrd;
33-
import fj.Ordering;
35+
36+
3437
import fj.control.Trampoline;
3538
import fj.control.parallel.Promise;
3639
import fj.control.parallel.Strategy;
@@ -111,13 +114,19 @@ public final boolean isNotEmpty() {
111114

112115
/**
113116
* Performs a reduction on this list using the given arguments.
117+
* @deprecated As of release 4.5, use {@link #reduce}
114118
*
115119
* @param nil The value to return if this list is empty.
116120
* @param cons The function to apply to the head and tail of this list if it is not empty.
117121
* @return A reduction on this list.
118122
*/
123+
@Deprecated
119124
public final <B> B list(final B nil, final F<A, F<List<A>, B>> cons) {
120-
return isEmpty() ? nil : cons.f(head()).f(tail());
125+
return reduce(Function.uncurryF2(cons), nil);
126+
}
127+
128+
public final <B> B reduce(final F2<A, List<A>, B> cons, final B nil) {
129+
return isEmpty() ? nil : cons.f(head(), tail());
121130
}
122131

123132
/**
@@ -143,10 +152,21 @@ public final List<A> orTail(final F0<List<A>> as) {
143152
/**
144153
* Returns an option projection of this list; <code>None</code> if empty, or the first element in
145154
* <code>Some</code>. Equivalent to {@link #headOption()}.
146-
*
155+
* @deprecated As of release 4.5, use {@link #headOption()}
147156
* @return An option projection of this list.
148157
*/
158+
@Deprecated
149159
public final Option<A> toOption() {
160+
return headOption();
161+
162+
}
163+
164+
/**
165+
* Returns the head of the list, if any. Equivalent to {@link #toOption()} .
166+
*
167+
* @return The optional head of the list.
168+
*/
169+
public Option<A> headOption() {
150170
return isEmpty() ? Option.<A>none() : some(head());
151171
}
152172

@@ -1134,14 +1154,6 @@ public static <A> F<List<A>, A> head_() {
11341154
return list -> list.head();
11351155
}
11361156

1137-
/**
1138-
* Returns the head of the list, if any. Equivalent to {@link #toOption()} .
1139-
*
1140-
* @return The optional head of the list.
1141-
*/
1142-
public Option<A> headOption() {
1143-
return toOption();
1144-
}
11451157

11461158
/**
11471159
* Reutrns the tail of the list, if any.
@@ -1529,13 +1541,24 @@ private void tail(final List<A> tail) {
15291541
return Array.array(as).toList();
15301542
}
15311543

1532-
1544+
/**
1545+
* @deprecated As of release 4.5, use {@link #fromIterable(Iterable)}
1546+
*/
1547+
@Deprecated
15331548
public static <A> List<A> list(final Iterable<A> i) {
1534-
return iterableList(i);
1549+
return fromIterable(i);
15351550
}
15361551

1552+
/**
1553+
* @deprecated As of release 4.5, use {@link #fromIterator(Iterator)}
1554+
*/
1555+
@Deprecated
15371556
public static <A> List<A> list(final Iterator<A> it) {
1538-
return iterableList(() -> it);
1557+
return fromIterable(() -> it);
1558+
}
1559+
1560+
public static <A> List<A> fromIterator(final Iterator<A> it) {
1561+
return fromIterable(() -> it);
15391562
}
15401563

15411564
/**
@@ -1840,20 +1863,24 @@ public static <A> F<Integer, F<List<A>, List<A>>> take() {
18401863

18411864
/**
18421865
* Takes the given iterable to a list.
1866+
* @deprecated From release 4.5 use {@link #fromIterable(Iterable)}
18431867
*
18441868
* @param i The iterable to take to a list.
18451869
* @return A list from the given iterable.
18461870
*/
1871+
@Deprecated
18471872
public static <A> List<A> iterableList(final Iterable<A> i) {
1848-
final Buffer<A> bs = empty();
1873+
return fromIterable(i);
1874+
}
18491875

1850-
for (final A a : i)
1876+
public static <A> List<A> fromIterable(final Iterable<A> i) {
1877+
final Buffer<A> bs = empty();
1878+
for (final A a : i) {
18511879
bs.snoc(a);
1852-
1880+
}
18531881
return bs.toList();
18541882
}
18551883

1856-
18571884
/**
18581885
* A mutable, singly linked list. This structure should be used <em>very</em> sparingly, in favour
18591886
* of the {@link List immutable singly linked list structure}.
@@ -2159,4 +2186,8 @@ public static <A> Prism<List<A>, P2<A, List<A>>> cons() {
21592186

21602187
}
21612188

2189+
public static final class Unsafe {
2190+
2191+
}
2192+
21622193
}

core/src/main/java/fj/data/TreeMap.java

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

3-
import fj.*;
3+
import fj.Equal;
4+
import fj.F;
5+
import fj.F1Functions;
6+
import fj.Hash;
7+
import fj.Ord;
8+
import fj.P;
9+
import fj.P2;
10+
import fj.P3;
11+
import fj.Show;
412

513
import java.util.Comparator;
614
import java.util.Iterator;
@@ -10,6 +18,7 @@
1018
import static fj.Function.flip;
1119
import static fj.P.p;
1220
import static fj.data.IterableW.join;
21+
import static fj.data.List.fromIterable;
1322
import static fj.data.List.iterableList;
1423

1524
/**
@@ -134,7 +143,7 @@ public boolean isEmpty() {
134143
* @return All values in this tree map.
135144
*/
136145
public List<V> values() {
137-
return iterableList(join(tree.toList().map(compose(IterableW.<V, Option<V>>wrap(), P2.<K, Option<V>>__2()))));
146+
return fromIterable(join(tree.toList().map(compose(IterableW.<V, Option<V>>wrap(), P2.<K, Option<V>>__2()))));
138147
}
139148

140149
/**

core/src/main/java/fj/data/Validation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ public static <A, E> Validation<List<E>, List<A>> sequenceNonCumulative(List<Val
828828
public <C> List<Validation<E, C>> traverseList(F<T, List<C>> f){
829829
return isSuccess() ?
830830
f.f(success()).map(Validation::success) :
831-
list(fail(e.left().value()));
831+
List.fromIterable(fail(e.left().value()));
832832
}
833833

834834
public <C> Stream<Validation<E, C>> traverseStream(F<T, Stream<C>> f){

core/src/test/java/fj/data/ListTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
import java.util.Arrays;
88

9+
import static org.hamcrest.CoreMatchers.equalTo;
910
import static org.junit.Assert.assertEquals;
1011
import static org.junit.Assert.assertFalse;
12+
import static org.junit.Assert.assertThat;
1113
import static org.junit.Assert.assertTrue;
1214

1315
/**
@@ -37,7 +39,7 @@ public void objectMethods() {
3739
@Test
3840
public void integration() {
3941
java.util.List<Integer> ul = Arrays.asList(1, 2, 3);
40-
List<Integer> dl = List.list(ul);
42+
List<Integer> dl = List.fromIterable(ul);
4143
assertTrue(ul.equals(dl.toJavaList()));
4244

4345
}
@@ -72,4 +74,11 @@ public void intersperseOverflow() {
7274
String s = list.toString();
7375
}
7476

77+
@Test
78+
public void listReduce() {
79+
String list = List.range(1, 11).reduce((a, la) -> List.cons(a, la).toString(), "");
80+
String expected = List.range(1, 11).toString();
81+
assertThat(expected, equalTo(list));
82+
}
83+
7584
}

core/src/test/java/fj/data/TreeMapTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void toMutableMap() {
8888
List<List<Integer>> l = List.range(1, max + 1).map(n -> List.single(n));
8989
TreeMap<List<Integer>, String> m2 = TreeMap.treeMap(Ord.listOrd(Ord.intOrd), l.zip(l.map(i -> i.toString())));
9090
Map<List<Integer>, String> mm = m2.toMutableMap();
91-
assertEquals(m2.keys(), List.iterableList(mm.keySet()));
91+
assertEquals(m2.keys(), List.fromIterable(mm.keySet()));
9292
}
9393

9494

java-core/src/main/java/fj/java/util/ListUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
public class ListUtil {
1212

1313
public static <A, B> List<B> map(List<A> list, F<A, B> f) {
14-
return fj.data.List.list(list).map(f).toJavaList();
14+
return fj.data.List.fromIterable(list).map(f).toJavaList();
1515
}
1616

1717
public static<A> List<A> filter(List<A> list, F<A, Boolean> f) {
18-
return fj.data.List.list(list).filter(f).toJavaList();
18+
return fj.data.List.fromIterable(list).filter(f).toJavaList();
1919
}
2020

2121
public static <A, B> B fold(List<A> list, F2<B, A, B> f, B b) {
22-
return fj.data.List.list(list).foldLeft(f, b);
22+
return fj.data.List.fromIterable(list).foldLeft(f, b);
2323
}
2424

2525
public static <A, B> List<B> flatMap(List<A> list, F<A, List<B>> f) {
26-
return fj.data.List.list(list).bind(a -> fj.data.List.list(f.f(a))).toJavaList();
26+
return fj.data.List.fromIterable(list).bind(a -> fj.data.List.fromIterable(f.f(a))).toJavaList();
2727
}
2828

2929
public static <A, B> List<B> bind(List<A> list, F<A, List<B>> f) {

0 commit comments

Comments
 (0)