use of suite.util.FunUtil.Fun in project suite by stupidsing.
the class FltFunUtil method suck.
/**
* Sucks data from a sink and produce into a source.
*/
public static FltSource suck(Sink<FltSink> fun) {
NullableSyncQueue<Float> queue = new NullableSyncQueue<>();
FltSink enqueue = c -> enqueue(queue, c);
Thread thread = Thread_.startThread(() -> {
try {
fun.sink(enqueue);
} finally {
enqueue(queue, EMPTYVALUE);
}
});
return () -> {
try {
return queue.take();
} catch (InterruptedException ex) {
thread.interrupt();
return Fail.t(ex);
}
};
}
use of suite.util.FunUtil.Fun 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;
}
use of suite.util.FunUtil.Fun in project suite by stupidsing.
the class HtmlUtil method parse.
public HtmlNode parse(String in) {
List<IntIntPair> pairs = new ArrayList<>();
int pos0, pos1 = 0;
while (0 <= (pos0 = in.indexOf("<", pos1)) && 0 <= (pos1 = in.indexOf(">", pos0 + 1))) pairs.add(IntIntPair.of(pos0, ++pos1));
Fun<String, IntObjPair<String>> getNameFun = tag -> {
int p0 = 1, p1 = p0 + 1, px = tag.length() - 1;
char first = tag.charAt(p1);
char last = tag.charAt(px - 1);
int d;
if (first == '!')
return IntObjPair.of(0, tag);
else {
if (first == '/') {
p1++;
d = -1;
} else if (last == '/') {
px--;
d = 0;
} else
d = 1;
int ps0 = tag.indexOf(' ');
int ps1 = 0 <= ps0 ? ps0 : px;
return IntObjPair.of(d, tag.substring(p1, ps1));
}
};
Deque<HtmlNode> deque = new ArrayDeque<>(List.of(new HtmlNode(null)));
int prevp = 0;
for (IntIntPair pair : pairs) {
HtmlNode htmlNode = deque.element(), htmlNode1;
int p0 = pair.t0;
int px = pair.t1;
htmlNode.children.add(new HtmlNode(in.substring(prevp, p0)));
String tag = in.substring(p0, px);
IntObjPair<String> dn = getNameFun.apply(tag);
int d = dn.t0;
String name = dn.t1;
if (d == -1)
while (!deque.isEmpty() && !String_.equals(getNameFun.apply(deque.pop().tag).t1, name)) ;
else {
htmlNode.children.add(htmlNode1 = new HtmlNode(tag));
if (d == 1)
deque.push(htmlNode1);
}
prevp = px;
}
return deque.pop();
}
use of suite.util.FunUtil.Fun in project suite by stupidsing.
the class Nodify method newNodifier.
@SuppressWarnings("unchecked")
private Nodifier newNodifier(Type type) {
Nodifier nodifier;
if (type instanceof Class) {
Class<?> clazz = (Class<?>) type;
if (clazz == boolean.class)
nodifier = new Nodifier(object -> Atom.of(object.toString()), node -> node == Atom.TRUE);
else if (clazz == int.class)
nodifier = new Nodifier(object -> Int.of((Integer) object), node -> ((Int) node).number);
else if (clazz == Chars.class)
nodifier = new Nodifier(object -> new Str(object.toString()), node -> To.chars(((Str) node).value));
else if (clazz == String.class)
nodifier = new Nodifier(object -> new Str(object.toString()), node -> ((Str) node).value);
else if (clazz.isEnum())
nodifier = new Nodifier(object -> Atom.of(object.toString()), Read.from(clazz.getEnumConstants()).toMap(e -> Atom.of(e.toString()))::get);
else if (clazz.isArray()) {
Class<?> componentType = clazz.getComponentType();
Nodifier nodifier1 = getNodifier(componentType);
Fun<Object, Node> forward = object -> {
Node node = Atom.NIL;
for (int i = Array.getLength(object) - 1; 0 <= i; i--) node = Tree.of(TermOp.OR____, apply_(nodifier1, Array.get(object, i)), node);
return node;
};
nodifier = new Nodifier(forward, node -> {
List<Object> list = //
Read.from(//
Tree.iter(node, TermOp.OR____)).map(//
n -> apply_(nodifier1, n)).toList();
int size = list.size();
Object objects = Array.newInstance(componentType, size);
for (int i = 0; i < size; i++) Array.set(objects, i, list.get(i));
return objects;
});
} else if (// polymorphism
clazz.isInterface())
nodifier = new Nodifier(object -> {
Class<?> clazz1 = object.getClass();
Node n = apply_(getNodifier(clazz1), object);
return Tree.of(TermOp.COLON_, Atom.of(clazz1.getName()), n);
}, node -> {
Tree tree = Tree.decompose(node, TermOp.COLON_);
if (tree != null) {
Class<?> clazz1;
try {
clazz1 = Class.forName(((Atom) tree.getLeft()).name);
} catch (ClassNotFoundException ex) {
clazz1 = Fail.t(ex);
}
return apply_(getNodifier(clazz1), tree.getRight());
} else
// happens when an enum implements an interface
return Fail.t("cannot instantiate enum from interfaces");
});
else {
List<FieldInfo> fieldInfos = //
Read.from(//
inspect.fields(clazz)).map(field -> {
Type type1 = field.getGenericType();
return new FieldInfo(field, field.getName(), getNodifier(type1));
}).toList();
List<Pair<Atom, FieldInfo>> pairs = Read.from(fieldInfos).map(f -> Pair.of(Atom.of(f.name), f)).toList();
nodifier = new Nodifier(object -> Rethrow.ex(() -> {
Dict dict = new Dict();
for (Pair<Atom, FieldInfo> pair : pairs) {
FieldInfo fieldInfo = pair.t1;
Node value = apply_(fieldInfo.nodifier, fieldInfo.field.get(object));
dict.map.put(pair.t0, Reference.of(value));
}
return dict;
}), node -> Rethrow.ex(() -> {
Map<Node, Reference> map = ((Dict) node).map;
Object object1 = Object_.new_(clazz);
for (Pair<Atom, FieldInfo> pair : pairs) {
FieldInfo fieldInfo = pair.t1;
Node value = map.get(pair.t0).finalNode();
fieldInfo.field.set(object1, apply_(fieldInfo.nodifier, value));
}
return object1;
}));
}
} else if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
Type rawType = pt.getRawType();
Type[] typeArgs = pt.getActualTypeArguments();
Class<?> clazz = rawType instanceof Class ? (Class<?>) rawType : null;
if (collectionClasses.contains(clazz)) {
Nodifier nodifier1 = getNodifier(typeArgs[0]);
nodifier = new Nodifier(object -> {
Tree start = Tree.of(null, null, null), tree = start;
for (Object o : (Collection<?>) object) {
Tree tree0 = tree;
Tree.forceSetRight(tree0, tree = Tree.of(TermOp.OR____, apply_(nodifier1, o), null));
}
Tree.forceSetRight(tree, Atom.NIL);
return start.getRight();
}, node -> {
List<Object> list = Read.from(Tree.iter(node, TermOp.OR____)).map(n -> apply_(nodifier1, n)).toList();
Collection<Object> object1 = (Collection<Object>) instantiate(clazz);
object1.addAll(list);
return object1;
});
} else if (mapClasses.contains(clazz)) {
Nodifier kn = getNodifier(typeArgs[0]);
Nodifier vn = getNodifier(typeArgs[1]);
nodifier = new Nodifier(object -> {
Dict dict = new Dict();
for (Entry<?, ?> e : ((Map<?, ?>) object).entrySet()) dict.map.put(apply_(kn, e.getKey()), Reference.of(apply_(vn, e.getValue())));
return dict;
}, node -> {
Map<Node, Reference> map = ((Dict) node).map;
Map<Object, Object> object1 = (Map<Object, Object>) instantiate(clazz);
for (Entry<Node, Reference> e : map.entrySet()) object1.put(apply_(kn, e.getKey()), apply_(vn, e.getValue().finalNode()));
return object1;
});
} else
nodifier = getNodifier(rawType);
} else
nodifier = Fail.t("unrecognized type " + type);
return nodifier;
}
use of suite.util.FunUtil.Fun in project suite by stupidsing.
the class FunCreatorTest method testObject.
@Test
public void testObject() {
Int_Int inc = i -> i + 1;
Iterate<FunExpr> fun = i -> f.object(inc).invoke("apply", i);
assertEquals(3, LambdaInstance.of(Int_Int.class, fun).newFun().apply(2));
}
Aggregations