示例#1
0
 /*
  * This implementation of map2 passes the initial RNG to the first argument
  * and the resulting RNG to the second argument. It's not necessarily wrong to
  * do this the other way around, since the results are random anyway. We could
  * even pass the initial RNG to both `f` and `g`, but that might have
  * unexpected results. E.g. if both arguments are `RNG.int` then we would
  * always get two of the same `Int` in the result. When implementing functions
  * like this, it's important to consider how we would test them for
  * correctness.
  */
 public static <A, B, C> Rand<C> map2_(Rand<A> ra, Rand<B> rb, Function<A, Function<B, C>> f) {
   return rng -> {
     Tuple<A, RNG> t1 = ra.apply(rng);
     Tuple<B, RNG> t2 = rb.apply(t1._2);
     return new Tuple<>(f.apply(t1._1).apply(t2._1), t2._2);
   };
 }
示例#2
0
 public static <A, B> Rand<B> flatMap(Rand<A> f, Function<A, Rand<B>> g) {
   return rng -> {
     Tuple<A, RNG> t = f.apply(rng);
     return g.apply(t._1).apply(t._2); // We pass the new input along
   };
 }
示例#3
0
 public static <A, B> Rand<B> map_(Rand<A> s, Function<A, B> f) {
   return rng -> {
     Tuple<A, RNG> t = s.apply(rng);
     return new Tuple<>(f.apply(t._1), t._2);
   };
 }