Skip to content

Commit 2477906

Browse files
Fixes #412. Add fj.data.Seq.sequence*, fj.data.Seq.traverse* functions.
1 parent 0425c2f commit 2477906

File tree

3 files changed

+638
-25
lines changed

3 files changed

+638
-25
lines changed

core/src/main/java/fj/Ord.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
package fj;
22

3-
import fj.data.Array;
4-
import fj.data.Either;
5-
import fj.data.List;
6-
import fj.data.Natural;
7-
import fj.data.NonEmptyList;
8-
import fj.data.Option;
9-
import fj.data.Set;
10-
import fj.data.Stream;
11-
import fj.data.Validation;
12-
13-
import java.math.BigDecimal;
14-
import java.math.BigInteger;
3+
import fj.data.*;
4+
5+
import java.math.*;
156
import java.util.Comparator;
167

17-
import static fj.Function.apply;
18-
import static fj.Function.compose;
19-
import static fj.Function.curry;
20-
import static fj.Semigroup.semigroup;
8+
import static fj.Function.*;
219
import static fj.Semigroup.semigroupDef;
2210

2311
/**
@@ -541,6 +529,37 @@ public static <A> Ord<List<A>> listOrd(final Ord<A> oa) {
541529
});
542530
}
543531

532+
/**
533+
* Return a seq ord using the given value ord.
534+
*
535+
* @param ord the given value ord
536+
* @param <A> the type of the seq value
537+
* @return the seq ord
538+
*/
539+
public static <A> Ord<Seq<A>> seqOrd(final Ord<A> ord) {
540+
return ordDef((l1, l2) -> {
541+
Seq<A> x1 = l1;
542+
Seq<A> x2 = l2;
543+
544+
while (x1.isNotEmpty() && x2.isNotEmpty()) {
545+
final Ordering o = ord.compare(x1.head(), x2.head());
546+
if (o == Ordering.LT || o == Ordering.GT) {
547+
return o;
548+
}
549+
x1 = x1.tail();
550+
x2 = x2.tail();
551+
}
552+
553+
if (x1.isEmpty() && x2.isEmpty()) {
554+
return Ordering.EQ;
555+
} else if (x1.isEmpty()) {
556+
return Ordering.LT;
557+
} else {
558+
return Ordering.GT;
559+
}
560+
});
561+
}
562+
544563
/**
545564
* An order instance for the {@link NonEmptyList} type.
546565
*

0 commit comments

Comments
 (0)