Search in sources :

Example 1 with Sink

use of suite.util.FunUtil.Sink in project suite by stupidsing.

the class DevMain method run.

private void run() {
    String input = FileUtil.read("src/main/java/suite/dev/DevMain.java");
    Text inputText = text(IRope.ropeList(input));
    try (Termios termios = new Termios(libc)) {
        termios.clear();
        Keyboard keyboard = new Keyboard(libc);
        Sink<State> redraw = state -> state.apply((st, undo, redo, text, oc, cc) -> cc.apply((cx, cy) -> oc.apply((ox, oy) -> {
            String[] lines = // 
            Ints_.range(// 
            viewSizeY).map(// 
            screenY -> text.get(ox, oy + screenY, viewSizeX).replace('\t', ' ')).toArray(String.class);
            termios.cursor(false);
            for (int screenY = 0; screenY < viewSizeY; screenY++) {
                termios.gotoxy(0, screenY);
                termios.puts(lines[screenY]);
            }
            termios.gotoxy(cx - ox, cy - oy);
            termios.cursor(true);
            return null;
        })));
        State state0 = new State(null, null, inputText, c(0, 0), c(0, 0));
        redraw.sink(state0);
        FixieFun3<VK, Character, State, State> mutate = (vk, ch, state) -> // 
        state.apply((st, undo, redo, text, oc, cc) -> oc.apply((ox, oy) -> cc.apply((cx, cy) -> {
            int ci = text.index(cx, cy);
            if (vk == VK.LEFT_)
                return st.cursor(ci - 1);
            else if (vk == VK.RIGHT)
                return st.cursor(ci + 1);
            else if (vk == VK.UP___)
                return st.cursor(cx, cy - 1);
            else if (vk == VK.DOWN_)
                return st.cursor(cx, cy + 1);
            else if (vk == VK.PGUP_)
                return st.cursor(cx, cy - viewSizeY);
            else if (vk == VK.PGDN_)
                return st.cursor(cx, cy + viewSizeY);
            else if (vk == VK.HOME_)
                return st.cursor(text.startOfLine(ci));
            else if (vk == VK.END__)
                return st.cursor(text.endOfLine(ci));
            else if (vk == VK.CTRL_HOME_)
                return st.cursor(0);
            else if (vk == VK.CTRL_END__)
                return st.cursor(text.length());
            else if (vk == VK.CTRL_LEFT_)
                return st.cursor(text.scanNext(ci, -1, ch_ -> !Character.isJavaIdentifierPart(ch_)));
            else if (vk == VK.CTRL_RIGHT)
                return st.cursor(text.scanNext(ci, 1, ch_ -> !Character.isJavaIdentifierPart(ch_)));
            else if (vk == VK.CTRL_UP___)
                return st.offset(ox, oy - 1).cursor(cx, cy - 1);
            else if (vk == VK.CTRL_DOWN_)
                return st.offset(ox, oy + 1).cursor(cx, cy + 1);
            else if (vk == VK.F7___)
                return st.offset(ox, max(cy - viewSizeY + 1, 0));
            else if (vk == VK.F8___)
                return st.offset(ox, min(cy, text.nLines()));
            else if (vk == VK.ALT_J____) {
                int index = text.endOfLine(ci);
                Text text1 = text.splice(index, index + 1, empty);
                return st.text(text1).cursor(index);
            } else if (vk == VK.BKSP_) {
                int index = ci;
                return 0 < index ? st.splice(index - 1, index, empty) : st;
            } else if (vk == VK.ALT_UP___) {
                int i1 = text.startOfLine(ci);
                if (0 < i1) {
                    int i0 = text.prevLine(i1);
                    int i2 = text.nextLine(i1);
                    return st.splice(i2, i2, text.text.subList(i0, i1)).splice(i0, i1, empty);
                } else
                    return st;
            } else if (vk == VK.ALT_DOWN_) {
                int i0 = text.startOfLine(ci);
                int i1 = text.nextLine(i0);
                if (i1 < text.length()) {
                    int i2 = text.nextLine(i1);
                    return st.splice(i1, i2, empty).splice(i0, i0, text.text.subList(i1, i2));
                } else
                    return st;
            } else if (vk == VK.DEL__)
                return st.splice(1, empty);
            else if (vk == VK.CTRL_K____)
                return st.splice(ci, text.endOfLine(ci), empty);
            else if (vk == VK.CTRL_U____)
                return st.splice(text.startOfLine(ci), ci, empty);
            else if (vk == VK.CTRL_D____)
                return st.splice(text.startOfLine(ci), text.nextLine(ci), empty);
            else if (vk == VK.CTRL_Y____)
                return redo != null ? redo : st;
            else if (vk == VK.CTRL_Z____) {
                State undo1 = undo != null ? undo : st;
                return new State(undo1.undo, st, undo1.text, oc, undo1.cursorCoord);
            } else if (vk == VK.CTRL_C____)
                return Fail.t();
            else if (ch != null)
                if (ch == 13) {
                    int i0 = text.startOfLine(ci);
                    int ix = i0;
                    char ch_;
                    while ((ch_ = text.at(ix)) == ' ' || ch_ == '\t') ix++;
                    return st.splice(0, IRope.ropeList("\n").concat(text.text.subList(i0, ix)));
                } else
                    return st.splice(0, IRope.ropeList(Character.toString(ch)));
            else
                return st;
        }))).apply((st, undo, redo, text, oc, cc) -> oc.apply((ox, oy) -> cc.apply((cx, cy) -> {
            IntIntPair cc_ = text.coord(sat(text.index(cx, cy), 0, text.length()));
            return st.cursor(cc_.t0, cc_.t1);
        }))).apply((st, undo, redo, text, oc, cc) -> oc.apply((ox, oy) -> cc.apply((cx, cy) -> {
            int x0 = Math.max(0, cx - viewSizeX + 1);
            int y0 = Math.max(0, cy - viewSizeY + 1);
            int ox_ = sat(ox, x0, cx);
            int oy_ = sat(oy, y0, cy);
            return st.offset(ox_, oy_);
        })));
        keyboard.loop(signal -> // 
        signal.fold(state0, // 
        (state, pair_) -> pair_.map((vk, ch) -> mutate.apply(vk, ch, state))).wire(redraw));
    }
}
Also used : LibcJna(suite.ansi.LibcJna) Termios(suite.ansi.Termios) Friends.min(suite.util.Friends.min) Native(com.sun.jna.Native) IntIntPair(suite.primitive.adt.pair.IntIntPair) Predicate(java.util.function.Predicate) IRope(suite.immutable.IRope) Friends.max(suite.util.Friends.max) IntSink(suite.primitive.IntPrimitives.IntSink) Keyboard(suite.ansi.Keyboard) IRopeList(suite.immutable.IRope.IRopeList) FixieFun6(suite.adt.pair.Fixie_.FixieFun6) VK(suite.ansi.Keyboard.VK) FixieFun3(suite.adt.pair.Fixie_.FixieFun3) Sink(suite.util.FunUtil.Sink) FileUtil(suite.os.FileUtil) Chars_(suite.primitive.Chars_) IntsBuilder(suite.primitive.Ints.IntsBuilder) Ints_(suite.primitive.Ints_) IntMutable(suite.primitive.IntMutable) Fail(suite.util.Fail) Keyboard(suite.ansi.Keyboard) VK(suite.ansi.Keyboard.VK) Termios(suite.ansi.Termios) IntIntPair(suite.primitive.adt.pair.IntIntPair)

