Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8017486

Lambda not return expected values

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 8
    • tools
    • os_x

      FULL PRODUCT VERSION :
      java version " 1.8.0-ea "
      Java(TM) SE Runtime Environment (build 1.8.0-ea-b94)
      Java HotSpot(TM) 64-Bit Server VM (build 25.0-b36, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Darwin Trungs-MacBook-Pro.local 12.4.0 Darwin Kernel Version 12.4.0: Wed May 1 17:57:12 PDT 2013; root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64

      A DESCRIPTION OF THE PROBLEM :
      Lambda function did not return correct values.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      > javac Bug.java
      > java Bug
      T T : wrong outcome
      F T : right outcome




      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.*;
      import java.util.function.Function;

      public class Bug {

          public static <P, Q> Iterator<Q> makeIterator(Iterator<P> iterator, Function<P, Q> extractor) {
              if (iterator == null) {
                  return Collections.emptyIterator();
              }

              return new Iterator<Q>() {
                  @Override
                  public boolean hasNext() {
                      return iterator.hasNext();
                  }

                  @Override
                  public Q next() {
                      P next = iterator.next();
                      Q value = extractor.apply(next);
                      return value;
                  }

                  @Override
                  public void remove() {
                      throw new UnsupportedOperationException();
                  }
              };
          }

          public static <P, Q> Iterable<Q> makeIterable(Iterable<P> iterable, Function<P, Q> extractor) {
              if (iterable == null) return Collections.emptyList();

              return () -> makeIterator(iterable.iterator(), extractor);
          }

          public static Iterable<Boolean> makeIterable(final boolean[] a) {
              return () -> new Iterator<Boolean>(){
                  private int index = 0;
                  @Override
                  public boolean hasNext() {
                      return index < a.length;
                  }

                  @Override
                  public Boolean next() {
                      return a[index++];
                  }
              };
          }

          public static void main(String ... args) {

              boolean[] a = new boolean[]{false, true};

              for (String s : makeIterable(makeIterable(a), (b) -> b ? " T " : " F " )) {
                  System.out.print(s);
                  System.out.print(' ');
              }
              System.out.println( " : wrong outcome " );

              Function<Boolean, String> f = (b) -> b ? " T " : " F " ;
              for (String s : makeIterable(makeIterable(a), f)) {
                  System.out.print(s);
                  System.out.print(' ');
              }
              System.out.println( " : right outcome " );

          }

      }

      ---------- END SOURCE ----------

            mcimadamore Maurizio Cimadamore
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: