fork download
  1. import java.util.Objects;
  2. import java.util.Optional;
  3. import java.util.function.BiFunction;
  4. import java.util.function.Function;
  5. import java.util.function.Supplier;
  6. import java.util.stream.Stream;
  7.  
  8. @FunctionalInterface
  9. interface Selector<TYPE> {
  10. Object $get(Function<TYPE, Object> function);
  11.  
  12. @SuppressWarnings("unchecked")
  13. public default <VALUE> VALUE get(Function<TYPE, VALUE> function) {
  14. return (VALUE) $get(
  15. (Function<TYPE, Object>) function
  16. );
  17. }
  18.  
  19. public default TYPE from(Selector<TYPE> selector) {
  20. throw new InternalError("Selector::from is not implemented.");
  21. }
  22.  
  23. public default <VALUE> TYPE with(Function<TYPE, ? extends VALUE> function, VALUE value) {
  24. return from(f -> (f == function)
  25. ? value
  26. : get(f)
  27. );
  28. }
  29.  
  30. public static <TYPE> Object none(Function<TYPE, Object> function) {
  31. throw new IllegalStateException("no value");
  32. }
  33. }
  34.  
  35. interface Cons<ELEMENT> {
  36. public ELEMENT getElement();
  37. public Cell<ELEMENT> getNext();
  38. public Implementation<ELEMENT> getImplementation();
  39.  
  40. public Cons<ELEMENT> withElement(ELEMENT element);
  41. public Cons<ELEMENT> withNext(Cell<ELEMENT> next);
  42. public Cons<ELEMENT> withImplementation(Implementation<ELEMENT> implementation);
  43.  
  44. @FunctionalInterface
  45. public interface Implementation<ELEMENT> {
  46. public <VALUE> VALUE select(ELEMENT element, Cell<ELEMENT> next, BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil);
  47. }
  48.  
  49. public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
  50. return getImplementation()
  51. .select(
  52. getElement(),
  53. getNext(),
  54. cons,
  55. nil
  56. )
  57. ;
  58. }
  59.  
  60. public static <ELEMENT> Cons<ELEMENT> none() {
  61. return $Cons.none();
  62. }
  63. }
  64.  
  65. @FunctionalInterface
  66. interface $Cons<ELEMENT> extends Cons<ELEMENT>, Selector<Cons<ELEMENT>> {
  67. public static <ELEMENT> Function<Cons<ELEMENT>, ELEMENT> Element() {
  68. return Cons::getElement;
  69. }
  70.  
  71. public static <ELEMENT> Function<Cons<ELEMENT>, Cell<ELEMENT>> Next() {
  72. return Cons::getNext;
  73. }
  74.  
  75. public static <ELEMENT> Function<Cons<ELEMENT>, Implementation<ELEMENT>> Implementation() {
  76. return Cons::getImplementation;
  77. }
  78.  
  79. @Override
  80. public default ELEMENT getElement() {
  81. return get(Element());
  82. }
  83.  
  84. @Override
  85. public default Cell<ELEMENT> getNext() {
  86. return get(Next());
  87. }
  88.  
  89. @Override
  90. public default Implementation<ELEMENT> getImplementation() {
  91. return get(Implementation());
  92. }
  93.  
  94. @Override
  95. public default Cons<ELEMENT> withElement(ELEMENT element) {
  96. return with(Element(), element);
  97. }
  98.  
  99. @Override
  100. public default Cons<ELEMENT> withNext(Cell<ELEMENT> next) {
  101. return with(Next(), next);
  102. }
  103.  
  104. @Override
  105. public default Cons<ELEMENT> withImplementation(Implementation<ELEMENT> implementation) {
  106. return with(Implementation(), implementation);
  107. }
  108.  
  109. @Override
  110. public default Cons<ELEMENT> from(Selector<Cons<ELEMENT>> selector) {
  111. return ($Cons<ELEMENT>) selector::get;
  112. }
  113.  
  114. public static <ELEMENT> $Cons<ELEMENT> none() {
  115. return Selector::none;
  116. }
  117. }
  118.  
  119. @FunctionalInterface
  120. interface Cell<ELEMENT> {
  121. public <VALUE> VALUE select(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil);
  122.  
  123. public default int size() {
  124. return select(
  125. (element, next) -> 1 + next.size(),
  126. () -> 0
  127. );
  128. }
  129.  
  130. public default Cell<ELEMENT> first(int size) {
  131. return Optional.of(size)
  132. .<Cell<ELEMENT>>map(s -> select(
  133. (element, next) -> s == 0
  134. ? nil()
  135. : cons(element, next.first(s - 1))
  136. ,
  137. Cell::nil
  138. ))
  139. .orElseThrow(IllegalArgumentException::new)
  140. ;
  141. }
  142.  
  143. public default <ELEMENT2> Cell<ELEMENT2> map(Function<? super ELEMENT, ? extends ELEMENT2> mapper) {
  144. return select(
  145. (element, next) -> Cell.cons(mapper.apply(element), next.map(mapper)),
  146. Cell::nil
  147. );
  148. }
  149.  
  150. public default <ELEMENT2> Cell<ELEMENT2> flatMap(Function<? super ELEMENT, Cell<ELEMENT2>> mapper) {
  151. return select(
  152. (element, next) -> mapper
  153. .apply(element)
  154. .select(
  155. Cell::cons,
  156. () -> next.flatMap(mapper)
  157. )
  158. ,
  159. Cell::nil
  160. );
  161. }
  162.  
  163. public static <ELEMENT, VALUE> VALUE nil(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
  164. return nil.get();
  165. }
  166.  
  167. public static <ELEMENT, VALUE> VALUE cons(ELEMENT element, Cell<ELEMENT> next, BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
  168. return cons.apply(element, next);
  169. }
  170.  
  171. public static <ELEMENT> Cell<ELEMENT> nil() {
  172. return Cell::nil;
  173. }
  174.  
  175. public static <ELEMENT> Cell<ELEMENT> cons(ELEMENT element, Cell<ELEMENT> next) {
  176. Stream.of(
  177. element,
  178. next
  179. )
  180. .forEach(Objects::requireNonNull)
  181. ;
  182. return Cons.<ELEMENT>none()
  183. .withElement(element)
  184. .withNext(next)
  185. .withImplementation(Cell::cons)
  186. ::cons
  187. ;
  188. }
  189.  
  190. public static void main(String... arguments) {
  191. Cell<Integer> cell =
  192. cons(1, cons(2, cons(3, nil())))
  193. ;
  194. System.out.println(nil() == nil()); // true
  195. System.out.println(cell.size() == 3); // true
  196. System.out.println(cell.first(2).size() == 2); // true
  197. }
  198. }
  199.  
  200. public interface Main {
  201. public static void main(String... arguments) {
  202. Cell.main(arguments);
  203. }
  204. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:13: error: illegal start of type
  public default <VALUE> VALUE get(Function<TYPE, VALUE> function) {
         ^
Main.java:13: error: = expected
  public default <VALUE> VALUE get(Function<TYPE, VALUE> function) {
                 ^
Main.java:13: error: ';' expected
  public default <VALUE> VALUE get(Function<TYPE, VALUE> function) {
                  ^
Main.java:13: error: illegal start of type
  public default <VALUE> VALUE get(Function<TYPE, VALUE> function) {
                       ^
Main.java:13: error: = expected
  public default <VALUE> VALUE get(Function<TYPE, VALUE> function) {
                               ^
Main.java:13: error: illegal start of type
  public default <VALUE> VALUE get(Function<TYPE, VALUE> function) {
                                  ^
Main.java:13: error: = expected
  public default <VALUE> VALUE get(Function<TYPE, VALUE> function) {
                                           ^
Main.java:13: error: <identifier> expected
  public default <VALUE> VALUE get(Function<TYPE, VALUE> function) {
                                                ^
Main.java:13: error: ';' expected
  public default <VALUE> VALUE get(Function<TYPE, VALUE> function) {
                                                 ^
Main.java:13: error: illegal start of type
  public default <VALUE> VALUE get(Function<TYPE, VALUE> function) {
                                                       ^
Main.java:13: error: = expected
  public default <VALUE> VALUE get(Function<TYPE, VALUE> function) {
                                                                 ^
Main.java:13: error: illegal start of type
  public default <VALUE> VALUE get(Function<TYPE, VALUE> function) {
                                                                   ^
Main.java:13: error: <identifier> expected
  public default <VALUE> VALUE get(Function<TYPE, VALUE> function) {
                                                                    ^
Main.java:14: error: <identifier> expected
    return (VALUE) $get(
                 ^
Main.java:14: error: ';' expected
    return (VALUE) $get(
                  ^
Main.java:15: error: illegal start of type
      (Function<TYPE, Object>) function
                             ^
Main.java:15: error: '(' expected
      (Function<TYPE, Object>) function
                               ^
Main.java:16: error: illegal start of type
    );
    ^
Main.java:16: error: <identifier> expected
    );
     ^
Main.java:17: error: = expected
  }
  ^
Main.java:17: error: ';' expected
  }
   ^
Main.java:19: error: illegal start of type
  public default TYPE from(Selector<TYPE> selector) {
         ^
Main.java:19: error: = expected
  public default TYPE from(Selector<TYPE> selector) {
                      ^
Main.java:19: error: illegal start of type
  public default TYPE from(Selector<TYPE> selector) {
                          ^
Main.java:19: error: = expected
  public default TYPE from(Selector<TYPE> selector) {
                                   ^
Main.java:19: error: <identifier> expected
  public default TYPE from(Selector<TYPE> selector) {
                                        ^
Main.java:19: error: ';' expected
  public default TYPE from(Selector<TYPE> selector) {
                                         ^
Main.java:19: error: illegal start of type
  public default TYPE from(Selector<TYPE> selector) {
                                                  ^
Main.java:19: error: <identifier> expected
  public default TYPE from(Selector<TYPE> selector) {
                                                   ^
Main.java:20: error: = expected
    throw new InternalError("Selector::from is not implemented.");
    ^
Main.java:20: error: ';' expected
    throw new InternalError("Selector::from is not implemented.");
         ^
Main.java:20: error: <identifier> expected
    throw new InternalError("Selector::from is not implemented.");
                           ^
Main.java:20: error: illegal start of type
    throw new InternalError("Selector::from is not implemented.");
                            ^
Main.java:23: error: class, interface, or enum expected
  public default <VALUE> TYPE with(Function<TYPE, ? extends VALUE> function, VALUE value) {
         ^
Main.java:28: error: class, interface, or enum expected
  }
  ^
Main.java:30: error: class, interface, or enum expected
  public static <TYPE> Object none(Function<TYPE, Object> function) {
                ^
Main.java:32: error: class, interface, or enum expected
  }
  ^
Main.java:49: error: illegal start of type
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
         ^
Main.java:49: error: = expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                 ^
Main.java:49: error: ';' expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                  ^
Main.java:49: error: illegal start of type
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                       ^
Main.java:49: error: = expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                               ^
Main.java:49: error: illegal start of type
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                   ^
Main.java:49: error: = expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                              ^
Main.java:49: error: illegal start of type
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                               ^
Main.java:49: error: <identifier> expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                ^
Main.java:49: error: = expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                       ^
Main.java:49: error: = expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                    ^
Main.java:49: error: <identifier> expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                            ^
Main.java:49: error: <identifier> expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                              ^
Main.java:49: error: = expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                               ^
Main.java:49: error: ';' expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                                ^
Main.java:49: error: <identifier> expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                                              ^
Main.java:49: error: ';' expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                                               ^
Main.java:49: error: illegal start of type
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                                                    ^
Main.java:49: error: = expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                                                              ^
Main.java:49: error: illegal start of type
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                                                               ^
Main.java:49: error: <identifier> expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                                                                ^
Main.java:49: error: = expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                                                                         ^
Main.java:49: error: ';' expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                                                                              ^
Main.java:49: error: <identifier> expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                                                                                   ^
Main.java:49: error: ';' expected
  public default <VALUE> VALUE cons(BiFunction<? super ELEMENT, Cell<ELEMENT>, ? extends VALUE> cons, Supplier<? extends VALUE> nil) {
                                                                                                                                    ^
Main.java:50: error: illegal start of type
    return getImplementation()
    ^
Main.java:50: error: ';' expected
    return getImplementation()
                              ^
Main.java:60: error: class, interface, or enum expected
  public static <ELEMENT> Cons<ELEMENT> none() {
                ^
Main.java:62: error: class, interface, or enum expected
  }
  ^
Main.java:68: error: ';' expected
    return Cons::getElement;
               ^
Main.java:68: error: not a statement
    return Cons::getElement;
                 ^
Main.java:72: error: ';' expected
    return Cons::getNext;
               ^
Main.java:72: error: not a statement
    return Cons::getNext;
                 ^
Main.java:76: error: ';' expected
    return Cons::getImplementation;
               ^
Main.java:76: error: not a statement
    return Cons::getImplementation;
                 ^
Main.java:80: error: illegal start of type
  public default ELEMENT getElement() {
         ^
Main.java:80: error: = expected
  public default ELEMENT getElement() {
                 ^
Main.java:80: error: ';' expected
  public default ELEMENT getElement() {
                        ^
Main.java:80: error: illegal start of type
  public default ELEMENT getElement() {
                                   ^
Main.java:80: error: <identifier> expected
  public default ELEMENT getElement() {
                                    ^
Main.java:80: error: = expected
  public default ELEMENT getElement() {
                                      ^
Main.java:80: error: ';' expected
  public default ELEMENT getElement() {
                                       ^
Main.java:81: error: <identifier> expected
    return get(Element());
              ^
Main.java:81: error: <identifier> expected
    return get(Element());
                      ^
Main.java:81: error: ';' expected
    return get(Element());
                       ^
Main.java:81: error: illegal start of type
    return get(Element());
                        ^
Main.java:81: error: <identifier> expected
    return get(Element());
                         ^
Main.java:82: error: = expected
  }
  ^
Main.java:82: error: ';' expected
  }
   ^
Main.java:84: error: <identifier> expected
  @Override
           ^
Main.java:85: error: = expected
  public default Cell<ELEMENT> getNext() {
  ^
Main.java:85: error: ';' expected
  public default Cell<ELEMENT> getNext() {
        ^
Main.java:90: error: illegal start of type
  public default Implementation<ELEMENT> getImplementation() {
         ^
Main.java:90: error: = expected
  public default Implementation<ELEMENT> getImplementation() {
                 ^
Main.java:90: error: ';' expected
  public default Implementation<ELEMENT> getImplementation() {
                               ^
Main.java:90: error: <identifier> expected
  public default Implementation<ELEMENT> getImplementation() {
                                       ^
Main.java:90: error: ';' expected
  public default Implementation<ELEMENT> getImplementation() {
                                        ^
Main.java:90: error: illegal start of type
  public default Implementation<ELEMENT> getImplementation() {
                                                          ^
Main.java:90: error: <identifier> expected
  public default Implementation<ELEMENT> getImplementation() {
                                                           ^
Main.java:90: error: = expected
  public default Implementation<ELEMENT> getImplementation() {
                                                             ^
Main.java:90: error: ';' expected
  public default Implementation<ELEMENT> getImplementation() {
                                                              ^
Main.java:91: error: <identifier> expected
    return get(Implementation());
              ^
Main.java:91: error: <identifier> expected
    return get(Implementation());
                             ^
100 errors
stdout
Standard output is empty