Example 2 with Sink

use of suite.util.FunUtil.Sink in project suite by stupidsing.

the class LazyIbTreeExtentFilePersister method gc.

@Override
public Map<Extent, Extent> gc(List<Extent> roots, int back) {
    synchronized (writeLock) {
        int end = nPages;
        int start = max(0, end - back);
        Set<Extent> isInUse = new HashSet<>();
        Sink<List<Extent>> use = extents_ -> {
            for (Extent extent : extents_) if (start <= extent.start)
                isInUse.add(extent);
        };
        use.sink(roots);
        List<Extent> extents = extentFile.scan(start, end);
        for (Extent extent : Read.from(extents).reverse()) if (isInUse.contains(extent))
            use.sink(Read.from(loadSlot(extent).pairs).map(Pair::second).toList());
        Map<Extent, Extent> map = new HashMap<>();
        if (!extents.isEmpty()) {
            int pointer = extents.get(0).start;
            for (Extent extent0 : extents) if (isInUse.contains(extent0)) {
                PersistSlot<T> ps0 = loadSlot(extent0);
                List<Pair<T, Extent>> pairs0 = ps0.pairs;
                List<Pair<T, Extent>> pairsx = Read.from(pairs0).map(Pair.map1(p -> map.getOrDefault(p, p))).toList();
                PersistSlot<T> psx = new PersistSlot<>(pairsx);
                Extent extentx = saveSlot(pointer, psx);
                pointer = extentx.end;
                map.put(extent0, extentx);
            }
            nPages = pointer;
            slotsByExtent.clear();
        }
        return map;
    }
}
Also used : SerializedFileFactory(suite.file.impl.SerializedFileFactory) PageFile(suite.file.PageFile) Read(suite.streamlet.Read) DataInput_(suite.util.DataInput_) DataOutput_(suite.util.DataOutput_) HashMap(java.util.HashMap) HashSet(java.util.HashSet) ExtentFile(suite.file.ExtentFile) Rethrow(suite.util.Rethrow) Map(java.util.Map) IdentityKey(suite.adt.IdentityKey) SerializedPageFile(suite.file.SerializedPageFile) FileFactory(suite.file.impl.FileFactory) Serializer(suite.util.Serialize.Serializer) Slot(suite.immutable.LazyIbTree.Slot) Set(java.util.Set) Bytes(suite.primitive.Bytes) IOException(java.io.IOException) HashBiMap(suite.adt.map.HashBiMap) To(suite.util.To) Serialize(suite.util.Serialize) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) BiMap(suite.adt.map.BiMap) Extent(suite.file.ExtentAllocator.Extent) Sink(suite.util.FunUtil.Sink) As(suite.streamlet.As) Comparator(java.util.Comparator) Extent(suite.file.ExtentAllocator.Extent) HashMap(java.util.HashMap) List(java.util.List) HashSet(java.util.HashSet) Pair(suite.adt.pair.Pair)

