Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Messing with types
import std.typetuple;
import std.typecons;
import std.traits;
struct Pack(TL...)
{
alias expand = TL;
enum length = TL.length;
@disable this();
}
enum isPack(T) = is(T == Pack!Args, Args...);
template EqualLength(Packs ...)
if(allSatisfy!(isPack, Packs) && Packs.length != 0)
{
static if (Packs.length == 1) enum EqualLength = true;
else
{
template EqualLengthTo(size_t len)
{
enum EqualLengthTo(T) = len == T.length;
}
enum EqualLength = Filter!(EqualLengthTo!(Packs[0].length), Packs[1 .. $]).length == Packs.length - 1;
}
}
template Alias(alias a)
{
static if (__traits(compiles, { alias x = a; }))
alias Alias = a;
else static if (__traits(compiles, { enum x = a; }))
enum Alias = a;
else
static assert(0, "Cannot alias " ~ a.stringof);
}
template Alias(a...)
{
alias Alias = a;
}
template Head(Pack)
if(isPack!Pack)
{
alias Head = Alias!(Pack.expand[0]);
}
template Tail(Pack)
if(isPack!Pack)
{
alias Tail = Pack!(Pack.expand[1 .. $]);
}
template TemplAppender(string suffix)
{
enum TemplAppender(string s) = s ~ suffix;
}
template RoundRobin(Args ...)
if(Args.length >= 2 && allSatisfy!(isPack, Args) && EqualLength!Args)
{
static if (Args[0].length == 0)
alias RoundRobin = TypeTuple!();
else
alias RoundRobin = TypeTuple!(staticMap!(Head, Args), RoundRobin!(staticMap!(Tail, Args)));
}
alias SliceOf(T) = T[];
alias TransformMembers(alias TypeTransform, alias NameTransform, T) = Tuple!(
RoundRobin!(Pack!(staticMap!(TypeTransform, FieldTypeTuple!PriceBar)),
Pack!(staticMap!(NameTransform, FieldNameTuple!PriceBar))));
import std.datetime;
struct PriceBar
{
DateTime date;
double open;
double high;
double low;
double close;
}
alias PriceBars = TransformMembers!(SliceOf, TemplAppender!"s", PriceBar);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.