Search in sources :

Example 6 with Source

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());
}
Also used : Source(suite.util.FunUtil.Source) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 7 with Source

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);
        }
    };
}
Also used : DblSink(suite.primitive.DblPrimitives.DblSink) DblSource(suite.primitive.DblPrimitives.DblSource) Dbl_Obj(suite.primitive.DblPrimitives.Dbl_Obj) Iterator(java.util.Iterator) LogUtil(suite.os.LogUtil) Source2(suite.util.FunUtil2.Source2) Source(suite.util.FunUtil.Source) NullableSyncQueue(suite.util.NullableSyncQueue) Thread_(suite.util.Thread_) Fun(suite.util.FunUtil.Fun) DblObjSource(suite.primitive.DblPrimitives.DblObjSource) Sink(suite.util.FunUtil.Sink) Collections(java.util.Collections) Fail(suite.util.Fail) DblTest(suite.primitive.DblPrimitives.DblTest) DblObjPair(suite.primitive.adt.pair.DblObjPair) NullableSyncQueue(suite.util.NullableSyncQueue) DblSink(suite.primitive.DblPrimitives.DblSink)

Example 8 with Source

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);
        }
    };
}
Also used : Iterator(java.util.Iterator) LogUtil(suite.os.LogUtil) Source2(suite.util.FunUtil2.Source2) FltSink(suite.primitive.FltPrimitives.FltSink) Source(suite.util.FunUtil.Source) NullableSyncQueue(suite.util.NullableSyncQueue) Thread_(suite.util.Thread_) Fun(suite.util.FunUtil.Fun) Flt_Obj(suite.primitive.FltPrimitives.Flt_Obj) FltObjPair(suite.primitive.adt.pair.FltObjPair) FltObjSource(suite.primitive.FltPrimitives.FltObjSource) Sink(suite.util.FunUtil.Sink) FltTest(suite.primitive.FltPrimitives.FltTest) Collections(java.util.Collections) FltSource(suite.primitive.FltPrimitives.FltSource) Fail(suite.util.Fail) NullableSyncQueue(suite.util.NullableSyncQueue) FltSink(suite.primitive.FltPrimitives.FltSink)

Example 9 with Source

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;
        }
    }));
}
Also used : Streamlet(suite.streamlet.Streamlet) Source(suite.util.FunUtil.Source) Segment(suite.text.Segment)

Example 10 with Source

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());
}
Also used : Node(suite.node.Node) Int(suite.node.Int) Int_Int(suite.primitive.Int_Int) Source(suite.util.FunUtil.Source) IntSource(suite.primitive.IntPrimitives.IntSource) Test(org.junit.Test)

Aggregations

Source (suite.util.FunUtil.Source)19 Node (suite.node.Node)9 Fun (suite.util.FunUtil.Fun)8 Fail (suite.util.Fail)7 Iterator (java.util.Iterator)6 LogUtil (suite.os.LogUtil)6 Sink (suite.util.FunUtil.Sink)6 Thread_ (suite.util.Thread_)6 Collections (java.util.Collections)5 List (java.util.List)5 Data (suite.node.Data)5 Tree (suite.node.Tree)5 Source2 (suite.util.FunUtil2.Source2)5 NullableSyncQueue (suite.util.NullableSyncQueue)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 RuleSet (suite.lp.kb.RuleSet)4 Map (java.util.Map)3 Test (org.junit.Test)3 Pair (suite.adt.pair.Pair)3