Search in sources :

Example 16 with Fun

use of suite.util.FunUtil.Fun in project suite by stupidsing.

the class FunCreatorTest method testIndex.

@Test
public void testIndex() {
    int[] ints = { 0, 1, 4, 9, 16 };
    Int_Int fun = LambdaInstance.of(Int_Int.class, i -> f.object(ints).index(i)).newFun();
    assertEquals(9, fun.apply(3));
    assertEquals(16, fun.apply(4));
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) Flt_Flt(suite.primitive.Flt_Flt) Dump(suite.inspect.Dump) PrintlnFunExpr(suite.jdk.gen.FunExprM.PrintlnFunExpr) Source(suite.util.FunUtil.Source) Assert.assertTrue(org.junit.Assert.assertTrue) TermOp(suite.node.io.TermOp) Test(org.junit.Test) Fun(suite.util.FunUtil.Fun) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) Tree(suite.node.Tree) Node(suite.node.Node) IntSource(suite.primitive.IntPrimitives.IntSource) Iterate(suite.util.FunUtil.Iterate) BiPredicate(java.util.function.BiPredicate) LambdaInstance(suite.jdk.lambda.LambdaInstance) Map(java.util.Map) Type(org.apache.bcel.generic.Type) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Int(suite.node.Int) LambdaInterface(suite.jdk.lambda.LambdaInterface) Int_Int(suite.primitive.Int_Int) Assert.assertEquals(org.junit.Assert.assertEquals) Int_Int(suite.primitive.Int_Int) Test(org.junit.Test)

Example 17 with Fun

use of suite.util.FunUtil.Fun in project suite by stupidsing.

the class FunCreatorTest method testProfile.

@Test
public void testProfile() {
    Iterate<FunExpr> fun = i -> (ProfileFunExpr) f.profile(f.int_(1));
    IntSource instance = LambdaInstance.of(IntSource.class, fun).newFun();
    assertEquals(1, instance.source());
    Dump.out(instance);
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) Flt_Flt(suite.primitive.Flt_Flt) Dump(suite.inspect.Dump) PrintlnFunExpr(suite.jdk.gen.FunExprM.PrintlnFunExpr) Source(suite.util.FunUtil.Source) Assert.assertTrue(org.junit.Assert.assertTrue) TermOp(suite.node.io.TermOp) Test(org.junit.Test) Fun(suite.util.FunUtil.Fun) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) Tree(suite.node.Tree) Node(suite.node.Node) IntSource(suite.primitive.IntPrimitives.IntSource) Iterate(suite.util.FunUtil.Iterate) BiPredicate(java.util.function.BiPredicate) LambdaInstance(suite.jdk.lambda.LambdaInstance) Map(java.util.Map) Type(org.apache.bcel.generic.Type) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Int(suite.node.Int) LambdaInterface(suite.jdk.lambda.LambdaInterface) Int_Int(suite.primitive.Int_Int) Assert.assertEquals(org.junit.Assert.assertEquals) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) IntSource(suite.primitive.IntPrimitives.IntSource) PrintlnFunExpr(suite.jdk.gen.FunExprM.PrintlnFunExpr) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Test(org.junit.Test)

Example 18 with Fun

use of suite.util.FunUtil.Fun in project suite by stupidsing.

the class Intercept method object.

public static <I> I object(Class<I> interface_, I object, Fun<Invocation, Invocation> fun) {
    @SuppressWarnings("unchecked") Class<I> clazz = (Class<I>) object.getClass();
    ClassLoader classLoader = clazz.getClassLoader();
    Class<?>[] classes = { interface_ };
    InvocationHandler handler = (proxy, method, parameters) -> {
        try {
            Invocation invocation0 = (m, ps) -> m.invoke(object, ps);
            Invocation invocation1 = fun.apply(invocation0);
            return invocation1.invoke(method, parameters);
        } catch (InvocationTargetException ite) {
            Throwable th = ite.getTargetException();
            throw th instanceof Exception ? (Exception) th : ite;
        }
    };
    @SuppressWarnings("unchecked") I proxied = (I) Proxy.newProxyInstance(classLoader, classes, handler);
    return proxied;
}
Also used : Proxy(java.lang.reflect.Proxy) InvocationHandler(java.lang.reflect.InvocationHandler) Method(java.lang.reflect.Method) Fun(suite.util.FunUtil.Fun) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 19 with Fun

use of suite.util.FunUtil.Fun in project suite by stupidsing.

the class Chr method chrThen.

private Streamlet<State> chrThen(Streamlet<State> states, Node then) {
    Generalizer generalizer = new Generalizer();
    Atom a = atom(".a"), b = atom(".b");
    if (Binder.bind(then, generalizer.generalize(Suite.substitute(".0 = .1", a, b)), new Trail())) {
        // built-in syntactic equality
        Reference from = generalizer.getVariable(a);
        Reference to = generalizer.getVariable(b);
        states = states.map(new Fun<>() {

            public State apply(State state) {
                IMap<Prototype, ISet<Node>> factsByPrototype1 = IMap.empty();
                for (Pair<Prototype, ISet<Node>> e : state.factsByPrototype) factsByPrototype1 = factsByPrototype1.put(e.t0, replace(e.t1));
                return new State(factsByPrototype1);
            }

            private ISet<Node> replace(ISet<Node> facts) {
                ISet<Node> facts1 = ISet.empty();
                for (Node node : facts) facts1 = facts1.replace(rw.replace(from, to, node));
                return facts1;
            }
        });
    }
    return states.map(state -> {
        Prototype prototype = Prototype.of(then);
        ISet<Node> facts = getFacts(state, prototype);
        return setFacts(state, prototype, facts.replace(then));
    });
}
Also used : Generalizer(suite.lp.doer.Generalizer) Trail(suite.lp.Trail) Prototype(suite.lp.kb.Prototype) Reference(suite.node.Reference) Node(suite.node.Node) ISet(suite.immutable.ISet) Atom(suite.node.Atom) Fun(suite.util.FunUtil.Fun)

