public static void main(String[] args) {

    // An Operation on a stream that returns a stream is an intermediary operation

    Stream<String> stream1 = Stream.of("one", "two", "three", "four", "five");
    Predicate<String> p1 = Predicate.isEqual("two");
    stream1.filter(p1); // This is an intermediary operation and it does nothing!

    /*
       The below piece of code does nothing!
       peek() and filter() methods are intermediary operations as they return a stream.
       Hence, this code does not prints anything.
       It is just a simple declaration.
    */
    List<String> result1 = new ArrayList<>();
    Stream<String> stream2 = Stream.of("one", "two", "three", "four", "five");
    Predicate<String> p2 = Predicate.isEqual("two");
    stream2.peek(System.out::println).filter(p2).peek(result1::add);
    System.out.println("size -> " + result1.size());

    System.out.println("******************************************");
    /*
       Using Final method (forEach())
       Only the Final operation triggers the processing of the data the stream is connected to.
    */
    List<String> result2 = new ArrayList<>();
    Stream<String> stream3 = Stream.of("one", "two", "three", "four", "five");
    Predicate<String> p3 = Predicate.isEqual("two");
    Predicate<String> p4 = Predicate.isEqual("three");
    stream3.peek(System.out::println).filter(p3.or(p4)).forEach(result2::add);
    System.out.println("size -> " + result2.size());
  }
  private V getVar(
      final LispStruct parameter, final DeclareStruct declare, final Environment environment) {

    if (!(parameter instanceof SymbolStruct) && !(parameter instanceof ListStruct)) {
      throw new TypeErrorException(
          expanderName + ": PARAMETER must be a Symbol or a List. Got: " + parameter);
    }

    final SymbolStruct var;
    final LispStruct initForm;

    if (parameter instanceof ListStruct) {
      final ListStruct listParameter = (ListStruct) parameter;
      final Iterator<LispStruct> iterator = listParameter.iterator();

      if (!iterator.hasNext()) {
        throw new ProgramErrorException(
            expanderName + ": List parameter must have either 1 or 2 elements. Got: 0");
      }
      final LispStruct first = iterator.next();

      if (!(first instanceof SymbolStruct)) {
        throw new TypeErrorException(
            expanderName + ": First element of list parameter must be a Symbol. Got: " + first);
      }
      var = (SymbolStruct) first;

      if (iterator.hasNext()) {
        final LispStruct value = iterator.next();
        initForm = getListParameterInitForm(value, environment);

        if (iterator.hasNext()) {
          throw new ProgramErrorException(
              expanderName + ": List parameter must have either 1 or 2 elements. Got: 3");
        }
      } else {
        initForm = NILStruct.INSTANCE;
      }
    } else {
      var = (SymbolStruct) parameter;
      initForm = NILStruct.INSTANCE;
    }

    final boolean isSpecial =
        declare
            .getSpecialDeclarations()
            .stream()
            .map(SpecialDeclarationStruct::getVar)
            .anyMatch(Predicate.isEqual(var));

    final Binding binding = new Binding(var);
    if (isSpecial) {
      environment.addDynamicBinding(binding);
    } else {
      environment.addLexicalBinding(binding);
    }

    return getClosureCreationVar(var, initForm, isSpecial);
  }
 @Override
 public Puzzle apply(Puzzle puzzle) {
   for (Iterator<Pair<Node, Node>> it = puzzle.terminals().iterator(); it.hasNext(); ) {
     Pair<Node, Node> terminals = it.next();
     if (puzzle.neighbors(terminals.first).anyMatch(Predicate.isEqual(terminals.second))
         && puzzle.nodes().filter(n -> n.kind() == terminals.first.kind()).count() > 2)
       puzzle = puzzle.set(terminals.first, terminals.second, Node.Kind.NONE);
   }
   return puzzle;
 }
