Skip to content

Instantly share code, notes, and snippets.

View PetarKirov's full-sized avatar

Petar Kirov PetarKirov

View GitHub Profile
@PetarKirov
PetarKirov / rxd.meta2.d
Last active January 1, 2022 16:51
UFCS-enabled algorithms on alias sequences and "template lambdas"
/++
module rxd.meta2;
# UFCS-enabled algorithms on alias sequences and "template lambdas"
Building blocks:
* `apply(fun, args...)` - applies `args` to `fun` where `fun` is either a
function or template, through speculative evaluation.
* `Π` - dependent (templated) Pi type, used for capturing compile-time
@PetarKirov
PetarKirov / output.md
Created February 11, 2019 13:28 — forked from wilzbach/output.md
Measure import time for each module in Phobos
module bare import time
std.regex 1.04s
std.net.curl 0.29s
std.zip 0.19s
std.path 0.15s
std.socket 0.15s
std.file 0.14s
std.mmfile 0.14s
std.datetime 0.14s
enum isVar(T...) =
__traits(compiles, { void foo(typeof(T[0]) arg) { } }) &&
__traits(compiles, &T[0]);
int m = 42;
int mt(T) = 42;
const int c = 42;
const int ct(T) = 42;
immutable int i = 42;
immutable int it(T) = 42;
@PetarKirov
PetarKirov / test.cpp
Last active March 5, 2019 08:39
C++ vs D compiler-generated assignment operator
#include <cstdio>
struct M
{
int x;
M() { /* printf("M ctor %p\n", this); */ }
M(const M&) { printf("M cctor %p\n", this); }
M& operator=(const M&) { printf("M assgn %p\n", this); return *this; }
~M() { printf("M dtor %p\n", this); }
};
void main()
{
import std.stdio, std.meta : AliasSeq;
static foreach (T; AliasSeq!(S1, S2, S3, S4, S5, S6, T1, T2, T3, T4, T5, T6, C1, C2, C3, C4, C5, C6))
{{
auto tid = newTypeid!T;
pragma (msg, T, " -> ", typeof(tid.xtoHash));
scope T x = make!T;
"newTypeid!%s(x).xtoHash() -> ".writef(T.stringof);
tid.xtoHash(x).writeln;
@PetarKirov
PetarKirov / set_field.d
Created September 23, 2019 19:27
Generic setField implementation
void main()
{
struct S
{
int a = 11;
string b = "22";
bool c = true;
double d = 4.4;
char e = '5';
int f = 66;
@PetarKirov
PetarKirov / get_process_path.d
Last active October 8, 2019 12:32
Get Process Path
void main(string[] args)
{
import std.process : userShell;
import std.stdio : writeln;
auto res = getProcessPath(args.length == 2? args[1] : userShell());
res.writeln;
}
/++
Gets the path to a command by running it and then using OS APIs to query
@PetarKirov
PetarKirov / example.d
Created October 18, 2019 16:55
Dlang: wrap function with default args
import std.stdio;
int add0(int a, int b) { return a + b; }
int add1(int a, int b = 1) { return a + b; }
int add2(int a = 2, int b = 1) { return a + b; }
void main()
{
wrapper!add0(10, 20).writeln;
wrapper!add1(10, 20).writeln;
@PetarKirov
PetarKirov / example.d
Created November 20, 2019 00:06
Tuple formatting in D
import std;
void main()
{
alias NV = tuple;
auto arr = [NV("Steve", 1), NV("George", 500), NV("Adam", -5)];
writefln("%(%(%s: %s%), %)", arr);
}
@PetarKirov
PetarKirov / keybase.md
Created January 8, 2020 10:33
keybase.md

Keybase proof

I hereby claim:

  • I am petarkirov on github.
  • I am zombinedev (https://keybase.io/zombinedev) on keybase.
  • I have a public key ASCA0YsistQoFVDfAnz7lwofsPa-p9KsgmJLPiejgiHt0wo

To claim this, I am signing this object: