Skip to content

Commit 64fdac7

Browse files
committed
Made Stream consistent with List for constructing a Stream from an array, iterator and iterable
1 parent 6f66aa4 commit 64fdac7

File tree

10 files changed

+107
-50
lines changed

10 files changed

+107
-50
lines changed

core/src/main/java/fj/F1Functions.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.concurrent.*;
1313

1414
import static fj.data.Option.some;
15+
import static fj.data.Stream.fromIterable;
1516
import static fj.data.Stream.iterableStream;
1617
import static fj.data.Zipper.fromStream;
1718

@@ -558,7 +559,7 @@ static public <A, B> F<P2<A, A>, P2<B, B>> mapBoth(final F<A, B> f) {
558559
*/
559560
static public <A, B> SynchronousQueue<B> mapJ(final F<A, B> f, final SynchronousQueue<A> as) {
560561
final SynchronousQueue<B> bs = new SynchronousQueue<B>();
561-
bs.addAll(iterableStream(as).map(f).toCollection());
562+
bs.addAll(fromIterable(as).map(f).toCollection());
562563
return bs;
563564
}
564565

@@ -570,7 +571,7 @@ static public <A, B> SynchronousQueue<B> mapJ(final F<A, B> f, final Synchronous
570571
* @return A new PriorityBlockingQueue with this function applied to each element.
571572
*/
572573
static public <A, B> PriorityBlockingQueue<B> mapJ(final F<A, B> f, final PriorityBlockingQueue<A> as) {
573-
return new PriorityBlockingQueue<B>(iterableStream(as).map(f).toCollection());
574+
return new PriorityBlockingQueue<B>(fromIterable(as).map(f).toCollection());
574575
}
575576

576577
/**
@@ -580,7 +581,7 @@ static public <A, B> PriorityBlockingQueue<B> mapJ(final F<A, B> f, final Priori
580581
* @return A new LinkedBlockingQueue with this function applied to each element.
581582
*/
582583
static public <A, B> LinkedBlockingQueue<B> mapJ(final F<A, B> f, final LinkedBlockingQueue<A> as) {
583-
return new LinkedBlockingQueue<B>(iterableStream(as).map(f).toCollection());
584+
return new LinkedBlockingQueue<B>(fromIterable(as).map(f).toCollection());
584585
}
585586

586587
/**
@@ -590,7 +591,7 @@ static public <A, B> LinkedBlockingQueue<B> mapJ(final F<A, B> f, final LinkedBl
590591
* @return A new CopyOnWriteArraySet with this function applied to each element.
591592
*/
592593
static public <A, B> CopyOnWriteArraySet<B> mapJ(final F<A, B> f, final CopyOnWriteArraySet<A> as) {
593-
return new CopyOnWriteArraySet<B>(iterableStream(as).map(f).toCollection());
594+
return new CopyOnWriteArraySet<B>(fromIterable(as).map(f).toCollection());
594595
}
595596

596597
/**
@@ -600,7 +601,7 @@ static public <A, B> CopyOnWriteArraySet<B> mapJ(final F<A, B> f, final CopyOnWr
600601
* @return A new CopyOnWriteArrayList with this function applied to each element.
601602
*/
602603
static public <A, B> CopyOnWriteArrayList<B> mapJ(final F<A, B> f, final CopyOnWriteArrayList<A> as) {
603-
return new CopyOnWriteArrayList<B>(iterableStream(as).map(f).toCollection());
604+
return new CopyOnWriteArrayList<B>(fromIterable(as).map(f).toCollection());
604605
}
605606

606607
/**
@@ -610,7 +611,7 @@ static public <A, B> CopyOnWriteArrayList<B> mapJ(final F<A, B> f, final CopyOnW
610611
* @return A new ConcurrentLinkedQueue with this function applied to each element.
611612
*/
612613
static public <A, B> ConcurrentLinkedQueue<B> mapJ(final F<A, B> f, final ConcurrentLinkedQueue<A> as) {
613-
return new ConcurrentLinkedQueue<B>(iterableStream(as).map(f).toCollection());
614+
return new ConcurrentLinkedQueue<B>(fromIterable(as).map(f).toCollection());
614615
}
615616

616617
/**
@@ -621,7 +622,7 @@ static public <A, B> ConcurrentLinkedQueue<B> mapJ(final F<A, B> f, final Concur
621622
*/
622623
static public <A, B> ArrayBlockingQueue<B> mapJ(final F<A, B> f, final ArrayBlockingQueue<A> as) {
623624
final ArrayBlockingQueue<B> bs = new ArrayBlockingQueue<B>(as.size());
624-
bs.addAll(iterableStream(as).map(f).toCollection());
625+
bs.addAll(fromIterable(as).map(f).toCollection());
625626
return bs;
626627
}
627628

@@ -633,7 +634,7 @@ static public <A, B> ArrayBlockingQueue<B> mapJ(final F<A, B> f, final ArrayBloc
633634
* @return A new TreeSet with this function applied to each element.
634635
*/
635636
static public <A, B> TreeSet<B> mapJ(final F<A, B> f, final TreeSet<A> as) {
636-
return new TreeSet<B>(iterableStream(as).map(f).toCollection());
637+
return new TreeSet<B>(fromIterable(as).map(f).toCollection());
637638
}
638639

639640
/**
@@ -643,7 +644,7 @@ static public <A, B> TreeSet<B> mapJ(final F<A, B> f, final TreeSet<A> as) {
643644
* @return A new PriorityQueue with this function applied to each element.
644645
*/
645646
static public <A, B> PriorityQueue<B> mapJ(final F<A, B> f, final PriorityQueue<A> as) {
646-
return new PriorityQueue<B>(iterableStream(as).map(f).toCollection());
647+
return new PriorityQueue<B>(fromIterable(as).map(f).toCollection());
647648
}
648649

649650
/**
@@ -653,7 +654,7 @@ static public <A, B> PriorityQueue<B> mapJ(final F<A, B> f, final PriorityQueue<
653654
* @return A new LinkedList with this function applied to each element.
654655
*/
655656
static public <A, B> LinkedList<B> mapJ(final F<A, B> f, final LinkedList<A> as) {
656-
return new LinkedList<B>(iterableStream(as).map(f).toCollection());
657+
return new LinkedList<B>(fromIterable(as).map(f).toCollection());
657658
}
658659

659660
/**
@@ -663,7 +664,7 @@ static public <A, B> LinkedList<B> mapJ(final F<A, B> f, final LinkedList<A> as)
663664
* @return A new ArrayList with this function applied to each element.
664665
*/
665666
static public <A, B> ArrayList<B> mapJ(final F<A, B> f, final ArrayList<A> as) {
666-
return new ArrayList<B>(iterableStream(as).map(f).toCollection());
667+
return new ArrayList<B>(fromIterable(as).map(f).toCollection());
667668
}
668669

669670
static public <A, B, C> F<A, C> map(F<A, B> target, F<B, C> f) {

core/src/main/java/fj/Monoid.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import fj.data.Stream;
1111

1212
import static fj.Semigroup.multiply1p;
13+
import static fj.data.Stream.fromIterable;
1314
import static fj.data.Stream.iterableStream;
1415

1516
import java.math.BigInteger;
@@ -183,7 +184,7 @@ public F<Stream<A>, A> sumLeftS() {
183184
* @return The sum of the given values and the interspersed value.
184185
*/
185186
public A join(final Iterable<A> as, final A a) {
186-
final Stream<A> s = iterableStream(as);
187+
final Stream<A> s = fromIterable(as);
187188
return s.isEmpty() ?
188189
zero :
189190
s.foldLeft1(Function.compose(sum, flip(sum).f(a)));

core/src/main/java/fj/Show.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public static <A> Show<Set<A>> setShow(final Show<A> sa) {
324324
*/
325325
public static <K, V> Show<TreeMap<K, V>> treeMapShow(final Show<K> sk, final Show<V> sv) {
326326
return show(tm -> {
327-
Stream<P2<K, V>> stream = Stream.iteratorStream(tm.iterator());
327+
Stream<P2<K, V>> stream = Stream.fromIterator(tm.iterator());
328328
return streamShow(Show.p2MapShow(sk, sv), "TreeMap(", ",", ")").show(stream);
329329
});
330330
}

core/src/main/java/fj/control/parallel/ParModule.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import fj.function.Effect1;
1919

2020
import static fj.data.Option.some;
21+
import static fj.data.Stream.fromIterable;
2122
import static fj.data.Stream.iterableStream;
2223

2324
/**
@@ -292,7 +293,7 @@ public <A, B> F<F<A, B>, F<Stream<A>, Promise<Stream<B>>>> parMapStream() {
292293
* @return A Promise of a new Iterable with the given function applied to each element.
293294
*/
294295
public <A, B> Promise<Iterable<B>> parMap(final Iterable<A> as, final F<A, B> f) {
295-
return parMap(iterableStream(as), f)
296+
return parMap(fromIterable(as), f)
296297
.fmap(Function.<Stream<B>, Iterable<B>>vary(Function.<Stream<B>>identity()));
297298
}
298299

@@ -464,7 +465,7 @@ public <A, B, C> Promise<Array<C>> parZipWith(final Array<A> as, final Array<B>
464465
* @return A Promise of a new iterable with the results of applying the given function across the two iterables, stepwise.
465466
*/
466467
public <A, B, C> Promise<Iterable<C>> parZipWith(final Iterable<A> as, final Iterable<B> bs, final F<A, F<B, C>> f) {
467-
return parZipWith(iterableStream(as), iterableStream(bs), f).fmap(
468+
return parZipWith(fromIterable(as), fromIterable(bs), f).fmap(
468469
Function.<Stream<C>, Iterable<C>>vary(Function.<Iterable<C>>identity()));
469470
}
470471

@@ -514,8 +515,8 @@ public <A, B> Promise<B> parFoldMap(final Stream<A> as, final F<A, B> map, final
514515
*/
515516
public <A, B> Promise<B> parFoldMap(final Iterable<A> as, final F<A, B> map, final Monoid<B> reduce,
516517
final F<Iterable<A>, P2<Iterable<A>, Iterable<A>>> chunking) {
517-
return parFoldMap(iterableStream(as), map, reduce, (Stream<A> stream) -> {
518-
final F<Iterable<A>, Stream<A>> is = iterable -> iterableStream(iterable);
518+
return parFoldMap(fromIterable(as), map, reduce, (Stream<A> stream) -> {
519+
final F<Iterable<A>, Stream<A>> is = iterable -> fromIterable(iterable);
519520
return chunking.f(stream).map1(is).map2(is);
520521
});
521522
}
@@ -530,7 +531,7 @@ public <A, B> Promise<B> parFoldMap(final Iterable<A> as, final F<A, B> map, fin
530531
* @return A promise of a result of mapping and folding in parallel.
531532
*/
532533
public <A, B> Promise<B> parFoldMap(final Iterable<A> as, final F<A, B> map, final Monoid<B> reduce) {
533-
return parFoldMap(iterableStream(as), map, reduce);
534+
return parFoldMap(fromIterable(as), map, reduce);
534535
}
535536

536537

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

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

33
import static fj.data.Option.some;
4+
import static fj.data.Stream.fromIterable;
45
import static fj.data.Stream.iterableStream;
56
import fj.Equal;
67
import fj.F;
@@ -85,7 +86,7 @@ public static <A, B> F<F<A, B>, F<A, IterableW<B>>> arrow() {
8586
* @return an iterable result of binding the given function over the wrapped Iterable.
8687
*/
8788
public <B, T extends Iterable<B>> IterableW<B> bind(final F<A, T> f) {
88-
return wrap(iterableStream(this).bind(a -> iterableStream(f.f(a))));
89+
return wrap(fromIterable(this).bind(a -> fromIterable(f.f(a))));
8990
}
9091

9192
/**
@@ -128,12 +129,12 @@ public static <A, B, C> F<Iterable<A>, F<Iterable<B>, IterableW<C>>> liftM2(fina
128129
* @return A iterable of iterables containing the results of the bind operations across all given iterables.
129130
*/
130131
public static <A, T extends Iterable<A>> IterableW<IterableW<A>> sequence(final Iterable<T> as) {
131-
final Stream<T> ts = iterableStream(as);
132+
final Stream<T> ts = fromIterable(as);
132133
return ts.isEmpty() ?
133134
iterable(wrap(Option.<A>none())) :
134135
wrap(ts.head()).bind(a ->
135136
sequence(ts.tail().map(IterableW.<T, Stream<T>>wrap())._1()).bind(as2 ->
136-
iterable(wrap(Stream.cons(a, () -> iterableStream(as2))))
137+
iterable(wrap(Stream.cons(a, () -> fromIterable(as2))))
137138
)
138139
);
139140
}
@@ -221,7 +222,7 @@ public A foldLeft1(final F2<A, A, A> f) {
221222
* @return The final result after the left-fold reduction.
222223
*/
223224
public A foldLeft1(final F<A, F<A, A>> f) {
224-
return iterableStream(this).foldLeft1(f);
225+
return fromIterable(this).foldLeft1(f);
225226
}
226227

227228
/**
@@ -254,7 +255,7 @@ public Iterator<A> iterator() {
254255
* @return A new iterable with the results of applying the functions to this iterable.
255256
*/
256257
public <B> IterableW<B> zapp(final Iterable<F<A, B>> fs) {
257-
return wrap(iterableStream(this).zapp(iterableStream(fs)));
258+
return wrap(fromIterable(this).zapp(fromIterable(fs)));
258259
}
259260

260261
/**
@@ -268,7 +269,7 @@ public <B> IterableW<B> zapp(final Iterable<F<A, B>> fs) {
268269
* iterable.
269270
*/
270271
public <B, C> Iterable<C> zipWith(final Iterable<B> bs, final F<A, F<B, C>> f) {
271-
return wrap(iterableStream(this).zipWith(iterableStream(bs), f));
272+
return wrap(fromIterable(this).zipWith(fromIterable(bs), f));
272273
}
273274

274275
/**
@@ -295,7 +296,7 @@ public <B, C> Iterable<C> zipWith(final Iterable<B> bs, final F2<A, B, C> f) {
295296
* iterable.
296297
*/
297298
public <B> Iterable<P2<A, B>> zip(final Iterable<B> bs) {
298-
return wrap(iterableStream(this).zip(iterableStream(bs)));
299+
return wrap(fromIterable(this).zip(fromIterable(bs)));
299300
}
300301

301302
/**
@@ -304,7 +305,7 @@ public <B> Iterable<P2<A, B>> zip(final Iterable<B> bs) {
304305
* @return A new iterable with the same length as this iterable.
305306
*/
306307
public Iterable<P2<A, Integer>> zipIndex() {
307-
return wrap(iterableStream(this).zipIndex());
308+
return wrap(fromIterable(this).zipIndex());
308309
}
309310

310311
/**
@@ -317,29 +318,29 @@ public List<A> toStandardList() {
317318
return new List<A>() {
318319

319320
public int size() {
320-
return iterableStream(IterableW.this).length();
321+
return fromIterable(IterableW.this).length();
321322
}
322323

323324
public boolean isEmpty() {
324-
return iterableStream(IterableW.this).isEmpty();
325+
return fromIterable(IterableW.this).isEmpty();
325326
}
326327

327328
@SuppressWarnings({"unchecked"})
328329
public boolean contains(final Object o) {
329-
return iterableStream(IterableW.this).exists(Equal.<A>anyEqual().eq((A) o));
330+
return fromIterable(IterableW.this).exists(Equal.<A>anyEqual().eq((A) o));
330331
}
331332

332333
public Iterator<A> iterator() {
333334
return IterableW.this.iterator();
334335
}
335336

336337
public Object[] toArray() {
337-
return Array.iterableArray(iterableStream(IterableW.this)).array();
338+
return Array.iterableArray(fromIterable(IterableW.this)).array();
338339
}
339340

340341
@SuppressWarnings({"SuspiciousToArrayCall"})
341342
public <T> T[] toArray(final T[] a) {
342-
return iterableStream(IterableW.this).toCollection().toArray(a);
343+
return fromIterable(IterableW.this).toCollection().toArray(a);
343344
}
344345

345346
public boolean add(final A a) {
@@ -351,7 +352,7 @@ public boolean remove(final Object o) {
351352
}
352353

353354
public boolean containsAll(final Collection<?> c) {
354-
return iterableStream(IterableW.this).toCollection().containsAll(c);
355+
return fromIterable(IterableW.this).toCollection().containsAll(c);
355356
}
356357

357358
public boolean addAll(final Collection<? extends A> c) {
@@ -375,7 +376,7 @@ public void clear() {
375376
}
376377

377378
public A get(final int index) {
378-
return iterableStream(IterableW.this).index(index);
379+
return fromIterable(IterableW.this).index(index);
379380
}
380381

381382
public A set(final int index, final A element) {
@@ -420,7 +421,7 @@ public ListIterator<A> listIterator(final int index) {
420421
}
421422

422423
public List<A> subList(final int fromIndex, final int toIndex) {
423-
return wrap(Stream.iterableStream(IterableW.this).drop(fromIndex).take(toIndex - fromIndex)).toStandardList();
424+
return wrap(Stream.fromIterable(IterableW.this).drop(fromIndex).take(toIndex - fromIndex)).toStandardList();
424425
}
425426

426427
private ListIterator<A> toListIterator(final Option<Zipper<A>> z) {
@@ -476,6 +477,6 @@ public void add(final A a) {
476477
}
477478

478479
public Option<Zipper<A>> toZipper() {
479-
return Zipper.fromStream(iterableStream(this));
480+
return Zipper.fromStream(fromIterable(this));
480481
}
481482
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1542,21 +1542,26 @@ private void tail(final List<A> tail) {
15421542
}
15431543

15441544
/**
1545+
* Constructs a list from the given Iterable.
15451546
* @deprecated As of release 4.5, use {@link #fromIterable(Iterable)}
1546-
*/
1547+
*/
15471548
@Deprecated
15481549
public static <A> List<A> list(final Iterable<A> i) {
15491550
return fromIterable(i);
15501551
}
15511552

15521553
/**
1554+
* Constructs a list from the given Iterator.
15531555
* @deprecated As of release 4.5, use {@link #fromIterator(Iterator)}
15541556
*/
15551557
@Deprecated
15561558
public static <A> List<A> list(final Iterator<A> it) {
15571559
return fromIterable(() -> it);
15581560
}
15591561

1562+
/**
1563+
* Constructs a list from the given Iterator.
1564+
*/
15601565
public static <A> List<A> fromIterator(final Iterator<A> it) {
15611566
return fromIterable(() -> it);
15621567
}
@@ -1873,6 +1878,9 @@ public static <A> List<A> iterableList(final Iterable<A> i) {
18731878
return fromIterable(i);
18741879
}
18751880

1881+
/**
1882+
* Constructs a list from the Iterable.
1883+
*/
18761884
public static <A> List<A> fromIterable(final Iterable<A> i) {
18771885
final Buffer<A> bs = empty();
18781886
for (final A a : i) {

0 commit comments

Comments
 (0)