Skip to content

Commit f24d043

Browse files
committed
Added methods to create a Seq. Deprecated overloaded seq method.
1 parent 99f38a1 commit f24d043

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

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

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,66 @@ public static <A> Seq<A> single(final A a) {
6565
return new Seq<A>(Seq.<A>mkTree().single(a));
6666
}
6767

68+
/**
69+
* Constructs a sequence from the given elements.
70+
* @param as The elements to create the sequence from.
71+
* @return A sequence with the given elements.
72+
*/
6873
@SafeVarargs public static <A> Seq<A> seq(final A... as) {
69-
return seq(List.list(as));
74+
return fromList(List.list(as));
7075
}
7176

77+
/**
78+
* Constructs a sequence from the given list.
79+
*
80+
* @deprecated As of release 4.5, use {@link #fromList(List)}
81+
*
82+
* @param list The list to create the sequence from.
83+
* @return A sequence with the given elements in the list.
84+
*/
85+
@Deprecated
7286
public static <A>Seq<A> seq(final List<A> list) {
73-
return list.foldLeft((b, a) -> b.snoc(a), Seq.<A>empty());
87+
return fromList(list);
88+
}
89+
90+
/**
91+
* Constructs a sequence from the given list.
92+
* @param list The list to create the sequence from.
93+
* @return A sequence with the elements of the list.
94+
*/
95+
public static <A>Seq<A> fromList(final List<A> list) {
96+
return fromIterable(list);
97+
}
98+
99+
/**
100+
* Constructs a sequence from the iterable.
101+
* @param i The iterable to create the sequence from.
102+
* @return A sequence with the elements of the iterable.
103+
*/
104+
public static <A>Seq<A> fromIterable(final Iterable<A> i) {
105+
Seq<A> s = Seq.empty();
106+
for (final A a: i) {
107+
s = s.snoc(a);
108+
}
109+
return s;
110+
}
111+
112+
/**
113+
* Constructs a sequence from the iterator.
114+
* @param i The iterator to create the sequence from.
115+
* @return A sequence with the elements of the iterator.
116+
*/
117+
public static <A>Seq<A> fromIterator(final Iterator<A> i) {
118+
return fromIterable(() -> i);
119+
}
120+
121+
/**
122+
* Constructs a sequence from the given list.
123+
* @param list The list to create the sequence from.
124+
* @return A sequence with the elements of the list.
125+
*/
126+
public static <A>Seq<A> fromJavaList(final java.util.List<A> list) {
127+
return fromIterable(list);
74128
}
75129

76130
/**
@@ -145,6 +199,9 @@ public List<A> toList() {
145199
return buf.toList();
146200
}
147201

202+
/**
203+
* Converts the sequence to a java.util.List
204+
*/
148205
public final java.util.List<A> toJavaList() {
149206
return new AbstractList<A>() {
150207
@Override public A get(int i) { return index(i); }

props-core/src/test/java/fj/data/properties/SeqProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public Property foldRight() {
122122

123123
public Property length() {
124124
return property(arbList(arbInteger), list ->
125-
prop(Seq.seq(list).length() == list.length())
125+
prop(Seq.fromList(list).length() == list.length())
126126
);
127127
}
128128

0 commit comments

Comments
 (0)