예제 #1
1
 @Test
 public void testFlattenResult() {
   List<Integer> list = List.list(1, 2, 3, 4, 5, 0, 6, 7, 8, 9);
   List<Double> list2 = List.flattenResult(list.map(inverse));
   assertEquals(
       "[1.0, 0.5, 0.3333333333333333, 0.25, 0.2, 0.16666666666666666, 0.14285714285714285, 0.125, 0.1111111111111111, NIL]",
       list2.toString());
 }
예제 #2
0
 @Test
 public void testExists() throws Exception {
   List<Integer> list = List.list(1, 2, 3, 4, 5, 6, 7, 8);
   assertTrue(list.exists(x -> x < 4));
   assertFalse(list.exists(x -> x < 1));
   assertTrue(list.exists(x -> x > 0));
   assertTrue(list.exists(x -> x > 4));
 }
예제 #3
0
 @Test
 public void testExistsEmpty() throws Exception {
   List<Integer> list = List.list();
   assertFalse(list.exists(x -> x < 4));
   assertFalse(list.exists(x -> x < 1));
   assertFalse(list.exists(x -> x > 0));
   assertFalse(list.exists(x -> x > 4));
 }
예제 #4
0
 /**
  * This test demonstrates the known problem of creating streams from mutable structures.
  *
  * <p>Some of the ways streams created in this way can fail is: - weak stream references getting
  * garbage collected - underlying mutable data structure changes - iterator gets updated (e.g.
  * iterator used to create 2 different streams).
  */
 @Test(expected = ConcurrentModificationException.class)
 public void iterableStreamWithStructureUpdate() {
   java.util.List<Integer> list = List.list(1, 2, 3).toJavaList();
   Stream<Integer> s1 = Stream.fromIterable(list);
   int x = s1.head();
   list.remove(1);
   Stream<Integer> s2 = s1.tail()._1();
   x = s2.head();
 }
예제 #5
0
 /*
  * A simple recursive solution
  */
 public static Tuple<List<Integer>, RNG> ints(int count, RNG rng) {
   return count == 0 ? new Tuple<>(List.list(), rng) : intsHelper(count, rng);
 }
예제 #6
0
 /*
  * A tail-recursive stack safe solution. Note that the output list is in
  * reverse order, but this is perfectly acceptable regarding the requirements.
  */
 public static Tuple<List<Integer>, RNG> ints2(int count, RNG rng) {
   return ints2(count, rng, List.list()).eval();
 }
예제 #7
0
 /*
  * In `sequence`, the base case of the fold is a `unit` action that returns
  * the empty list. At each step in the fold, we accumulate in `acc` and `f` is
  * the current element in the list. `map2(f, acc)(_ :: _)` results in a value
  * of type `Rand[List[A]]` We map over that to prepend (cons) the element onto
  * the accumulated list.
  *
  * We are using `foldRight`. If we used `foldLeft` then the values in the
  * resulting list would appear in reverse order. It would be arguably better
  * to use `foldLeft` followed by `reverse`. What do you think?
  */
 public static <A> Rand<List<A>> sequence(List<Rand<A>> fs) {
   return fs.foldRight(unit(List.list()), f -> acc -> map2(f, acc, x -> y -> y.cons(x)));
 }
예제 #8
0
 /*
  * A tail-recursive stack safe solution. Note that the output list is in
  * reverse order, but this is perfectly acceptable regarding the requirements.
  */
 public static Tuple<List<Double>, RNG> doubles(int count, RNG rng) {
   return doubles(count, rng, List.list()).eval();
 }
예제 #9
0
파일: Heap.java 프로젝트: jmgimeno/fpinjava
 public static <A> List<A> toList(Heap<A> heap) {
   return heap.tail()
       .flatMap(tail -> heap.head().map(head -> toList(tail).cons(head)))
       .getOrElse(List.list());
 }
예제 #10
0
파일: Heap.java 프로젝트: jmgimeno/fpinjava
 public List<A> toList() {
   return toList(List.list(), this).eval();
 }
예제 #11
0
 /**
  * Constructs a non empty list from the given elements.
  *
  * @param head The first in the non-empty list.
  * @param tail The elements to construct a list's tail with.
  * @return A non-empty list with the given elements.
  */
 @SafeVarargs
 public static <A> NonEmptyList<A> nel(final A head, final A... tail) {
   return nel(head, List.list(tail));
 }