Skip to content

Commit 87db1bd

Browse files
committed
#177: Implemented toListReverse and toStreamReverse for Set and TreeMap
1 parent c7f1c41 commit 87db1bd

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,19 @@ public final <B> B foldMap(final F<A, B> f, final Monoid<B> m) {
269269
m.sum(m.sum(l().foldMap(f, m), f.f(head())), r().foldMap(f, m));
270270
}
271271

272+
/**
273+
* Folds this Set from the right using the given monoid.
274+
*
275+
* @param f A transformation from this Set's elements, to the monoid.
276+
* @param m The monoid to fold this Set with.
277+
* @return The result of folding the Set from the right with the given monoid.
278+
*/
279+
public final <B> B foldMapRight(final F<A, B> f, final Monoid<B> m) {
280+
return isEmpty() ?
281+
m.zero() :
282+
m.sum(m.sum(r().foldMapRight(f, m), f.f(head())), l().foldMapRight(f, m));
283+
}
284+
272285
/**
273286
* Returns a list representation of this set.
274287
*
@@ -278,6 +291,15 @@ public final List<A> toList() {
278291
return foldMap(List.cons(List.<A>nil()), Monoid.<A>listMonoid());
279292
}
280293

294+
/**
295+
* Returns a list representation of this set in reverse order.
296+
*
297+
* @return a list representation of this set in reverse order.
298+
*/
299+
public final List<A> toListReverse() {
300+
return foldMapRight(List.cons(List.<A>nil()), Monoid.<A>listMonoid());
301+
}
302+
281303
/**
282304
* Returns a stream representation of this set.
283305
*
@@ -293,6 +315,21 @@ public final Stream<A> toStream() {
293315
}
294316
}
295317

318+
/**
319+
* Returns a stream representation of this set in reverse order.
320+
*
321+
* @return a stream representation of this set in reverse order.
322+
*/
323+
public final Stream<A> toStreamReverse() {
324+
if (isEmpty()) {
325+
return Stream.nil();
326+
} else if (r().isEmpty()) {
327+
return Stream.cons(head(), () -> l().toStreamReverse());
328+
} else {
329+
return r().toStreamReverse().append(Stream.cons(head(), () -> l().toStreamReverse()));
330+
}
331+
}
332+
296333
/**
297334
* Binds the given function across this set.
298335
*

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,18 @@ public Stream<P2<K, V>> toStream() {
186186
return tree.toStream().map(p -> p.map2(o -> o.some()));
187187
}
188188

189+
public Stream<P2<K, V>> toStreamReverse() {
190+
return tree.toStreamReverse().map(p -> p.map2(o -> o.some()));
191+
}
192+
189193
public List<P2<K, V>> toList() {
190194
return tree.toList().map(p -> p.map2(o -> o.some()));
191195
}
192196

197+
public List<P2<K, V>> toListReverse() {
198+
return tree.toListReverse().map(p -> p.map2(o -> o.some()));
199+
}
200+
193201
/**
194202
* An immutable projection of the given mutable map.
195203
*

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,30 @@
2121
@CheckParams(maxSize = 10000)
2222
public class SetProperties {
2323

24-
public final static Arbitrary<Set<Integer>> as = Arbitrary.arbSet(Ord.intOrd, Arbitrary.arbInteger, 5);
24+
public final static int maxSize = 20;
25+
public final static Arbitrary<Set<Integer>> as = Arbitrary.arbSet(Ord.intOrd, Arbitrary.arbInteger, maxSize);
26+
public final static Equal<List<Integer>> eq = Equal.listEqual(Equal.intEqual);
2527

2628
Property setToListIsSorted() {
2729
return property(as, s -> prop(s.toList().equals(s.toList().sort(Ord.intOrd))));
2830
}
2931

3032
Property stream() {
3133
return property(as, s -> {
32-
Equal<List<Integer>> eq = Equal.listEqual(Equal.intEqual);
3334
List<Integer> l1 = s.toList();
3435
List<Integer> l2 = s.toStream().toList();
3536
return prop(eq.eq(l1, l2));
3637
});
3738
}
3839

40+
Property listReverse() {
41+
return property(as, s -> {
42+
return prop(eq.eq(s.toList().reverse(), s.toListReverse()));
43+
});
44+
}
45+
46+
Property streamReverse() {
47+
return property(as, s -> prop(eq.eq(s.toStream().toList().reverse(), s.toStreamReverse().toList())));
48+
}
49+
3950
}

0 commit comments

Comments
 (0)