Search in sources :

Example 1 with Pair

use of primal.adt.Pair in project suite by stupidsing.

the class CheckType method check.

public void check(List<Rule> rules) {
    var nElementsByPrototype = clu.getNumberOfElements(rules);
    var types = new HashMap<Pair<Prototype, Integer>, Reference>();
    Read.from(rules).concatMap(rule -> {
        var generalizer = new Generalizer();
        var head = generalizer.generalize(rule.head);
        var tail = generalizer.generalize(rule.tail);
        return clu.scan(tail).cons(head);
    }).forEach(pred -> {
        var prototype = Prototype.of(pred);
        var nElements = prototype != null ? nElementsByPrototype.get(prototype) : null;
        var ps = nElements != null ? TreeUtil.elements(pred, nElements) : new Node[0];
        try {
            if (nElements != null)
                for (var i = 1; i < nElements; i++) {
                    var key = Pair.of(prototype, i);
                    var type0 = types.computeIfAbsent(key, k -> new Reference());
                    var type1 = getType(ps[i]);
                    bind(type0, type1);
                }
        } catch (Exception ex) {
            fail("in predicate " + prototype, ex);
        }
    });
    trail.unwindAll();
}
Also used : Pair(primal.adt.Pair) Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) IdentityKey(primal.adt.IdentityKey) BaseOp(suite.node.io.BaseOp) Fail.fail(primal.statics.Fail.fail) Rule(suite.lp.kb.Rule) TermOp(suite.node.io.TermOp) HashMap(java.util.HashMap) Read(primal.MoreVerbs.Read) Node(suite.node.Node) List(java.util.List) Atom(suite.node.Atom) Map(java.util.Map) Prototype(suite.lp.kb.Prototype) Binder(suite.lp.doer.Binder) SwitchNode(suite.node.io.SwitchNode) TreeUtil(suite.node.util.TreeUtil) Generalizer(suite.lp.doer.Generalizer) TreeTuple(suite.node.tree.TreeTuple) Dict(suite.node.Dict) Prototype(suite.lp.kb.Prototype) Generalizer(suite.lp.doer.Generalizer) HashMap(java.util.HashMap) Reference(suite.node.Reference)

Example 2 with Pair

use of primal.adt.Pair in project suite by stupidsing.

the class ScrapeHtml method parse.

