@Override public int length(ListModule list) { return list.size() == 0 ? 0 : 1 + (new ListLengthCalculator() { @Override public int length(ListModule list) { return list.size() == 0 ? 0 : 1 + Eternity.length(list.cdr()); } }) .length(list.cdr()); }
@Override public int length(ListModule list) { // in this definition we recognize a common structure: we ask // something about the size of the list and then delegate to a // function (constructed dynamically) to do so work. This function // asks the same question about the size of the list and then // delegate to a new function... return list.size() == 0 ? 0 : 1 + (new ListLengthCalculator() { @Override public int length(ListModule list) { // label:* return list.size() == 0 ? 0 : 1 + (new ListLengthCalculator() { @Override public int length(ListModule list) { // label:** // observe that here we fix the finish of // the chain because we hard-code the use of // Eternity. In this way if we want to // extend the chain, we're forced to make a // copy of the same code because this is a // fixed structure (is something like // enumerating all our knowledge) return list.size() == 0 ? 0 : 1 + Eternity.length(list.cdr()); } }) .length(list.cdr()); // this invocation executes // label:** } }) .length(list.cdr()); // this invocation executes label:* }
@Override public int length(ListModule list) { return list.size() == 0 ? 0 : 1 + this.length(list.cdr()); }