use of suite.streamlet.Outlet in project suite by stupidsing.
the class TopDownParse method build.
private Parser build(Grammar eg) {
Parser parser, g;
List<Parser> parsers;
switch(eg.type) {
case AND___:
parsers = buildChildren(eg);
parser = (parse, st) -> {
Outlet<State> o = Outlet.of(st);
for (Parser g_ : parsers) o = o.concatMap(st_ -> st_.pr(parse, g_));
return o;
};
break;
case ENTITY:
parser = buildEntity(eg.content);
break;
case EXCEPT:
Parser parser0 = build(eg.children.get(0));
Parser parser1 = build(eg.children.get(1));
parser = (parse, st) -> st.p(parse, parser0).filter(st1 -> {
String in1 = parse.in.substring(st.pos, st1.pos);
return new State(null, 0, null, 0).p(new Parse(in1), parser1).count() == 0;
});
break;
case NAMED_:
parser = deepen(build(eg.children.get(0)), eg.content);
break;
case ONCE__:
g = build(eg.children.get(0));
parser = (parse, st) -> Outlet.of(st.pr(parse, g).take(1));
break;
case OPTION:
g = build(eg.children.get(0));
parser = (parse, st) -> st.pr(parse, g).cons(st);
break;
case OR____:
parsers = buildChildren(eg);
parser = (parse, st) -> Outlet.of(parsers).concatMap(g_ -> st.pr(parse, g_));
break;
case REPT0_:
parser = buildRepeat(eg, true);
break;
case REPT0H:
parser = buildRepeatHeadRecursion(eg);
break;
case REPT1_:
parser = buildRepeat(eg, false);
break;
case STRING:
ExpectFun e = expect.string(eg.content);
parser = skipWhitespaces((parse, st) -> parse.expect(st, e, st.pos));
break;
default:
parser = null;
}
return parser;
}
use of suite.streamlet.Outlet in project suite by stupidsing.
the class Trade_ method collectBrokeredTrades.
public static Streamlet<Trade> collectBrokeredTrades(Outlet<Trade> outlet) {
Trade[] trades0 = outlet.toArray(Trade.class);
List<Trade> trades1 = new ArrayList<>();
int length0 = trades0.length;
int i0 = 0;
IntIntSink tx = (i0_, i1_) -> {
if (Ints_.range(i0_, i1_).mapInt(i -> trades0[i].buySell).sum() != 0)
while (i0_ < i1_) {
Trade trade0 = trades0[i0_++];
if (!String_.equals(trade0.remark, "#"))
trades1.add(trade0);
}
};
for (int i = 1; i < length0; i++) {
Trade trade0 = trades0[i0];
Trade trade1 = trades0[i];
boolean isGroup = //
true && //
String_.equals(trade0.date, trade1.date) && //
String_.equals(trade0.symbol, trade1.symbol) && trade0.price == trade1.price;
if (!isGroup) {
tx.sink2(i0, i);
i0 = i;
}
}
tx.sink2(i0, length0);
return Read.from(trades1);
}
use of suite.streamlet.Outlet in project suite by stupidsing.
the class Trade_ method dividend.
public static float dividend(Streamlet<Trade> trades, Fun<String, LngFltPair[]> fun, Dbl_Dbl feeFun) {
float sum = 0f;
for (Pair<String, List<Trade>> pair : trades.toMultimap(trade -> trade.symbol).listEntries()) {
LngFltPair[] dividends = fun.apply(pair.t0);
Outlet<Trade> outlet = Outlet.of(pair.t1);
LngIntPair tn = LngIntPair.of(0l, 0);
Source<LngIntPair> tradeSource = () -> {
Trade trade = outlet.next();
long 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));
};
LngIntPair tn1 = tradeSource.source();
for (LngFltPair dividend : dividends) {
while (tn1 != null && tn1.t0 < dividend.t0) {
tn.update(tn1.t0, tn1.t1);
tn1 = tradeSource.source();
}
float amount = tn.t1 * dividend.t1;
sum += amount - feeFun.apply(amount);
}
}
return sum;
}
use of suite.streamlet.Outlet in project suite by stupidsing.
the class Trade_ method collectAcquiredPrices.
public static Map<String, Float> collectAcquiredPrices(Outlet<Trade> outlet) {
Map<String, List<IntFltPair>> acquireBySymbol = new HashMap<>();
for (Trade trade : outlet) {
String symbol = trade.symbol;
int buySell = trade.buySell;
float price = trade.price;
List<IntFltPair> acquires0 = acquireBySymbol.getOrDefault(symbol, List.of());
List<IntFltPair> acquires1 = new ArrayList<>();
for (IntFltPair acquire : acquires0) {
int n0 = acquire.t0;
int diff = buySell < 0 ? max(0, min(-buySell, n0)) : min(0, max(-buySell, n0));
int n1 = n0 - diff;
buySell += diff;
if (n1 != 0)
acquires1.add(IntFltPair.of(n1, acquire.t1));
}
if (buySell != 0)
acquires1.add(IntFltPair.of(buySell, price));
acquireBySymbol.put(symbol, acquires1);
}
return //
Read.from2(//
acquireBySymbol).mapValue(acquires -> {
IntFltPair sum = IntFltPair.of(0, 0f);
for (IntFltPair acquire : acquires) sum.update(sum.t0 + acquire.t0, sum.t1 + acquire.t0 * acquire.t1);
return sum.t1 / sum.t0;
}).toMap();
}
Aggregations