public HtmlNode parse(String in) {
    var pairs = new ArrayList<IntRange>();
    int pos0, posx = 0;
    nextTag: while (0 <= (pos0 = in.indexOf("<", posx))) if ((posx = pos0 + 1) < in.length() && !Is.whitespace(in.charAt(posx)))
        if (0 <= (posx = in.indexOf(">", posx))) {
            pairs.add(IntRange.of(pos0, ++posx));
            if (in.startsWith("<![CDATA[", pos0)) {
                posx = in.indexOf("]]>", pos0 + 9);
                continue nextTag;
            }
            for (var rawTextTag : List.of("script", "style", "textarea", "title")) if (in.startsWith(rawTextTag, pos0 + 1)) {
                posx = in.indexOf("</" + rawTextTag, posx);
                continue nextTag;
            }
        } else
            break;
    Fun<String, IntObjPair<String>> getNameFun = tag -> {
        int p1 = 1, px = tag.length() - 1;
        var first = tag.charAt(p1);
        var last = tag.charAt(px - 1);
        int d;
        if (first == '!')
            return IntObjPair.of(0, null);
        else {
            if (first == '/') {
                p1++;
                d = -1;
            } else if (last == '/') {
                px--;
                d = 0;
            } else
                d = 1;
            var ps = 0;
            while (ps < px && !Is.whitespace(tag.charAt(ps))) ps++;
            var name = tag.substring(p1, ps);
            return IntObjPair.of(d, name);
        }
    };
    var deque = new ArrayDeque<>(List.of(new HtmlNode(null, "", 0, 0)));
    IntIntSink addTextFun = (prevp, p0) -> {
        if (prevp != p0) {
            var s = htmlUtil.decode(in.substring(prevp, p0)).trim();
            if (!s.isEmpty())
                deque.element().children.add(new HtmlNode(null, s, prevp, p0));
        }
    };
    var prevp = 0;
    for (var pair : pairs) {
        var htmlNode = deque.element();
        var p0 = pair.s;
        var px = pair.e;
        addTextFun.sink2(prevp, p0);
        var tag = in.substring(p0, px);
        prevp = getNameFun.apply(tag).map((d, name) -> {
            if (d == -1) {
                // closing tag
                HtmlNode hn;
                while (!deque.isEmpty()) if (Equals.string(getNameFun.apply((hn = deque.pop()).tag).v, name)) {
                    hn.p2 = p0;
                    hn.px = px;
                    break;
                }
            } else {
                // opening tag
                var htmlNode1 = new HtmlNode(name, tag, p0, px);
                htmlNode.children.add(htmlNode1);
                if (d == 1)
                    deque.push(htmlNode1);
            }
            return px;
        });
    }
    addTextFun.sink2(prevp, in.length());
    return deque.pop();
}
Also used : Pair(primal.adt.Pair) PerMap(primal.persistent.PerMap) IntRange(primal.primitive.adt.IntRange) Build(primal.Verbs.Build) IntIntSink(primal.primitive.IntIntSink) Predicate(java.util.function.Predicate) Fun(primal.fp.Funs.Fun) Set(java.util.Set) Assoc(primal.parser.Operator.Assoc) Read(primal.MoreVerbs.Read) Is(primal.Verbs.Is) ArrayList(java.util.ArrayList) Streamlet(primal.streamlet.Streamlet) List(java.util.List) Split(primal.MoreVerbs.Split) Substring(primal.Verbs.Substring) ArrayDeque(java.util.ArrayDeque) Equals(primal.Verbs.Equals) IntObjPair(primal.primitive.adt.pair.IntObjPair) IntIntSink(primal.primitive.IntIntSink) IntObjPair(primal.primitive.adt.pair.IntObjPair) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque)

Example 3 with Pair

use of primal.adt.Pair in project suite by stupidsing.

the class Dump method toDetails.

private static void toDetails(String prefix, Object object, Sink<String> sink) {
    var dumpedObjects = new IdentityHashMap<Object, Object>();
    new Object() {

        private void d(String prefix, Object object) {
            d(prefix, object, object != null ? object.getClass() : void.class);
        }

        private void d(String prefix, Object object, Class<?> clazz) {
            sink.f(prefix);
            sink.f(" =");
            if (object == null)
                sink.f(" null\n");
            else if (dumpedObjects.put(object, true) == null)
                try {
                    d_(prefix, object, clazz);
                } finally {
                    dumpedObjects.remove(object);
                }
            else
                sink.f(" <<recursed>>");
        }

        private void d_(String prefix, Object object, Class<?> clazz) {
            if (clazz.isArray() && Util.isSimple(clazz.getComponentType())) {
                sink.f(" [ ");
                for (var i = 0; i < Array.getLength(object); i++) sink.f(Array.get(object, i) + ", ");
                sink.f("]\n");
            } else if (clazz == String.class)
                sink.f(" \"" + object + "\"");
            else if (Util.isSimple(clazz))
                sink.f(" " + object + " [" + clazz.getSimpleName() + "]\n");
            else
                d_c(prefix, object, clazz);
        }

        private void d_c(String prefix, Object object, Class<?> clazz) {
            if (!Collection.class.isAssignableFrom(clazz))
                sink.f(" " + object);
            sink.f(" [" + clazz.getSimpleName() + "]\n");
            var count = 0;
            // simple listings for simple classes
            if (clazz.isArray())
                if (Util.isSimple(clazz.getComponentType()))
                    d(prefix, Build.string(sb -> {
                        sb.append("[");
                        for (var i = 0; i < Array.getLength(object); i++) sb.append(Array.get(object, i) + ", ");
                        sb.append("]");
                    }));
                else
                    for (var i = 0; i < Array.getLength(object); i++) d(prefix + "[" + count++ + "]", Array.get(object, i));
            else if (Collection.class.isAssignableFrom(clazz))
                for (var o1 : (Collection<?>) object) d(prefix + "[" + count++ + "]", o1);
            else if (Map.class.isAssignableFrom(clazz))
                for (var e : ((Map<?, ?>) object).entrySet()) {
                    Object key = e.getKey(), value = e.getValue();
                    d(prefix + "[" + count + "].getKey()", key);
                    d(prefix + "[" + count + "].getValue()", value);
                    count++;
                }
            else if (MapObject.class.isAssignableFrom(clazz)) {
                var i = 0;
                for (var object1 : MapObject_.list((MapObject<?>) object)) d(prefix + "." + i++, object1);
            } else if (Pair.class.isAssignableFrom(clazz)) {
                var pair = (Pair<?, ?>) object;
                d(prefix + ".fst", pair.k);
                d(prefix + ".snd", pair.v);
            } else
                for (var pair : readers(object)) {
                    var k = prefix + "." + pair.k;
                    try {
                        d(k, pair.v.call());
                    } catch (Throwable ex) {
                        sink.f(k + "()");
                        sink.f(" caught " + ex + "\n");
                    }
                }
        }
    }.d(prefix, object);
}
Also used : IdentityHashMap(java.util.IdentityHashMap) MapObject(suite.object.MapObject) IdentityHashMap(java.util.IdentityHashMap) Map(java.util.Map) Pair(primal.adt.Pair)

