com.github.basking2.sdsai.itrex.functions.RangeFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdsai-itrex Show documentation
Show all versions of sdsai-itrex Show documentation
An S-Expression inspiried library focused on iterators.
package com.github.basking2.sdsai.itrex.functions;
import com.github.basking2.sdsai.itrex.EvaluationContext;
import com.github.basking2.sdsai.itrex.iterators.Iterators;
import com.github.basking2.sdsai.itrex.iterators.RangeIterator;
import java.util.Iterator;
/**
* Build a range function.
*/
public class RangeFunction implements FunctionInterface> {
@Override
public Iterator apply(final Iterator> iterator, final EvaluationContext evaluationContext) {
if (!iterator.hasNext()) {
@SuppressWarnings("unchecked")
final Iterator emptyIterator = (Iterator) Iterators.EMPTY_ITERATOR;
return emptyIterator;
}
final int start = Integer.valueOf(iterator.next().toString());
if (!iterator.hasNext()) {
// When there is no 2nd argument, start is used as the stop value.
// Start is 0 and step is 1.
return new RangeIterator(0, start, 1);
}
final int stop = Integer.valueOf(iterator.next().toString());
if (!iterator.hasNext()) {
return new RangeIterator(start, stop, 1);
}
final int step = Integer.valueOf(iterator.next().toString());
return new RangeIterator(start, stop, step);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy