Search in sources :

Example 1 with PerMap

use of primal.persistent.PerMap in project suite by stupidsing.

the class InterpretFunLazy0 method inferType.

public Node inferType(Node node) {
    class InferType {

        private PerMap<String, Node> env;

        private InferType(PerMap<String, Node> env) {
            this.env = env;
        }

        private Node infer(Node node) {
            return new // 
            SwitchNode<Node>(// 
            node).match("define .0 := .1 ~ .2", (a, b, c) -> {
                var tv = new Reference();
                var i1 = new InferType(env.put(Atom.name(a), tv));
                bind(infer(b), tv);
                return i1.infer(c);
            }).match("if .0 then .1 else .2", (a, b, c) -> {
                var tr = new Reference();
                bind(Suite.parse("BOOLEAN"), infer(a));
                bind(tr, infer(b));
                bind(tr, infer(c));
                return tr;
            }).match(".0 => .1", (a, b) -> {
                var tp = new Reference();
                var env1 = env.replace(Atom.name(a), tp);
                return Suite.substitute("FUN .0 .1", tp, new InferType(env1).infer(b));
            }).match(".0_{.1}", (a, b) -> {
                var tr = new Reference();
                bind(Suite.substitute("FUN .0 .1", infer(b), tr), infer(a));
                return tr;
            }).applyTree((op, l, r) -> {
                var tr = new Reference();
                var tl = Suite.substitute("FUN .0 FUN .1 .2", infer(l), infer(r), tr);
                bind(tl, env.getOrFail(op.name_()));
                return tr;
            }).applyIf(Atom.class, a -> {
                return env.getOrFail(a.name);
            }).applyIf(Int.class, a -> {
                return Suite.parse("NUMBER");
            }).applyIf(Node.class, a -> {
                return Atom.NIL;
            }).nonNullResult();
        }

        private boolean bind(Node t0, Node t1) {
            return Binder.bind(t0, t1) ? true : fail();
        }
    }
    var env0 = // 
    PerMap.<String, Node>empty().put(Atom.TRUE.name, // 
    Suite.parse("BOOLEAN")).put(Atom.FALSE.name, // 
    Suite.parse("BOOLEAN")).put(BaseOp.AND___.name, // 
    Suite.substitute("FUN .0 FUN .1 CONS .0 .1")).put(ERROR.name, // 
    new Reference()).put(FST__.name, // 
    Suite.substitute("FUN (CONS .0 .1) .0")).put(SND__.name, Suite.substitute("FUN (CONS .0 .1) .1"));
    var env1 = // 
    Read.from2(// 
    TreeUtil.boolOperations).keys().fold(env0, (e, o) -> e.put(o.name_(), Suite.substitute("FUN NUMBER FUN NUMBER BOOLEAN")));
    var env2 = // 
    Read.from2(// 
    TreeUtil.intOperations).keys().fold(env1, (e, o) -> e.put(o.name_(), Suite.substitute("FUN NUMBER FUN NUMBER NUMBER")));
    return new InferType(env2).infer(node);
}
Also used : PerMap(primal.persistent.PerMap) Reference(suite.node.Reference) Suite(suite.Suite) BaseOp(suite.node.io.BaseOp) Fail.fail(primal.statics.Fail.fail) Fun(primal.fp.Funs.Fun) Iterate(primal.fp.Funs.Iterate) Read(primal.MoreVerbs.Read) Node(suite.node.Node) Atom(suite.node.Atom) Mutable(primal.adt.Mutable) Binder(suite.lp.doer.Binder) SwitchNode(suite.node.io.SwitchNode) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) Reference(suite.node.Reference) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode) PerMap(primal.persistent.PerMap) Atom(suite.node.Atom)

Example 2 with PerMap

use of primal.persistent.PerMap in project suite by stupidsing.

the class ServerMain method handler.

