Search in sources :

Example 1 with Chars

use of suite.primitive.Chars in project suite by stupidsing.

the class MonadIntrinsics method newReader.

private Node newReader(IntrinsicCallback callback, InputStream is) {
    BufferedReader br = new BufferedReader(new InputStreamReader(is, Constants.charset));
    IPointer<Chars> icrp = Intrinsics.read(br);
    return callback.enclose(new CharsIntrinsics().drain, new Data<>(icrp));
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Chars(suite.primitive.Chars)

Example 2 with Chars

use of suite.primitive.Chars in project suite by stupidsing.

the class Formatter method format_.

private void format_(Node node, int parentPrec) {
    new // 
    SwitchNode<Node>(// 
    node).doIf(Atom.class, n -> {
        sb.append(quoteAtomIfRequired(n.name));
    }).doIf(Data.class, n -> {
        Object data = n.data;
        if (data instanceof Chars)
            sb.append("Chars<" + quoteStringIfRequired(data.toString()) + ">");
        else if (data instanceof Node)
            sb.append("Data<" + data.toString() + ">");
        else
            sb.append("Data<" + data.getClass().getSimpleName() + ">");
    }).doIf(Dict.class, n -> {
        sb.append("dict<");
        for (Entry<Node, Reference> e : n.map.entrySet()) {
            format(e.getKey(), TermOp.getLeftPrec(TermOp.AND___));
            sb.append(":");
            format(e.getValue(), TermOp.getLeftPrec(TermOp.AND___));
            sb.append(",");
        }
        sb.append(">");
    }).doIf(Int.class, n -> {
        sb.append(n.number);
    }).doIf(Reference.class, n -> {
        sb.append(n.name());
    }).doIf(Str.class, n -> {
        sb.append(quoteStringIfRequired(n.value));
    }).doIf(Tree.class, n -> {
        formatTree(parentPrec, n);
    }).doIf(Tuple.class, n -> {
        sb.append("tuple<");
        for (Node n_ : n.nodes) {
            format(n_, TermOp.getLeftPrec(TermOp.AND___));
            sb.append(", ");
        }
        sb.append(">");
    }).doIf(Node.class, n -> {
        sb.append(n.getClass().getSimpleName() + '@' + Integer.toHexString(System.identityHashCode(n)));
    }).nonNullResult();
}
Also used : Reference(suite.node.Reference) Set(java.util.Set) Chars(suite.primitive.Chars) Tree(suite.node.Tree) Node(suite.node.Node) HashSet(java.util.HashSet) ProverConstant(suite.lp.doer.ProverConstant) Data(suite.node.Data) CommentPreprocessor(suite.parser.CommentPreprocessor) String_(suite.util.String_) Atom(suite.node.Atom) Entry(java.util.Map.Entry) Int(suite.node.Int) Tuple(suite.node.Tuple) Dict(suite.node.Dict) Str(suite.node.Str) Entry(java.util.Map.Entry) Dict(suite.node.Dict) Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree) Chars(suite.primitive.Chars) Atom(suite.node.Atom)

Example 3 with Chars

use of suite.primitive.Chars in project suite by stupidsing.

the class FactorizeResult method merge.

public static FactorizeResult merge(String name, List<FactorizeResult> list) {
    Chars pre = List_.first(list).pre;
    Chars post = List_.last(list).post;
    List<FPair> pairs = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        Chars space;
        if (i != list.size() - 1)
            space = Chars.of(pre.cs, list.get(i).post.start, list.get(i + 1).pre.end);
        else
            space = To.chars("");
        pairs.add(new FPair(list.get(i).node, space));
    }
    FNode fn = new FTree(name, pairs);
    return new FactorizeResult(pre, fn, post);
}
Also used : ArrayList(java.util.ArrayList) Chars(suite.primitive.Chars)

Example 4 with Chars

use of suite.primitive.Chars in project suite by stupidsing.

the class As method utf8decode.

public static Outlet<Chars> utf8decode(Outlet<Bytes> bytesOutlet) {
    Source<Bytes> source = bytesOutlet.source();
    return Outlet.of(new Source<>() {

        private BytesBuilder bb = new BytesBuilder();

        public Chars source() {
            Chars chars;
            while ((chars = decode()).size() == 0) {
                Bytes bytes = source.source();
                if (bytes != null)
                    bb.append(bytes);
                else if (bb.size() == 0)
                    return null;
                else
                    return Fail.t();
            }
            return chars;
        }

        private Chars decode() {
            Bytes bytes = bb.toBytes();
            CharsBuilder cb = new CharsBuilder();
            int s = 0;
            while (s < bytes.size()) {
                int b0 = Byte.toUnsignedInt(bytes.get(s++));
                int ch, e;
                if (b0 < 0x80) {
                    ch = b0;
                    e = s;
                } else if (b0 < 0xE0) {
                    ch = b0 & 0x1F;
                    e = s + 1;
                } else if (b0 < 0xF0) {
                    ch = b0 & 0x0F;
                    e = s + 2;
                } else if (b0 < 0xF8) {
                    ch = b0 & 0x07;
                    e = s + 3;
                } else if (b0 < 0xFC) {
                    ch = b0 & 0x03;
                    e = s + 4;
                } else if (b0 < 0xFE) {
                    ch = b0 & 0x01;
                    e = s + 5;
                } else
                    throw new RuntimeException();
                if (e <= bytes.size()) {
                    while (s < e) {
                        int b = Byte.toUnsignedInt(bytes.get(s++));
                        if ((b & 0xC0) == 0x80)
                            ch = (ch << 6) + (b & 0x3F);
                        else
                            Fail.t();
                    }
                    cb.append((char) ch);
                } else
                    break;
            }
            bb = new BytesBuilder();
            bb.append(bytes.range(s));
            return cb.toChars();
        }
    });
}
Also used : Bytes(suite.primitive.Bytes) CharsBuilder(suite.primitive.Chars.CharsBuilder) Chars(suite.primitive.Chars) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 5 with Chars

