Skip to content

Commit 3548db1

Browse files
committed
functionaljava#235 Changes after Code Review + added Gen.pickOne()
1 parent 498e59b commit 3548db1

File tree

2 files changed

+74
-62
lines changed

2 files changed

+74
-62
lines changed

quickcheck/src/main/java/fj/test/Gen.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -563,20 +563,32 @@ public static <A> Gen<List<A>> listOf1(final Gen<A> g) {
563563
return listOf(g, 1);
564564
}
565565

566+
/**
567+
* Returns a generator that picks one element from the given list. If the given list is empty, then the
568+
* returned generator will never produce a value.
569+
*
570+
* @param as The list from which to pick an element.
571+
* @return A generator that picks an element from the given list.
572+
*/
573+
public static <A> Gen<A> pickOne(List<A> as) {
574+
// This is the fastest of the four; functionally, any of them would do
575+
return wordOf(1, as).map(List::head);
576+
}
577+
566578
/**
567579
* Returns a generator of lists that picks the given number of elements from the given list. If
568580
* the given number is less than zero or greater than the length of the given list, then the
569581
* returned generator will never produce a value.
570582
* <p>
571-
* Note: pick is synonymous with pickCombinationWithoutReplacement
583+
* Note: pick is synonymous with combinationOf
572584
*
573585
* @param n The number of elements to pick from the given list.
574586
* @param as The list from which to pick elements.
575587
* @return A generator of lists that picks the given number of elements from the given list.
576588
*/
577589
@Deprecated
578590
public static <A> Gen<List<A>> pick(int n, List<A> as) {
579-
return pickCombinationWithoutReplacement(n, as);
591+
return combinationOf(n, as);
580592
}
581593