Example 3 with Sink

use of suite.util.FunUtil.Sink in project suite by stupidsing.

the class SewingProverImpl method save.

private Restore save(Runtime rt) {
    Cps cps0 = rt.cps;
    Env env0 = rt.env;
    Node query0 = rt.query;
    IList<Trampoline> cutPoint0 = rt.cutPoint;
    IList<Trampoline> rems0 = rt.rems;
    int pit0 = rt.trail.getPointInTime();
    Sink<Node> handler0 = rt.handler;
    return rt_ -> {
        rt_.cps = cps0;
        rt_.env = env0;
        rt_.query = query0;
        rt_.cutPoint = cutPoint0;
        rt_.rems = rems0;
        rt_.trail.unwind(pit0);
        rt_.handler = handler0;
    };
}
Also used : Prover(suite.lp.doer.Prover) BindEnv(suite.lp.doer.BinderFactory.BindEnv) BuiltinPredicate(suite.lp.predicate.PredicateUtil.BuiltinPredicate) LogUtil(suite.os.LogUtil) Mutable(suite.adt.Mutable) BinderFactory(suite.lp.doer.BinderFactory) Node(suite.node.Node) ProverConstant(suite.lp.doer.ProverConstant) VariableMapper(suite.lp.sewing.VariableMapper) Suspend(suite.node.Suspend) Map(java.util.Map) Prototype(suite.lp.kb.Prototype) RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) Bind_(suite.lp.doer.BinderFactory.Bind_) Cloner(suite.lp.doer.Cloner) Object_(suite.util.Object_) SystemPredicates(suite.lp.predicate.SystemPredicates) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Clone_(suite.lp.doer.ClonerFactory.Clone_) ListMultimap(suite.adt.map.ListMultimap) As(suite.streamlet.As) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) ProverConfig(suite.lp.Configuration.ProverConfig) Read(suite.streamlet.Read) Rule(suite.lp.kb.Rule) HashMap(java.util.HashMap) IList(suite.immutable.IList) ArrayList(java.util.ArrayList) Data(suite.node.Data) Formatter(suite.node.io.Formatter) String_(suite.util.String_) Rethrow(suite.util.Rethrow) Binder(suite.lp.doer.Binder) Tuple(suite.node.Tuple) Reference(suite.node.Reference) Suite(suite.Suite) SuiteException(suite.node.util.SuiteException) Iterator(java.util.Iterator) CompileExpressionImpl(suite.lp.compile.impl.CompileExpressionImpl) Source(suite.util.FunUtil.Source) TermOp(suite.node.io.TermOp) List_(suite.util.List_) Evaluate_(suite.lp.doer.EvaluatorFactory.Evaluate_) Rewrite(suite.node.util.Rewrite) Streamlet(suite.streamlet.Streamlet) Atom(suite.node.Atom) Sink(suite.util.FunUtil.Sink) ProverFactory(suite.lp.doer.ProverFactory) Env(suite.lp.sewing.Env) Fail(suite.util.Fail) Node(suite.node.Node) BindEnv(suite.lp.doer.BinderFactory.BindEnv) Env(suite.lp.sewing.Env)

Example 4 with Sink

use of suite.util.FunUtil.Sink 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 5 with Sink

use of suite.util.FunUtil.Sink 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)

Aggregations

Sink (suite.util.FunUtil.Sink)10 Fail (suite.util.Fail)8 Iterator (java.util.Iterator)7 LogUtil (suite.os.LogUtil)7 Source (suite.util.FunUtil.Source)7 Fun (suite.util.FunUtil.Fun)6 Collections (java.util.Collections)5 Source2 (suite.util.FunUtil2.Source2)5 NullableSyncQueue (suite.util.NullableSyncQueue)5 Thread_ (suite.util.Thread_)5 Map (java.util.Map)4 As (suite.streamlet.As)4 Read (suite.streamlet.Read)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Pair (suite.adt.pair.Pair)3 Friends.max (suite.util.Friends.max)3 ArrayList (java.util.ArrayList)2 Suite (suite.Suite)2 Mutable (suite.adt.Mutable)2