private Handler handler() {
    BiPredicate<String, String> authenticate = (username, password) -> // 
    Defaults.secrets().prove(Suite.substitute("auth .0 .1", new Str(username), new Str(password)));
    Fun2<String, String, List<String>> authenticateRoles = (username, password) -> {
        return authenticate.test(username, password) ? List.of("role") : null;
    };
    var sseHeaders = new Header(// 
    PerMap.<String, PerList<String>>empty().put("Cache-Control", // 
    PerList.of("no-cache")).put("Content-Type", PerList.of("text/event-stream")));
    Handler handlerDump = request -> Response.of(Pull.from(// 
    "" + // 
    "<html>" + "<br/>method = " + // 
    request.method + "<br/>server = " + // 
    request.server + "<br/>paths = " + // 
    request.paths + "<br/>attrs = " + // 
    HttpHeaderUtil.getAttrs(request.query) + "<br/>headers = " + // 
    request.headers + "</html>"));
    Handler handlerSse = request -> Response.ofWriter(Http.S200, sseHeaders, write -> {
        new Object() {

            private int i = 8;

            private void dispatch() {
                sleep.sink2(1000l, () -> {
                    if (0 < i--) {
                        var event = "event: number\ndata: { \"i\": " + i + " }\n\n";
                        write.f(Bytes.of(event.getBytes(Utf8.charset)));
                        dispatch();
                    } else
                        write.f(null);
                });
            }
        }.dispatch();
    });
    Handler handlerStatus = request -> {
        var cfg = new TradeCfgImpl();
        var summarize = Summarize.of(cfg);
        var sbs = summarize.summarize(trade -> trade.strategy);
        return Response.of(Pull.from("<pre>" + sbs.log + new TreeMap<>(sbs.pnlByKey) + "</pre>"));
    };
    var hh = new HttpHandle();
    var hhsa = new HttpHandleSessionAuth();
    var hhta = new HttpHandleTokenAuth();
    return hh.routeByPath(// 
    PerMap.<String, Handler>empty().put("api", // 
    hhta.applyFilter("role", hh.serveText("in good shape"))).put("hello", // 
    hh.serveText("hello world")).put("html", // 
    hh.serveDir(Paths.get(FileUtil.suiteDir() + "/src/main/html"))).put("path", // 
    hh.serveDir(Tmp.root)).put("site", // 
    hhsa.getHandler(authenticate, handlerDump)).put("sse", // 
    handlerSse).put("status", // 
    handlerStatus).put("token", hh.routeByMethod(// 
    PerMap.<String, Handler>empty().put("PATCH", // 
    hhta.refreshToken(authenticateRoles)).put("POST", hhta.getToken(authenticateRoles)))));
}
Also used : Schedule(suite.os.Schedule) Sink2(primal.fp.Funs2.Sink2) LocalDateTime(java.time.LocalDateTime) Scheduler(suite.os.Scheduler) RunUtil(suite.util.RunUtil) ArrayList(java.util.ArrayList) Defaults(suite.cfg.Defaults) BiPredicate(java.util.function.BiPredicate) Http(suite.http.Http) Response(suite.http.Http.Response) SmtpServer(suite.smtp.SmtpServer) Utf8(primal.Nouns.Utf8) LocalTime(java.time.LocalTime) FileUtil(suite.os.FileUtil) Pull(primal.MoreVerbs.Pull) HttpServe(suite.http.HttpServe) Sleep(primal.Verbs.Sleep) Handler(suite.http.Http.Handler) TelegramBotMain(suite.sample.TelegramBotMain) PerMap(primal.persistent.PerMap) TradeCfgImpl(suite.trade.data.TradeCfgImpl) Files(java.nio.file.Files) HttpNio(suite.http.HttpNio) HttpHandleSessionAuth(suite.http.HttpHandleSessionAuth) Summarize(suite.trade.analysis.Summarize) HttpHandle(suite.http.HttpHandle) Start(primal.Verbs.Start) HttpHeaderUtil(suite.http.HttpHeaderUtil) Header(suite.http.Http.Header) Bytes(primal.primitive.adt.Bytes) List(java.util.List) TreeMap(java.util.TreeMap) Paths(java.nio.file.Paths) HttpHandleTokenAuth(suite.http.HttpHandleTokenAuth) Fun2(primal.fp.Funs2.Fun2) PerList(primal.persistent.PerList) Str(suite.node.Str) Tmp(primal.Nouns.Tmp) HttpHandle(suite.http.HttpHandle) TradeCfgImpl(suite.trade.data.TradeCfgImpl) Handler(suite.http.Http.Handler) Str(suite.node.Str) HttpHandleSessionAuth(suite.http.HttpHandleSessionAuth) Header(suite.http.Http.Header) ArrayList(java.util.ArrayList) List(java.util.List) PerList(primal.persistent.PerList) HttpHandleTokenAuth(suite.http.HttpHandleTokenAuth)

Aggregations

PerMap (primal.persistent.PerMap)2 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 BiPredicate (java.util.function.BiPredicate)1 Pull (primal.MoreVerbs.Pull)1 Read (primal.MoreVerbs.Read)1 Tmp (primal.Nouns.Tmp)1 Utf8 (primal.Nouns.Utf8)1 Sleep (primal.Verbs.Sleep)1 Start (primal.Verbs.Start)1 Mutable (primal.adt.Mutable)1 Fun (primal.fp.Funs.Fun)1 Iterate (primal.fp.Funs.Iterate)1 Fun2 (primal.fp.Funs2.Fun2)1 Sink2 (primal.fp.Funs2.Sink2)1