range now handles better long long chains, as well as float values.

This commit is contained in:
gnembon committed 2020-12-13 06:29:42 -04:00
1 parent 34ee6784a4
commit 7bacea1810
5 files changed
+71 -17

No files matched your search

@@ -11,7 +11,7 @@ import java.util.Locale;
public abstract class LazyListValue extends AbstractListValue implements Iterator<Value>
{
public static LazyListValue range(long from, long to, long step)
public static LazyListValue rangeDouble(double from, double to, double step)
{
return new LazyListValue()
{
@@ -23,10 +23,10 @@ public abstract class LazyListValue extends AbstractListValue implements Iterato
this.limit = to;
this.stepp = step;
}
private long start;
private long current;
private long limit;
private long stepp;
private final double start;
private double current;
private final double limit;
private final double stepp;
@Override
public Value next()
{
@@ -50,7 +50,50 @@ public abstract class LazyListValue extends AbstractListValue implements Iterato
@Override
public String getString()
{
return String.format(Locale.ROOT, "[%d, %d, ..., %d)",start, start+stepp, limit);
return String.format(Locale.ROOT, "[%s, %s, ..., %s)",NumericValue.of(start).getString(), NumericValue.of(start+stepp).getString(), NumericValue.of( limit).getString());
}
};
}
public static LazyListValue rangeLong(long from, long to, long step)
{
return new LazyListValue()
{
{
if (step == 0)
throw new InternalExpressionException("Range will never end with a zero step");
this.start = from;
this.current = this.start;
this.limit = to;
this.stepp = step;
}
private final long start;
private long current;
private final long limit;
private final long stepp;
@Override
public Value next()
{
Value val = new NumericValue(current);
current += stepp;
return val;
}
@Override
public void reset()
{
current = start;
}
@Override
public boolean hasNext()
{
return stepp > 0?(current < limit):(current > limit);
}
@Override
public String getString()
{
return String.format(Locale.ROOT, "[%s, %s, ..., %s)",NumericValue.of(start).getString(), NumericValue.of(start+stepp).getString(), NumericValue.of( limit).getString());
}
};
}
@@ -308,4 +308,9 @@ public class NumericValue extends Value
if (longValue != null) return new NumericValue(-longValue);
return new NumericValue(-value);
}
public boolean isInteger()
{
return longValue!= null || getDouble() == (double)getLong();
}
}
+6 -4
View File
@@ -13,10 +13,12 @@ import java.util.regex.PatternSyntaxException;
public abstract class Value implements Comparable<Value>, Cloneable
{
public static Value FALSE = new NumericValue(0);
public static Value TRUE = new NumericValue(1);
public static Value ZERO = FALSE;
public static Value NULL = new NullValue();
public static NumericValue FALSE = new NumericValue(0);
public static NumericValue TRUE = new NumericValue(1);
public static NumericValue ZERO = FALSE;
public static NumericValue ONE = TRUE;
public static NullValue NULL = new NullValue();
public String boundVariable;