582594
/**
@@ -594,7 +606,7 @@ public static <A> Gen<List<A>> pick(int n, List<A> as) {
594606
* @param as The list from which to pick elements.
595607
* @return A generator of lists that picks the given number of elements from the given list.
596608
*/
597-
public static <A> Gen<List<A>> pickCombinationWithoutReplacement(int n, List<A> as) {
609+
public static <A> Gen<List<A>> combinationOf(int n, List<A> as) {
598610
int aLength = as.length();
599611
return ((n >= 0) && (n <= aLength)) ?
600612
parameterised(s -> r -> {
@@ -634,10 +646,10 @@ private Trampoline<List<A>> tramp(List<A> remainAs, int remainN, int remainALeng
634646
* @param as The list from which to pick elements.
635647
* @return A generator of lists that picks the given number of elements from the given list.
636648
*/
637-
public static <A> Gen<List<A>> pickCombinationWithReplacement(int n, List<A> as) {
649+
public static <A> Gen<List<A>> selectionOf(int n, List<A> as) {
638650
Array<A> aArr = as.toArray();
639651
return (n >= 0) ?
640-
pick(indexPermutation(n, aArr.length()).map(indexes -> indexes.sort(intOrd)), aArr) :
652+
pick(indexWord(n, aArr.length()).map(indexes -> indexes.sort(intOrd)), aArr) :
641653
fail();
642654
}
643655

@@ -656,9 +668,9 @@ public static <A> Gen<List<A>> pickCombinationWithReplacement(int n, List<A> as)
656668
* @param as The list from which to pick elements.
657669
* @return A generator of lists that picks the given number of elements from the given list.
658670
*/
659-
public static <A> Gen<List<A>> pickPermutationWithoutReplacement(int n, List<A> as) {
671+
public static <A> Gen<List<A>> permutationOf(int n, List<A> as) {
660672
return parameterised(s -> r ->
661-
pickCombinationWithoutReplacement(n, as).map(combination -> {
673+
combinationOf(n, as).map(combination -> {
662674
// Shuffle combination using the Fisher-Yates algorithm
663675
Array<A> aArr = combination.toArray();
664676
int length = aArr.length();
@@ -687,14 +699,14 @@ public static <A> Gen<List<A>> pickPermutationWithoutReplacement(int n, List<A>
687699
* @param as The list from which to pick elements.
688700
* @return A generator of lists that picks the given number of elements from the given list.
689701
*/
690-
public static <A> Gen<List<A>> pickPermutationWithReplacement(int n, List<A> as) {
702+
public static <A> Gen<List<A>> wordOf(int n, List<A> as) {
691703
Array<A> aArr = as.toArray();
692704
return (n >= 0) ?
693-
pick(indexPermutation(n, aArr.length()), aArr) :
705+
pick(indexWord(n, aArr.length()), aArr) :
694706
fail();
695707
}
696708

697-
private static Gen<List<Integer>> indexPermutation(int n, int m) {
709+
private static Gen<List<Integer>> indexWord(int n, int m) {
698710
return sequenceN(n, choose(0, m - 1));
699711
}
700712

@@ -706,14 +718,14 @@ private static <A> Gen<List<A>> pick(Gen<List<Integer>> indexesGen, Array<A> as)
706718
/**
707719
* Returns a generator of lists that produces some of the values of the given list.
708720
* <p>
709-
* Note: someOf is synonymous with someCombinationWithoutReplacementOf
721+
* Note: someOf is synonymous with someCombinationOf
710722
*
711723
* @param as The list from which to pick values.
712724
* @return A generator of lists that produces some of the values of the given list.
713725
*/
714726
@Deprecated
715727
public static <A> Gen<List<A>> someOf(List<A> as) {
716-
return someCombinationWithoutReplacementOf(as);
728+
return someCombinationOf(as);
717729
}
718730

719731
/**
@@ -727,8 +739,8 @@ public static <A> Gen<List<A>> someOf(List<A> as) {
727739
* @param as The list from which to pick values.
728740
* @return A generator of lists that produces some of the values of the given list.
729741
*/
730-
public static <A> Gen<List<A>> someCombinationWithoutReplacementOf(List<A> as) {
731-
return choose(0, as.length()).bind(n -> pickCombinationWithoutReplacement(n, as));
742+
public static <A> Gen<List<A>> someCombinationOf(List<A> as) {
743+
return choose(0, as.length()).bind(n -> combinationOf(n, as));
732744
}
733745

734746
/**
@@ -743,8 +755,8 @@ public static <A> Gen<List<A>> someCombinationWithoutReplacementOf(List<A> as) {
743755
* @param as The list from which to pick values.
744756
* @return A generator of lists that produces some of the values of the given list.
745757
*/
746-
public static <A> Gen<List<A>> someCombinationWithReplacementOf(int maxLength, List<A> as) {
747-
return choose(0, maxLength).bind(n -> pickCombinationWithReplacement(n, as));
758+
public static <A> Gen<List<A>> someSelectionOf(int maxLength, List<A> as) {
759+
return choose(0, maxLength).bind(n -> selectionOf(n, as));
748760
}
749761

750762
/**
@@ -758,8 +770,8 @@ public static <A> Gen<List<A>> someCombinationWithReplacementOf(int maxLength, L
758770
* @param as The list from which to pick values.
759771
* @return A generator of lists that produces some of the values of the given list.
760772
*/
761-
public static <A> Gen<List<A>> somePermutationWithoutReplacementOf(List<A> as) {
762-
return choose(0, as.length()).bind(n -> pickPermutationWithoutReplacement(n, as));
773+
public static <A> Gen<List<A>> somePermutationOf(List<A> as) {
774+
return choose(0, as.length()).bind(n -> permutationOf(n, as));
763775
}
764776

765777
/**
@@ -774,8 +786,8 @@ public static <A> Gen<List<A>> somePermutationWithoutReplacementOf(List<A> as) {
774786
* @param as The list from which to pick values.
775787
* @return A generator of lists that produces some of the values of the given list.
776788
*/
777-
public static <A> Gen<List<A>> somePermutationWithReplacementOf(int maxLength, List<A> as) {
778-
return choose(0, maxLength).bind(n -> pickPermutationWithReplacement(n, as));
789+
public static <A> Gen<List<A>> someWordOf(int maxLength, List<A> as) {
790+
return choose(0, maxLength).bind(n -> wordOf(n, as));
779791
}
780792

781793
/**

quickcheck/src/test/java/fj/test/TestGen.java renamed to quickcheck/src/test/java/fj/test/GenTest.java

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,38 @@
77
import static fj.Ord.charOrd;
88
import static fj.data.List.list;
99
import static fj.data.List.range;
10-
import static fj.test.Gen.pickCombinationWithReplacement;
11-
import static fj.test.Gen.pickCombinationWithoutReplacement;
12-
import static fj.test.Gen.pickPermutationWithReplacement;
13-
import static fj.test.Gen.pickPermutationWithoutReplacement;
10+
import static fj.test.Gen.selectionOf;
11+
import static fj.test.Gen.combinationOf;
12+
import static fj.test.Gen.wordOf;
13+
import static fj.test.Gen.permutationOf;
1414
import static fj.test.Rand.standard;
1515
import static org.junit.Assert.assertEquals;
1616
import static org.junit.Assert.assertTrue;
1717

18-
public final class TestGen {
18+
public final class GenTest {
1919

2020
private static final List<Character> AS = list('A', 'B', 'C');
2121

2222
@Test
23-
public void testPickCombinationWithoutReplacement_none() {
24-
Gen<List<Character>> instance = pickCombinationWithoutReplacement(0, AS);
23+
public void testCombinationOf_none() {
24+
Gen<List<Character>> instance = combinationOf(0, AS);
2525
testPick(100, instance, actual -> {
2626
assertTrue(actual.isEmpty());
2727
});
2828
}
2929

3030
@Test
31-
public void testPickCombinationWithoutReplacement_one() {
32-
Gen<List<Character>> instance = pickCombinationWithoutReplacement(1, AS);
31+
public void testCombinationOf_one() {
32+
Gen<List<Character>> instance = combinationOf(1, AS);
3333
testPick(100, instance, actual -> {
3434
assertEquals(1, actual.length());
3535
assertTrue(AS.exists(a -> a.equals(actual.head())));
3636
});
3737
}
3838

3939
@Test
40-
public void testPickCombinationWithoutReplacement_two() {
41-
Gen<List<Character>> instance = pickCombinationWithoutReplacement(2, AS);
40+
public void testCombinationOf_two() {
41+
Gen<List<Character>> instance = combinationOf(2, AS);
4242
testPick(100, instance, actual -> {
4343
assertEquals(2, actual.length());
4444
assertTrue(actual.forall(actualA -> AS.exists(a -> a.equals(actualA))));
@@ -47,8 +47,8 @@ public void testPickCombinationWithoutReplacement_two() {
4747
}
4848

4949
@Test
50-
public void testPickCombinationWithoutReplacement_three() {
51-
Gen<List<Character>> instance = pickCombinationWithoutReplacement(3, AS);
50+
public void testCombinationOf_three() {
51+
Gen<List<Character>> instance = combinationOf(3, AS);
5252
testPick(100, instance, actual -> {
5353
assertEquals(3, actual.length());
5454
assertTrue(actual.forall(actualA -> AS.exists(a -> a.equals(actualA))));
@@ -57,25 +57,25 @@ public void testPickCombinationWithoutReplacement_three() {
5757
}
5858

5959
@Test
60-
public void testPickCombinationWithReplacement_none() {
61-
Gen<List<Character>> instance = pickCombinationWithReplacement(0, AS);
60+
public void testSelectionOf_none() {
61+
Gen<List<Character>> instance = selectionOf(0, AS);
6262
testPick(100, instance, actual -> {
6363
assertTrue(actual.isEmpty());
6464
});
6565
}
6666

6767
@Test
68-
public void testPickCombinationWithReplacement_one() {
69-
Gen<List<Character>> instance = pickCombinationWithReplacement(1, AS);
68+
public void testSelectionOf_one() {
69+
Gen<List<Character>> instance = selectionOf(1, AS);
7070
testPick(100, instance, actual -> {
7171
assertEquals(1, actual.length());
7272
assertTrue(AS.exists(a -> a.equals(actual.head())));
7373
});
7474
}
7575

7676
@Test
77-
public void testPickCombinationWithReplacement_two() {
78-
Gen<List<Character>> instance = pickCombinationWithReplacement(2, AS);
77+
public void testSelectionOf_two() {
78+
Gen<List<Character>> instance = selectionOf(2, AS);
7979
testPick(100, instance, actual -> {
8080
assertEquals(2, actual.length());
8181
assertTrue(actual.forall(actualA -> AS.exists(a -> a.equals(actualA))));
@@ -84,8 +84,8 @@ public void testPickCombinationWithReplacement_two() {
8484
}
8585

8686
@Test
87-
public void testPickCombinationWithReplacement_three() {
88-
Gen<List<Character>> instance = pickCombinationWithReplacement(3, AS);
87+
public void testSelectionOf_three() {
88+
Gen<List<Character>> instance = selectionOf(3, AS);
8989
testPick(100, instance, actual -> {
9090
assertEquals(3, actual.length());
9191
assertTrue(actual.forall(actualA -> AS.exists(a -> a.equals(actualA))));
@@ -94,8 +94,8 @@ public void testPickCombinationWithReplacement_three() {
9494
}
9595

9696
@Test
97-
public void testPickCombinationWithReplacement_four() {
98-
Gen<List<Character>> instance = pickCombinationWithReplacement(4, AS);
97+
public void testSelectionOf_four() {
98+
Gen<List<Character>> instance = selectionOf(4, AS);
9999
testPick(100, instance, actual -> {
100100
assertEquals(4, actual.length());
101101
assertTrue(actual.forall(actualA -> AS.exists(a -> a.equals(actualA))));
@@ -104,34 +104,34 @@ public void testPickCombinationWithReplacement_four() {
104104
}
105105

106106
@Test
107-
public void testPickPermutationWithoutReplacement_none() {
108-
Gen<List<Character>> instance = pickPermutationWithoutReplacement(0, AS);
107+
public void testPermutationOf_none() {
108+
Gen<List<Character>> instance = permutationOf(0, AS);
109109
testPick(100, instance, actual -> {
110110
assertTrue(actual.isEmpty());
111111
});
112112
}
113113

114114
@Test
115-
public void testPickPermutationWithoutReplacement_one() {
116-
Gen<List<Character>> instance = pickPermutationWithoutReplacement(1, AS);
115+
public void testPermutationOf_one() {
116+
Gen<List<Character>> instance = permutationOf(1, AS);
117117
testPick(100, instance, actual -> {
118118
assertEquals(1, actual.length());
119119
assertTrue(AS.exists(a -> a.equals(actual.head())));
120120
});
121121
}
122122

123123
@Test
124-
public void testPickPermutationWithoutReplacement_two() {
125-
Gen<List<Character>> instance = pickCombinationWithoutReplacement(2, AS);
124+
public void testPermutationOf_two() {
125+
Gen<List<Character>> instance = combinationOf(2, AS);
126126
testPick(100, instance, actual -> {
127127
assertEquals(2, actual.length());
128128
assertTrue(actual.tails().forall(l -> l.isEmpty() || l.tail().forall(a -> !a.equals(l.head()))));
129129
});
130130
}
131131

132132
@Test
133-
public void testPickPermutationWithoutReplacement_three() {
134-
Gen<List<Character>> instance = pickPermutationWithoutReplacement(3, AS);
133+
public void testPermutationOf_three() {
134+
Gen<List<Character>> instance = permutationOf(3, AS);
135135
testPick(100, instance, actual -> {
136136
assertEquals(3, actual.length());
137137
assertTrue(actual.forall(actualA -> AS.exists(a -> a.equals(actualA))));
@@ -140,51 +140,51 @@ public void testPickPermutationWithoutReplacement_three() {
140140
}
141141

142142
@Test
143-
public void testPickPermutationWithReplacement_none() {
144-
Gen<List<Character>> instance = pickPermutationWithReplacement(0, AS);
143+
public void testWordOf_none() {
144+
Gen<List<Character>> instance = wordOf(0, AS);
145145
testPick(100, instance, actual -> {
146146
assertTrue(actual.isEmpty());
147147
});
148148
}
149149

150150
@Test
151-
public void testPickPermutationWithReplacement_one() {
152-
Gen<List<Character>> instance = pickPermutationWithReplacement(1, AS);
151+
public void testWordOf_one() {
152+
Gen<List<Character>> instance = wordOf(1, AS);
153153
testPick(100, instance, actual -> {
154154
assertEquals(1, actual.length());
155155
assertTrue(AS.exists(a -> a.equals(actual.head())));
156156
});
157157
}
158158

159159
@Test
160-
public void testPickPermutationWithReplacement_two() {
161-
Gen<List<Character>> instance = pickPermutationWithReplacement(2, AS);
160+
public void testWordOf_two() {
161+
Gen<List<Character>> instance = wordOf(2, AS);
162162
testPick(100, instance, actual -> {
163163
assertEquals(2, actual.length());
164164
assertTrue(actual.forall(actualA -> AS.exists(a -> a.equals(actualA))));
165165
});
166166
}
167167

168168
@Test
169-
public void testPickPermutationWithReplacement_three() {
170-
Gen<List<Character>> instance = pickPermutationWithReplacement(3, AS);
169+
public void testWordOf_three() {
170+
Gen<List<Character>> instance = wordOf(3, AS);
171171
testPick(100, instance, actual -> {
172172
assertEquals(3, actual.length());
173173
assertTrue(actual.forall(actualA -> AS.exists(a -> a.equals(actualA))));
174174
});
175175
}
176176

177177
@Test
178-
public void testPickPermutationWithReplacement_four() {
179-
Gen<List<Character>> instance = pickPermutationWithReplacement(4, AS);
178+
public void testWordOf_four() {
179+
Gen<List<Character>> instance = wordOf(4, AS);
180180
testPick(100, instance, actual -> {
181181
assertEquals(4, actual.length());
182182
assertTrue(actual.forall(actualA -> AS.exists(a -> a.equals(actualA))));
183183
});
184184
}
185185

186186
private static <A> void testPick(int n, Gen<List<A>> instance, Effect1<List<A>> test) {
187-
range(0, n).map(i -> instance.gen(0, standard)).foreachDoEffect(test::f);
187+
range(0, n).map(ignore -> instance.gen(0, standard)).foreachDoEffect(test::f);
188188
}
189189

190190
}

0 commit comments

Comments
 (0)