Example 4 with Pair

use of primal.adt.Pair in project suite by stupidsing.

the class Dump method toLine.

private static void toLine(Object node, Sink<String> sink) {
    var dumpedObjects = new IdentityHashMap<Object, Object>();
    new Object() {

        private void d(Object object, String suffix) {
            if (object == null)
                sink.f("null");
            else if (dumpedObjects.put(object, true) == null)
                try {
                    d_(object);
                } finally {
                    dumpedObjects.remove(object);
                }
            else
                sink.f("(recursed)");
            sink.f(suffix);
        }

        private void d_(Object object) {
            var clazz = object.getClass();
            if (clazz.isArray()) {
                sink.f("[");
                for (var i = 0; i < Array.getLength(object); i++) d(Array.get(object, i), ",");
                sink.f("]");
            } else if (Util.isSimple(clazz))
                sink.f(object.toString());
            else
                new // 
                Switch<Object>(// 
                object).doIf(Collection.class, collection -> {
                    sink.f("[");
                    for (var object1 : collection) d(object1, ",");
                    sink.f("]");
                }).doIf(Map.class, map -> {
                    sink.f("{");
                    for (var e : ((Map<?, ?>) object).entrySet()) {
                        d(e.getKey(), ":");
                        d(e.getValue(), ",");
                    }
                    sink.f("}");
                }).doIf(MapObject.class, mi -> {
                    sink.f(mi.getClass().getSimpleName());
                    sink.f("{");
                    for (var object1 : MapObject_.list(mi)) d(object1, ",");
                    sink.f("}");
                }).doIf(Pair.class, pair -> {
                    sink.f("<");
                    d(pair.k, "|");
                    d(pair.v, ">");
                }).doIf(Object.class, o -> {
                    sink.f(o.getClass().getSimpleName());
                    sink.f("{");
                    for (var pair : readers(object)) {
                        Object value;
                        try {
                            value = pair.v.call();
                        } catch (Throwable ex) {
                            value = "<" + ex.getClass() + ">";
                        }
                        if (value != null) {
                            sink.f(pair.k + ":");
                            d(value, ",");
                        }
                    }
                    sink.f("}");
                }).nonNullResult();
        }
    }.d(node, "");
}
Also used : Pair(primal.adt.Pair) Array(java.lang.reflect.Array) IdentityHashMap(java.util.IdentityHashMap) Singleton(suite.node.util.Singleton) Build(primal.Verbs.Build) Collection(java.util.Collection) Util(suite.util.Util) Callable(java.util.concurrent.Callable) MapObject_(suite.object.MapObject_) Log_(primal.os.Log_) Assoc(primal.parser.Operator.Assoc) Sink(primal.fp.Funs.Sink) Read(primal.MoreVerbs.Read) Tree(suite.node.Tree) Streamlet(primal.streamlet.Streamlet) SmartSplit(suite.util.SmartSplit) List(java.util.List) MapObject(suite.object.MapObject) Map(java.util.Map) Substring(primal.Verbs.Substring) Get(primal.Verbs.Get) Switch(suite.util.Switch) Left(primal.Verbs.Left) IdentityHashMap(java.util.IdentityHashMap) MapObject(suite.object.MapObject) IdentityHashMap(java.util.IdentityHashMap) Map(java.util.Map) Pair(primal.adt.Pair)