use of suite.primitive.Chars in project suite by stupidsing.

the class BackTestMain method run.

@Override
protected boolean run(String[] args) {
    // BEGIN
    // END
    String arg0 = 0 < args.length ? args[0] : "";
    String arg1 = 1 < args.length ? args[1] : "";
    String arg2 = 2 < args.length ? args[2] : "";
    Streamlet<String> strategyMatches = !arg0.isEmpty() ? Read.from(arg0.split(",")) : null;
    Streamlet<Integer> years = !arg1.isEmpty() ? // 
    Read.from(// 
    arg1.split(",")).concatMap(s -> {
        Pair<String, String> pair = ParseUtil.search(s, "-", Assoc.RIGHT);
        return // 
        pair != null ? // 
        Ints_.range(Integer.valueOf(pair.t0), Integer.valueOf(pair.t1)).map(i -> i) : Read.each(Integer.valueOf(s));
    }) : // 
    Ints_.range(2007, Trade_.thisYear).map(i -> i);
    Fun<Time, Streamlet<Asset>> fun = // 
    !arg2.isEmpty() ? // 
    time -> Read.from(arg2.split(",")).map(cfg::queryCompany).collect(As::streamlet) : cfg::queryCompaniesByMarketCap;
    BackAllocConfigurations bac_ = new BackAllocConfigurations(cfg, fun);
    Streamlet2<String, BackAllocConfiguration> bacByTag = bac_.bacs().bacByName;
    Streamlet2<String, Simulate> simulationByKey = // 
    bacByTag.filterKey(// 
    n -> strategyMatches == null || strategyMatches.isAny(sm -> Wildcard.match(sm, n) != null)).map(// 
    Pair::of).join2(// 
    years.sort(Object_::compare).map(TimeRange::ofYear)).map2((pair, period) -> pair.t0, (pair, period) -> {
        BackAllocConfiguration bac = pair.t1;
        Streamlet<Asset> assets = bac.assetsFun.apply(period.from);
        return runner.backTest(bac.backAllocator, period, assets);
    }).collect(As::streamlet2);
    String content0 = // 
    Read.bytes(// 
    Paths.get("src/main/java/" + getClass().getName().replace('.', '/') + ".java")).collect(// 
    As::utf8decode).map(// 
    Chars::toString).collect(As::joined);
    String content1 = ParseUtil.fit(content0, "// BEGIN", "// END")[1];
    System.out.println(content1);
    System.out.println(runner.conclude(simulationByKey));
    return true;
}
Also used : Read(suite.streamlet.Read) Streamlet2(suite.streamlet.Streamlet2) Object_(suite.util.Object_) Trade_(suite.trade.Trade_) ExecutableProgram(suite.util.RunUtil.ExecutableProgram) Chars(suite.primitive.Chars) Fun(suite.util.FunUtil.Fun) RunUtil(suite.util.RunUtil) Wildcard(suite.parser.Wildcard) ParseUtil(suite.util.ParseUtil) Pair(suite.adt.pair.Pair) ConfigurationImpl(suite.trade.data.ConfigurationImpl) BackAllocConfiguration(suite.trade.backalloc.BackAllocConfiguration) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) BackAllocConfigurations(suite.trade.backalloc.BackAllocConfigurations) Paths(java.nio.file.Paths) Configuration(suite.trade.data.Configuration) As(suite.streamlet.As) Simulate(suite.trade.backalloc.BackAllocTester.Simulate) Ints_(suite.primitive.Ints_) Asset(suite.trade.Asset) Assoc(suite.node.io.Operator.Assoc) TimeRange(suite.trade.TimeRange) Time(suite.trade.Time) Object_(suite.util.Object_) As(suite.streamlet.As) Simulate(suite.trade.backalloc.BackAllocTester.Simulate) BackAllocConfigurations(suite.trade.backalloc.BackAllocConfigurations) Streamlet(suite.streamlet.Streamlet) BackAllocConfiguration(suite.trade.backalloc.BackAllocConfiguration) Pair(suite.adt.pair.Pair)

Aggregations

Chars (suite.primitive.Chars)10 ArrayList (java.util.ArrayList)4 ProverConstant (suite.lp.doer.ProverConstant)2 Atom (suite.node.Atom)2 Dict (suite.node.Dict)2 Node (suite.node.Node)2 Str (suite.node.Str)2 FTerminal (suite.node.parser.FactorizeResult.FTerminal)2 Bytes (suite.primitive.Bytes)2 BytesBuilder (suite.primitive.Bytes.BytesBuilder)2 CharsBuilder (suite.primitive.Chars.CharsBuilder)2 Fun (suite.util.FunUtil.Fun)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Paths (java.nio.file.Paths)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 Suite (suite.Suite)1