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();
}
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();
}
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);
}
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, "");
}
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;
}
Aggregations