@Override
public Sequence eval(Sequence[] args, Sequence contextSequence)
throws XPathException {
Sequence result = new ValueSequence();
if (isCalledAs("map")) {
final FunctionReference ref = (FunctionReference) args[0].itemAt(0);
ref.analyze(cachedContextInfo);
for (final SequenceIterator i = args[1].iterate(); i.hasNext(); ) {
final Item item = i.nextItem();
final Sequence r = ref.evalFunction(contextSequence, null, new Sequence[]{item.toSequence()});
result.addAll(r);
}
} else if (isCalledAs("for-each")) {
final FunctionReference ref = (FunctionReference) args[1].itemAt(0);
ref.analyze(cachedContextInfo);
for (final SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
final Item item = i.nextItem();
final Sequence r = ref.evalFunction(contextSequence, null, new Sequence[]{item.toSequence()});
result.addAll(r);
}
} else if (isCalledAs("filter")) {
FunctionReference ref;
Sequence seq;
// Hack: switch parameters for backwards compatibility
if (Type.subTypeOf(args[1].getItemType(), Type.FUNCTION_REFERENCE)) {
ref = (FunctionReference) args[1].itemAt(0);
seq = args[0];
} else {
ref = (FunctionReference) args[0].itemAt(0);
seq = args[1];
}
ref.analyze(cachedContextInfo);
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
final Item item = i.nextItem();
final Sequence r = ref.evalFunction(contextSequence, null, new Sequence[] { item.toSequence() });
if (r.effectiveBooleanValue())
{result.add(item);}
}
} else if (isCalledAs("fold-left")) {
final FunctionReference ref = (FunctionReference) args[2].itemAt(0);
ref.analyze(cachedContextInfo);
Sequence zero = args[1];
Sequence input = args[0];
while (!input.isEmpty()) {
final SequenceIterator i = input.iterate();
zero = ref.evalFunction(contextSequence, null, new Sequence[] { zero, i.nextItem().toSequence() });
ValueSequence tail = new ValueSequence();
while (i.hasNext()) {
tail.add(i.nextItem());
}
input = tail;
}
result = zero;
} else if (isCalledAs("fold-right")) {
final FunctionReference ref = (FunctionReference) args[2].itemAt(0);
ref.analyze(cachedContextInfo);
final Sequence zero = args[1];
final Sequence input = args[0];
result = foldRight(ref, zero, input, contextSequence);
} else if (isCalledAs("map-pairs")) {
final FunctionReference ref = (FunctionReference) args[0];
ref.analyze(cachedContextInfo);
final SequenceIterator i1 = args[1].iterate();
final SequenceIterator i2 = args[2].iterate();
while (i1.hasNext() && i2.hasNext()) {
final Sequence r = ref.evalFunction(contextSequence, null,
new Sequence[] { i1.nextItem().toSequence(), i2.nextItem().toSequence() });
result.addAll(r);
}
} else if (isCalledAs("for-each-pair")) {
final FunctionReference ref = (FunctionReference) args[2].itemAt(0);
ref.analyze(cachedContextInfo);
final SequenceIterator i1 = args[0].iterate();
final SequenceIterator i2 = args[1].iterate();
while (i1.hasNext() && i2.hasNext()) {
final Sequence r = ref.evalFunction(contextSequence, null,
new Sequence[] { i1.nextItem().toSequence(), i2.nextItem().toSequence() });
result.addAll(r);
}
}
return result;