|
1 | 1 | package fj.data; |
2 | 2 |
|
| 3 | +import fj.P; |
3 | 4 | import fj.control.Trampoline; |
4 | 5 | import org.junit.Test; |
5 | 6 |
|
6 | 7 | import java.io.IOException; |
7 | 8 |
|
8 | 9 | import static fj.Function.constant; |
9 | 10 | import static fj.Ord.*; |
10 | | -import static fj.P.p; |
| 11 | +import static fj.P.*; |
11 | 12 | import static fj.data.Either.*; |
12 | 13 | import static fj.data.List.*; |
| 14 | +import static fj.data.Option.iif; |
13 | 15 | import static fj.data.Option.sequence; |
14 | | -import static fj.data.Option.sequenceEitherLeft; |
15 | | -import static fj.data.Option.sequenceEitherRight; |
16 | | -import static fj.data.Option.sequenceF; |
17 | | -import static fj.data.Option.sequenceIO; |
18 | | -import static fj.data.Option.sequenceList; |
19 | | -import static fj.data.Option.sequenceOption; |
20 | | -import static fj.data.Option.sequenceP1; |
21 | | -import static fj.data.Option.sequenceSeq; |
22 | | -import static fj.data.Option.sequenceSet; |
23 | | -import static fj.data.Option.sequenceStream; |
24 | | -import static fj.data.Option.sequenceTrampoline; |
25 | | -import static fj.data.Option.sequenceValidation; |
26 | 16 | import static fj.data.Option.*; |
27 | 17 | import static fj.data.Validation.fail; |
28 | 18 | import static fj.data.Validation.*; |
@@ -66,6 +56,177 @@ public void sequenceValidationTest() { |
66 | 56 | assertEquals(some(success("string")), sequence(Validation.<Integer, Option<String>>success(some("string")))); |
67 | 57 | } |
68 | 58 |
|
| 59 | + @Test |
| 60 | + public void testBind1() { |
| 61 | + range(0, 1).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 62 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 63 | + list.map(List::single) : |
| 64 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 65 | + .forEach(list -> { |
| 66 | + assertEquals(iif(list.forall(Option::isSome), 0), list.index(0).bind(Option::some)); |
| 67 | + }); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testBind2() { |
| 73 | + range(0, 2).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 74 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 75 | + list.map(List::single) : |
| 76 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 77 | + .forEach(list -> { |
| 78 | + assertEquals(iif(list.forall(Option::isSome), p(0, 1)), list.index(0).bind(list.index(1), p2())); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testBind3() { |
| 84 | + range(0, 3).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 85 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 86 | + list.map(List::single) : |
| 87 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 88 | + .forEach(list -> { |
| 89 | + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2)), list.index(0).bind(list.index(1), list.index(2), p3())); |
| 90 | + }); |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testBind4() { |
| 96 | + range(0, 4).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 97 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 98 | + list.map(List::single) : |
| 99 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 100 | + .forEach(list -> { |
| 101 | + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3)), list.index(0).bind(list.index(1), list.index(2), list.index(3), p4())); |
| 102 | + }); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testBind5() { |
| 108 | + range(0, 5).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 109 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 110 | + list.map(List::single) : |
| 111 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 112 | + .forEach(list -> { |
| 113 | + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4)), list.index(0).bind(list.index(1), list.index(2), list.index(3), list.index(4), p5())); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testBind6() { |
| 119 | + range(0, 6).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 120 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 121 | + list.map(List::single) : |
| 122 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 123 | + .forEach(list -> { |
| 124 | + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4, 5)), list.index(0).bind(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), p6())); |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testBind7() { |
| 130 | + range(0, 7).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 131 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 132 | + list.map(List::single) : |
| 133 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 134 | + .forEach(list -> { |
| 135 | + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4, 5, 6)), list.index(0).bind(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6), p7())); |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testBind8() { |
| 141 | + range(0, 8).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 142 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 143 | + list.map(List::single) : |
| 144 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 145 | + .forEach(list -> { |
| 146 | + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4, 5, 6, 7)), list.index(0).bind(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6), list.index(7), P.<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer>p8())); |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void testBindProduct2() { |
| 152 | + range(0, 2).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 153 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 154 | + list.map(List::single) : |
| 155 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 156 | + .forEach(list -> { |
| 157 | + assertEquals(iif(list.forall(Option::isSome), p(0, 1)), list.index(0).bindProduct(list.index(1))); |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testBindProduct3() { |
| 163 | + range(0, 3).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 164 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 165 | + list.map(List::single) : |
| 166 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 167 | + .forEach(list -> { |
| 168 | + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2)), list.index(0).bindProduct(list.index(1), list.index(2))); |
| 169 | + }); |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void testBindProduct4() { |
| 175 | + range(0, 4).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 176 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 177 | + list.map(List::single) : |
| 178 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 179 | + .forEach(list -> { |
| 180 | + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3)), list.index(0).bindProduct(list.index(1), list.index(2), list.index(3))); |
| 181 | + }); |
| 182 | + |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void testBindProduct5() { |
| 187 | + range(0, 5).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 188 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 189 | + list.map(List::single) : |
| 190 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 191 | + .forEach(list -> { |
| 192 | + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4)), list.index(0).bindProduct(list.index(1), list.index(2), list.index(3), list.index(4))); |
| 193 | + }); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + public void testBindProduct6() { |
| 198 | + range(0, 6).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 199 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 200 | + list.map(List::single) : |
| 201 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 202 | + .forEach(list -> { |
| 203 | + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4, 5)), list.index(0).bindProduct(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5))); |
| 204 | + }); |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + public void testBindProduct7() { |
| 209 | + range(0, 7).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 210 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 211 | + list.map(List::single) : |
| 212 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 213 | + .forEach(list -> { |
| 214 | + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4, 5, 6)), list.index(0).bindProduct(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6))); |
| 215 | + }); |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + public void testBindProduct8() { |
| 220 | + range(0, 8).map(i -> arrayList(Option.<Integer>none(), some(i))) |
| 221 | + .foldLeft(accumulator -> list -> accumulator.isEmpty() ? |
| 222 | + list.map(List::single) : |
| 223 | + accumulator.bind(accumulatorElement -> list.map(accumulatorElement::snoc)), List.<List<Option<Integer>>>nil()) |
| 224 | + .forEach(list -> { |
| 225 | + assertEquals(iif(list.forall(Option::isSome), p(0, 1, 2, 3, 4, 5, 6, 7)), list.index(0).bindProduct(list.index(1), list.index(2), list.index(3), list.index(4), list.index(5), list.index(6), list.index(7))); |
| 226 | + }); |
| 227 | + } |
| 228 | + |
| 229 | + |
69 | 230 | @Test |
70 | 231 | public void testSequenceEitherLeft() { |
71 | 232 | assertEquals(left(none()), sequenceEitherLeft(none())); |
|
0 commit comments