Skip to content

Commit 6dc9220

Browse files
committed
Adding SortWith for sorting given a Comparator
1 parent 7269102 commit 6dc9220

File tree

5 files changed

+80
-7
lines changed

5 files changed

+80
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
1111
- `CheckedFn1` now overrides all possible methods with covariant return type
1212
- `MapLens#asCopy` has overload taking copy function
1313
- `MapLens#valueAt` has overload taking copy function
14+
- `SortWith` for sorting an `Iterable` given a `Comparator` over its elements
1415

1516
### Fixed
1617
- issue where certain ways to compose `Effect`s unintentionally nullified the effect

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn1/Sort.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.jnape.palatable.lambda.functions.builtin.fn1;
22

33
import com.jnape.palatable.lambda.functions.Fn1;
4+
import com.jnape.palatable.lambda.functions.builtin.fn2.SortBy;
5+
import com.jnape.palatable.lambda.functions.builtin.fn2.SortWith;
46

57
import java.util.List;
68

@@ -12,7 +14,8 @@
1214
* this is both eager and monolithic.
1315
*
1416
* @param <A> the input Iterable and output List element type
15-
* @see com.jnape.palatable.lambda.functions.builtin.fn2.SortBy
17+
* @see SortBy
18+
* @see SortWith
1619
*/
1720
public final class Sort<A extends Comparable<A>> implements Fn1<Iterable<A>, List<A>> {
1821

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/SortBy.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import com.jnape.palatable.lambda.functions.Fn1;
44
import com.jnape.palatable.lambda.functions.Fn2;
5+
import com.jnape.palatable.lambda.functions.builtin.fn1.Sort;
56

6-
import java.util.ArrayList;
77
import java.util.List;
88
import java.util.function.Function;
99

10-
import static com.jnape.palatable.lambda.functions.builtin.fn2.ToCollection.toCollection;
10+
import static com.jnape.palatable.lambda.functions.builtin.fn2.SortWith.sortWith;
1111
import static java.util.Comparator.comparing;
1212

1313
/**
@@ -17,7 +17,8 @@
1717
*
1818
* @param <A> the input Iterable and output List element type
1919
* @param <B> the mapped Comparable type
20-
* @see com.jnape.palatable.lambda.functions.builtin.fn1.Sort
20+
* @see Sort
21+
* @see SortWith
2122
*/
2223
public final class SortBy<A, B extends Comparable<B>> implements Fn2<Function<? super A, ? extends B>, Iterable<A>, List<A>> {
2324

@@ -28,9 +29,7 @@ private SortBy() {
2829

2930
@Override
3031
public List<A> apply(Function<? super A, ? extends B> fn, Iterable<A> as) {
31-
List<A> result = toCollection(ArrayList::new, as);
32-
result.sort(comparing(fn));
33-
return result;
32+
return sortWith(comparing(fn), as);
3433
}
3534

3635
@SuppressWarnings("unchecked")
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn2;
2+
3+
import com.jnape.palatable.lambda.functions.Fn1;
4+
import com.jnape.palatable.lambda.functions.Fn2;
5+
import com.jnape.palatable.lambda.functions.builtin.fn1.Sort;
6+
7+
import java.util.ArrayList;
8+
import java.util.Comparator;
9+
import java.util.List;
10+
11+
import static com.jnape.palatable.lambda.functions.builtin.fn2.ToCollection.toCollection;
12+
13+
/**
14+
* Given an {@link Iterable} and a {@link java.util.Comparator} over the {@link Iterable} element type, produce a
15+
* sorted {@link List} of the original elements based on sorting applied by the {@link java.util.Comparator}. Note that
16+
* this is both eager and monolithic.
17+
*
18+
* @param <A> the input Iterable and output List element type
19+
* @see Sort
20+
* @see SortBy
21+
*/
22+
public final class SortWith<A> implements Fn2<Comparator<? super A>, Iterable<A>, List<A>> {
23+
24+
private static final SortWith INSTANCE = new SortWith();
25+
26+
private SortWith() {
27+
}
28+
29+
@Override
30+
public List<A> apply(Comparator<? super A> comparator, Iterable<A> as) {
31+
List<A> result = toCollection(ArrayList::new, as);
32+
result.sort(comparator);
33+
return result;
34+
}
35+
36+
@SuppressWarnings("unchecked")
37+
public static <A> SortWith<A> sortWith() {
38+
return INSTANCE;
39+
}
40+
41+
public static <A> Fn1<Iterable<A>, List<A>> sortWith(Comparator<? super A> comparator) {
42+
return SortWith.<A>sortWith().apply(comparator);
43+
}
44+
45+
public static <A> List<A> sortWith(Comparator<? super A> comparator, Iterable<A> as) {
46+
return SortWith.<A>sortWith(comparator).apply(as);
47+
}
48+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn2;
2+
3+
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
4+
import org.junit.Test;
5+
6+
import java.util.Comparator;
7+
8+
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
9+
import static com.jnape.palatable.lambda.functions.builtin.fn2.SortWith.sortWith;
10+
import static java.util.Arrays.asList;
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class SortWithTest {
14+
15+
@Test
16+
public void sortsWithGivenComparator() {
17+
assertEquals(asList(tuple("bar", 4), tuple("baz", 3), tuple("foo", 1), tuple("foo", 2)),
18+
sortWith(Comparator.<Tuple2<String, Integer>, String>comparing(Tuple2::_1)
19+
.thenComparing(Tuple2::_2),
20+
asList(tuple("foo", 1), tuple("foo", 2), tuple("bar", 4), tuple("baz", 3))));
21+
}
22+
}

0 commit comments

Comments
 (0)