Skip to content

Commit

Permalink
#162: Cleaned up Nth reimplementation with trampoline by making it ta…
Browse files Browse the repository at this point in the history
…il call recursive.
  • Loading branch information
jvdb committed May 29, 2017
1 parent 80f6292 commit 6410257
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static io.parsingdata.metal.SafeTrampoline.complete;
import static io.parsingdata.metal.SafeTrampoline.intermediate;
import static io.parsingdata.metal.Util.checkNotNull;
import static io.parsingdata.metal.data.transformation.Reversal.reverse;

import java.math.BigInteger;
import java.util.Objects;
Expand Down Expand Up @@ -61,19 +62,19 @@ public Nth(final ValueExpression values, final ValueExpression indices) {

@Override
public ImmutableList<Optional<Value>> eval(final ParseGraph graph, final Encoding encoding) {
return eval(values.eval(graph, encoding), indices.eval(graph, encoding)).computeResult();
return reverse(eval(values.eval(graph, encoding), indices.eval(graph, encoding), new ImmutableList<>()).computeResult());
}

private SafeTrampoline<ImmutableList<Optional<Value>>> eval(final ImmutableList<Optional<Value>> values, final ImmutableList<Optional<Value>> indices) {
if (indices.isEmpty()) { return complete(ImmutableList::new); }
private SafeTrampoline<ImmutableList<Optional<Value>>> eval(final ImmutableList<Optional<Value>> values, final ImmutableList<Optional<Value>> indices, final ImmutableList<Optional<Value>> result) {
if (indices.isEmpty()) { return complete(() -> result); }
if (indices.head.isPresent()) {
final BigInteger index = indices.head.get().asNumeric();
final BigInteger valueCount = BigInteger.valueOf(values.size);
if (index.compareTo(valueCount) < 0 && index.compareTo(ZERO) >= 0) {
return complete(() -> eval(values, indices.tail).computeResult().add(nth(values, valueCount.subtract(index).subtract(ONE)).computeResult()));
return intermediate(() -> eval(values, indices.tail, result.add(nth(values, valueCount.subtract(index).subtract(ONE)).computeResult())));
}
}
return complete(() -> eval(values, indices.tail).computeResult().add(Optional.empty()));
return intermediate(() -> eval(values, indices.tail, result.add(Optional.empty())));
}

private SafeTrampoline<Optional<Value>> nth(final ImmutableList<Optional<Value>> values, final BigInteger index) {
Expand Down

0 comments on commit 6410257

Please sign in to comment.