Skip to content

Instantly share code, notes, and snippets.

View PetarKirov's full-sized avatar

Petar Kirov PetarKirov

View GitHub Profile
@PetarKirov
PetarKirov / copy_constructor_overloads.d
Created February 2, 2026 00:52
D Copy Constructor — Minimal Overload Set
/+
Copy Constructor — Minimal Overload Set (15 overloads)
======================================================
Tests all 25 source × target qualifier combinations using
only 3 source groups × 5 target qualifiers = 15 overloads.
Target
Source │ mut │ const │ immut │ shared │ sh.const │
──────────────┼──────┼───────┼───────┼────────┼──────────┤
mutable │ G1 │ G1 │ G1 │ G1 │ G1 │
@PetarKirov
PetarKirov / compile_time_sort.d
Created January 26, 2025 10:52
Compile-time sort array in D
enum int[] x = [4, 3, 8, 10];
enum int[] y = imported!`std.algorithm`.sort(x).release;
void main()
{
int[] z = y;
imported!`std.stdio`.writeln(z);
}
@PetarKirov
PetarKirov / main.ts
Created January 14, 2024 22:24
TypeScript type-level Peano arithmetic
type Zero = [];
type NN = number[];
type GetNN<N extends NN> = N['length'];
type NextNN<N extends NN> = [...N, N['length']];
type PrevNN<N extends NN> = N extends [...infer Init, infer _] ? Init : never;
type EqualNN<A extends NN, B extends NN> =
GetNN<A> extends GetNN<B> ? true : false;
@PetarKirov
PetarKirov / fiber_thread_example.d
Created December 1, 2022 11:36
Switching fiber execution to a different thread
import core.thread : thread_joinAll, Thread, ThreadGroup, Fiber;
import std.stdio : writefln;
void main()
{
writefln("Running on the main thread with id: '%s'", Thread.getThis().id);
auto fiber = new Fiber(() {
writefln("[before suspend] Running inside fiber on thread id '%s'", Thread.getThis().id);
Fiber.yield();
@PetarKirov
PetarKirov / test.bash
Created April 10, 2022 13:17
Parsing git remote project names and paths
while IFS="" read -r line; do
project_path=$(echo "$line" | sed -E 's"(git@|.+://)[^:/]+[:/](.+)/.+$"\2"')
repo_name=$(echo "$line" | sed -E -e 's|.+/(.+)$|\1|' -e 's|(.+)(.git)$|\1|')
echo "* '$line'"
echo " * '$project_path'"
echo " * '$repo_name'"
echo
done < ./example_urls
@PetarKirov
PetarKirov / README.md
Created December 21, 2021 08:49
Trivial subset implementation in D
interface ICallable
{
void opCall() const;
}
auto makeDelegate(alias fun, Args...)(auto ref Args args)
{
return new class(args) ICallable
{
Args m_args;
@PetarKirov
PetarKirov / README.md
Last active November 8, 2021 10:33
docker-bake.json D and Nix generators

docker-bake.json generators in various languages

D

Requirements

  • dmd >= 2.089.0

How to run

@PetarKirov
PetarKirov / example.d
Created October 8, 2021 16:59
Generic basic toString example in D
void genericToString(T, Writer)(scope ref const T obj, scope ref Writer writer)
{
import std.format : formattedWrite;
writer.formattedWrite("Transaction {\n");
writer.formattedWrite!" from: 0x%s\n"(obj.from.chars);
writer.formattedWrite!" to: 0x%s\n"(obj.to.chars);
writer.formattedWrite("}");
}
@PetarKirov
PetarKirov / README.md
Last active September 30, 2021 15:06
Calling Ethereum JSON-RPC from D

Calling Ethereum JSON-RPC from D

Usage

./eth_json_rpc.d --rpc-url '<RPC-URL-HERE>' --block-number <NUMBER> [--include-txs <true|false>]