Example 5 with Pair

use of primal.adt.Pair in project suite by stupidsing.

the class Trade_ method dividend.

public static float dividend(Streamlet<Trade> trades, Fun<String, LngFltPair[]> fun, Dbl_Dbl feeFun) {
    var sum = 0f;
    for (var pair : Read.fromMultimap(trades.toMultimap(trade -> trade.symbol))) {
        var dividends = fun.apply(pair.k);
        var puller = Puller.of(pair.v);
        LngIntPair tn = LngIntPair.of(0l, 0);
        Source<LngIntPair> tradeSource = () -> {
            var trade = puller.pull();
            var t = trade != null ? Time.of(trade.date + " 12:00:00").epochSec(8) : Long.MAX_VALUE;
            return LngIntPair.of(t, tn.t1 + (trade != null ? trade.buySell : 0));
        };
        var tn1 = tradeSource.g();
        for (var dividend : dividends) {
            while (tn1 != null && tn1.t0 < dividend.t0) {
                tn.update(tn1.t0, tn1.t1);
                tn1 = tradeSource.g();
            }
            var amount = tn.t1 * dividend.t1;
            sum += amount - feeFun.apply(amount);
        }
    }
    return sum;
}
Also used : Union(primal.Verbs.Union) Math.log1p(java.lang.Math.log1p) Fun(primal.fp.Funs.Fun) Math_(suite.math.Math_) HashMap(java.util.HashMap) LngFltPair(primal.primitive.adt.pair.LngFltPair) Math.abs(java.lang.Math.abs) Obj_Flt(primal.primitive.FltPrim.Obj_Flt) Dbl_Dbl(primal.primitive.Dbl_Dbl) Streamlet_.forInt(suite.util.Streamlet_.forInt) ArrayList(java.util.ArrayList) Streamlet(primal.streamlet.Streamlet) Map(java.util.Map) Math.expm1(java.lang.Math.expm1) Valuation(suite.trade.Account.Valuation) Pair(primal.adt.Pair) IntFltPair(primal.primitive.adt.pair.IntFltPair) IntIntSink(primal.primitive.IntIntSink) Source(primal.fp.Funs.Source) Set(java.util.Set) Math.min(java.lang.Math.min) Read(primal.MoreVerbs.Read) LngIntPair(primal.primitive.adt.pair.LngIntPair) List(java.util.List) Eod(suite.trade.data.DataSource.Eod) AsInt(primal.primitive.fp.AsInt) Math.max(java.lang.Math.max) Math.floor(java.lang.Math.floor) Equals(primal.Verbs.Equals) Puller(primal.puller.Puller) LngIntPair(primal.primitive.adt.pair.LngIntPair)

Aggregations

Pair (primal.adt.Pair)8 Read (primal.MoreVerbs.Read)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)4 HashMap (java.util.HashMap)3 Build (primal.Verbs.Build)3 Equals (primal.Verbs.Equals)3 Fun (primal.fp.Funs.Fun)3 Fail.fail (primal.statics.Fail.fail)3 Streamlet (primal.streamlet.Streamlet)3 Suite (suite.Suite)3 Atom (suite.node.Atom)3 Node (suite.node.Node)3 Tree (suite.node.Tree)3 ArrayDeque (java.util.ArrayDeque)2 IdentityHashMap (java.util.IdentityHashMap)2 Set (java.util.Set)2 Split (primal.MoreVerbs.Split)2 Is (primal.Verbs.Is)2