use of suite.util.FunUtil.Source in project suite by stupidsing.
the class LempelZivWelchTest method doTest.
private String doTest(String s0) {
byte[] bs = s0.getBytes(Constants.charset);
Source<Byte> source0 = new Source<>() {
private int index;
public Byte source() {
return index < bs.length ? bs[index++] : null;
}
};
LempelZivWelch<Byte> lzw = new LempelZivWelch<>(allBytes());
Source<Integer> source1 = lzw.encode(source0);
Source<Byte> source2 = lzw.decode(source1);
BytesBuilder bb = new BytesBuilder();
Byte b;
while ((b = source2.source()) != null) bb.append(b);
return To.string(bb.toBytes());
}
use of suite.util.FunUtil.Source in project suite by stupidsing.
the class DblFunUtil method suck.
/**
* Sucks data from a sink and produce into a source.
*/
public static DblSource suck(Sink<DblSink> fun) {
NullableSyncQueue<Double> queue = new NullableSyncQueue<>();
DblSink enqueue = c -> enqueue(queue, c);
Thread thread = Thread_.startThread(() -> {
try {
fun.sink(enqueue);
} finally {
enqueue(queue, EMPTYVALUE);
}
});
return () -> {
try {
return queue.take();
} catch (InterruptedException ex) {
thread.interrupt();
return Fail.t(ex);
}
};
}
use of suite.util.FunUtil.Source in project suite by stupidsing.
the class FltFunUtil method suck.
/**
* Sucks data from a sink and produce into a source.
*/
public static FltSource suck(Sink<FltSink> fun) {
NullableSyncQueue<Float> queue = new NullableSyncQueue<>();
FltSink enqueue = c -> enqueue(queue, c);
Thread thread = Thread_.startThread(() -> {
try {
fun.sink(enqueue);
} finally {
enqueue(queue, EMPTYVALUE);
}
});
return () -> {
try {
return queue.take();
} catch (InterruptedException ex) {
thread.interrupt();
return Fail.t(ex);
}
};
}
use of suite.util.FunUtil.Source in project suite by stupidsing.
the class ParseUtil method split.
public static Streamlet<String> split(String in, String name) {
char[] chars = in.toCharArray();
int length = chars.length;
return new Streamlet<>(() -> Outlet.of(new Source<String>() {
private int pos = 0;
public String source() {
if (pos < length) {
Segment segment = searchPosition(chars, Segment.of(pos, length), name, Assoc.LEFT, true);
int pos0 = pos;
int end;
if (segment != null) {
end = segment.start;
pos = segment.end;
} else
end = pos = length;
return new String(chars, pos0, end);
} else
return null;
}
}));
}
use of suite.util.FunUtil.Source in project suite by stupidsing.
the class FunCreatorTest method testExpression.
@Test
public void testExpression() {
Int N1 = Int.of(1);
@SuppressWarnings({ "rawtypes", "unchecked" }) FunCreator<Source<Node>> fc = (FunCreator) FunCreator.of(Source.class);
assertEquals(Suite.parse("1"), fc.create(() -> f.object(N1)).apply(void_).source());
assertEquals(Suite.parse("1 + 1"), fc.create(() -> //
f.invokeStatic(//
Tree.class, //
"of", //
f.object(TermOp.PLUS__), //
f.object(N1).cast_(Node.class), //
f.object(N1).cast_(Node.class))).apply(//
void_).source());
}
Aggregations