Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Anonymous classes in Java are analogous to lambda-expressions in Scheme. The purpose of this lab is to practice writing such classes. We will use the Intermediate Full Java language level for this lab.

Lambda-expressions in Java

...

Code Block
    int x_minus_y(int x, final int y) {
        return (Integer)new Lambda() {
            Object apply(Object arg) {
                return (Integer)arg - y;}
        }.apply(x);
    }

...

What does this anonymous inner class do?

Code Block

Lambda whatIsIt() {
   return new Lambda() {
        Object apply(Object s1) {
            return new Lambda() {
                Object apply(Object s2) {
                    return new Lambda() {
                        Object apply(Object s3) {
                            return s1 + " " + s2 + " " + s3;
                        }
                    };
                }
            };
        }
    };
}

Actually the above does not compile in the Intermediate language level. In full Java syntax, the code would like the following (and would compile):

Code Block
public class Currying {
    Lambda whatIsIt() {
       return new Lambda() {
            public Object apply(final Object s1) {
                return new Lambda() {
                    public Object apply(final Object s2) {
                        return new Lambda() {
                            public Object apply(final Object s3) {
                                return s1 + " " + s2 + " " + s3;
                            }
                        };
                    }
                };
            }
        };
    }
}

...