Skip to content

Instantly share code, notes, and snippets.

View PetarKirov's full-sized avatar

Petar Kirov PetarKirov

View GitHub Profile
@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>]
@PetarKirov
PetarKirov / from_json.d
Last active September 30, 2021 13:34
JSON Deserialization in D
import std.json : JSONValue, parseJSON;
T fromJson(T)(JSONValue j)
{
import std.traits : isNumeric;
// Handle JSON number and boolean values:
static if (is(typeof(j.get!T)))
return j.get!T;

Example usage

➤ ./test.d --first arg --second arg2
["--first", "arg", "--second", "arg2"]