Exemple #4
0
  private void predicate() {
    Predicate<String> p1 = s -> s.length() < 20;
    Predicate<String> p2 = s -> s.length() > 10;

    Predicate<String> p3 = p1.and(p2);

    Predicate<String> id = Predicate.isEqual(p1);

    //        List<String>

  }
 public static void main(String[] args) throws Exception {
   if (args.length < 2) {
     System.err.println("Ahem, I need at least 2 arguments please.");
     System.exit(1);
     return;
   }
   Pattern scalePat = Pattern.compile("-(?:-scale|s)(?:-(w|h))?=(\\d+)");
   @SuppressWarnings("unchecked")
   Predicate<String> isVerbose =
       (Predicate<String>) (Object) Predicate.isEqual("-v").or(Predicate.isEqual("--verbose"));
   boolean verbose = Stream.of(args).anyMatch(isVerbose);
   OptionalInt[] packed =
       Stream.of(args)
           .map(scalePat::matcher)
           .filter(Matcher::matches)
           .map(
               matcher -> {
                 OptionalInt[] ints = new OptionalInt[] {OptionalInt.empty(), OptionalInt.empty()};
                 OptionalInt scale = OptionalInt.of(Integer.parseInt(matcher.group(2)));
                 if (matcher.group(1) == null) {
                   ints[0] = ints[1] = scale;
                 } else {
                   if (matcher.group(1).equals("w")) {
                     ints[0] = scale;
                   } else {
                     ints[1] = scale;
                   }
                 }
                 return ints;
               })
           .reduce(
               new OptionalInt[] {OptionalInt.empty(), OptionalInt.empty()},
               (id, next) -> {
                 OptionalInt[] ints = new OptionalInt[2];
                 OptionalInt aID = id[0];
                 OptionalInt bID = id[1];
                 OptionalInt aNx = next[0];
                 OptionalInt bNx = next[1];
                 ints[0] = aNx.isPresent() ? aNx : aID;
                 ints[1] = bNx.isPresent() ? bNx : bID;
                 return ints;
               });
   int scaleWidth = packed[0].orElse(1);
   int scaleHeight = packed[1].orElse(1);
   Pattern lightPat = Pattern.compile("-(?:-light|l)=([dDbB]+)");
   List<Function<Color, Color>> lightChanges =
       Stream.of(args)
           .map(lightPat::matcher)
           .filter(Matcher::matches)
           .flatMap(
               m ->
                   m.group(1)
                       .chars()
                       .mapToObj(
                           c ->
                               c == 'd' || c == 'D'
                                   ? (Function<Color, Color>) Color::darker
                                   : (Function<Color, Color>) Color::brighter))
           .collect(Collectors.toList());
   args =
       Stream.of(args)
           .filter(isVerbose.or(scalePat.asPredicate()).or(lightPat.asPredicate()).negate())
           .toArray(String[]::new);
   Random r = new Random();
   int width = Integer.parseInt(args[0]);
   int height = Integer.parseInt(args[1]);
   String file = args.length > 2 ? args[2] : ":-";
   if (verbose) {
     System.err.println("Generating an image...");
   }
   byte[] pixels = new byte[width * height * 4];
   r.nextBytes(pixels);
   BufferedImage created = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
   for (int i = 0, index = 0; i < pixels.length; i += 4, index++) {
     int x = index % width;
     int y = index / width;
     Color color =
         new Color(
             ((/* 0x0F */ 0xFF) << 24)
                 | ((pixels[i + 1] & 0xFF) << 16)
                 | ((pixels[i + 2] & 0xFF) << 8)
                 | ((pixels[i + 3] & 0xFF)));
     for (Function<Color, Color> change : lightChanges) {
       color = change.apply(color);
     }
     created.setRGB(x, y, color.getRGB());
   }
   int scaledWidth = width * scaleWidth;
   int scaledHeight = height * scaleHeight;
   BufferedImage tmp = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
   Graphics2D g = tmp.createGraphics();
   AffineTransform at = AffineTransform.getScaleInstance(scaleWidth, scaleHeight);
   g.drawRenderedImage(created, at);
   created = tmp;
   if (verbose) {
     System.err.println("Writing to file...");
   }
   ImageIO.write(
       created, "PNG", file.equals(":-") ? System.out : Files.newOutputStream(Paths.get(file)));
   if (verbose) {
     System.err.println("Complete...");
   }
 }