Skip to content

Instantly share code, notes, and snippets.

View PetarKirov's full-sized avatar

Petar Kirov PetarKirov

View GitHub Profile
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 / 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
@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 / posix_spawn_example.d
Created October 14, 2018 16:51
Example usage of posix_spawn in D
import core.sys.posix.sys.types : mode_t, pid_t;
import core.sys.posix.signal : sigset_t;
import core.sys.posix.sched : sched_param;
import core.stdc.stdio : perror, printf;
import core.stdc.errno : errno;
import core.stdc.stdlib : exit, EXIT_FAILURE;
void checkErrno(int err, const char* msg)
{
if (!err) return;
vibe-d
sociomantic-tsunami/ocean
dlang/dub
higgsjs/Higgs
rejectedsoftware/ddox
BlackEdder/ggplotd
eBay/tsv-utils-dlang
dlang-community/D-Scanner
dlang-tour/core
d-widget-toolkit/dwt
@PetarKirov
PetarKirov / dub_variable_expansion.d
Last active September 9, 2018 16:23
Variable expansion in Dub
import std.array : appender;
import std.regex : regex, replaceAll;
import std.string : format, indexOf, toUpper;
import std.stdio : writeln;
string getVariable(string var)
{
return "<%s>".format(var.toUpper);
}
@PetarKirov
PetarKirov / runtime_dispatch.d
Last active December 4, 2020 08:46
Implementing run-time dispatch in D
import std.conv : to;
import std.meta : ApplyLeft, Filter;
import std.stdio : writef, writefln, writeln;
import std.variant : Variant;
void main()
{
auto c = new MyClass();
c.call("v0", [ ]);
c.call("v1", [ Variant(42), Variant("test") ]);
@PetarKirov
PetarKirov / test.d
Created September 9, 2017 22:37
CTFE Keeping global state
size_t bsearch(alias pred, uint start, uint end)()
{
static if (end <= start)
static assert (0);
else static if (start + 1 == end && pred!start)
return start;
else
{
@PetarKirov
PetarKirov / main.d
Created September 9, 2017 13:09
CTFE RNG
// http://forum.dlang.org/post/oxxydengmzzefjxjqgma@forum.dlang.org
/+ CTRNG
Proof-Of-Concept Compile-Time Random Number Generator.
Please never actually use this or anything like it.
While doing some metaprogramming tomfoolery, I stumbled into an
interesting scenario. It occurred to me that with some work, I could
probably turn what I had into a functioning compile-time random
number generator, despite D's restrictions on metaprogramming intended
@PetarKirov
PetarKirov / text.md
Last active August 22, 2017 14:26
Evaluating `scope`

Consider the following program:

@safe:

char[] formatInPlace(T)(char[] buf, T value);

void main()
{
    char[1024] buffer;
    formatInPlace(buffer[], 28.625);