Example 20 with Fun

use of suite.util.FunUtil.Fun in project suite by stupidsing.

the class Summarize method summarize.

public <K> SummarizeByStrategy<K> summarize(Fun<Trade, K> fun) {
    Streamlet2<K, Summarize_> summaryByKey = // 
    trades.groupBy(fun, // 
    trades_ -> summarize_(trades_, priceBySymbol, s -> null)).filterKey(// 
    key -> key != null).collect(As::streamlet2);
    Map<String, Map<K, Integer>> nSharesByKeyBySymbol = // 
    summaryByKey.concatMap((key, summary) -> // 
    summary.account.portfolio().map(// 
    (symbol, n) -> Fixie.of(symbol, key, n))).groupBy(Fixie3::get0, fixies0 -> // 
    fixies0.groupBy(Fixie3::get1, fixies1 -> // 
    fixies1.map(Fixie3::get2).uniqueResult()).toMap()).toMap();
    Map<String, Float> acquiredPrices = trades.collect(Trade_::collectBrokeredTrades).collect(Trade_::collectAcquiredPrices);
    Time now = Time.now();
    Summarize_ overall = summarize_(trades, priceBySymbol, symbol -> {
        boolean isMarketOpen = // 
        false || // 
        HkexUtil.isMarketOpen(now) || HkexUtil.isMarketOpen(now.addHours(1));
        DataSource ds = cfg.dataSource(symbol);
        // acquisition price
        float price0 = acquiredPrices.get(symbol);
        // previous close
        float price1 = ds.get(isMarketOpen ? -1 : -2).t1;
        // now
        float pricex = isMarketOpen ? priceBySymbol.get(symbol) : ds.get(-1).t1;
        String keys = // 
        Read.from2(// 
        nSharesByKeyBySymbol.getOrDefault(symbol, Map.ofEntries())).keys().map(// 
        Object::toString).sort(// 
        String_::compare).collect(As.joinedBy("/"));
        return // 
        percent(price1, pricex) + ", " + // 
        percent(price0, pricex) + (!keys.isEmpty() ? ", " + keys : "");
    });
    Map<K, String> outByKey = summaryByKey.mapValue(Summarize_::out0).toMap();
    StringBuilder sb = new StringBuilder();
    Sink<String> log = sb::append;
    for (Entry<K, String> e : outByKey.entrySet()) log.sink("\nFor strategy " + e.getKey() + ":" + e.getValue());
    log.sink(FormatUtil.tablize("\nOverall:\t" + Time.now().ymdHms() + overall.out1()));
    // profit and loss
    Map<K, Double> pnlByKey = // 
    sellAll(trades, priceBySymbol).groupBy(fun, // 
    t -> (double) Account.ofHistory(t).cash()).toMap();
    return new SummarizeByStrategy<>(sb.toString(), overall.account, pnlByKey);
}
Also used : Read(suite.streamlet.Read) Trade_(suite.trade.Trade_) Fun(suite.util.FunUtil.Fun) Yahoo(suite.trade.data.Yahoo) String_(suite.util.String_) Dbl_Dbl(suite.primitive.Dbl_Dbl) Map(java.util.Map) Fixie3(suite.adt.pair.Fixie_.Fixie3) LngFltPair(suite.primitive.adt.pair.LngFltPair) TransactionSummary(suite.trade.Account.TransactionSummary) Streamlet2(suite.streamlet.Streamlet2) HkexUtil(suite.trade.data.HkexUtil) Object_(suite.util.Object_) Hsbc(suite.trade.data.Broker.Hsbc) To(suite.util.To) Quant(ts.Quant) Iterate(suite.util.FunUtil.Iterate) Trade(suite.trade.Trade) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Fixie(suite.adt.pair.Fixie) Configuration(suite.trade.data.Configuration) Account(suite.trade.Account) Entry(java.util.Map.Entry) DataSource(suite.trade.data.DataSource) Sink(suite.util.FunUtil.Sink) As(suite.streamlet.As) Asset(suite.trade.Asset) FormatUtil(suite.util.FormatUtil) Time(suite.trade.Time) DataSource(suite.trade.data.DataSource) Trade_(suite.trade.Trade_) As(suite.streamlet.As) Map(java.util.Map)

Aggregations

Fun (suite.util.FunUtil.Fun)31 Node (suite.node.Node)18 Fail (suite.util.Fail)13 List (java.util.List)12 TermOp (suite.node.io.TermOp)12 ArrayList (java.util.ArrayList)11 Map (java.util.Map)11 Suite (suite.Suite)11 Tree (suite.node.Tree)11 Source (suite.util.FunUtil.Source)11 Atom (suite.node.Atom)10 Reference (suite.node.Reference)10 Pair (suite.adt.pair.Pair)9 Int (suite.node.Int)9 Read (suite.streamlet.Read)9 Iterate (suite.util.FunUtil.Iterate)8 To (suite.util.To)7 Collections (java.util.Collections)6 Entry (java.util.Map.Entry)6 LogUtil (suite.os.LogUtil)6