Skip to content

Instantly share code, notes, and snippets.

View PetarKirov's full-sized avatar

Petar Kirov PetarKirov

View GitHub Profile
@PetarKirov
PetarKirov / check_if_invariants_are_enabled.d
Last active November 18, 2020 06:31
D: Check if invariants are enabled using CTFE
bool invariantsEnabled() @trusted nothrow pure
{
static struct HasInvariant
{
@safe pure:
invariant { throw new Exception("From invariant"); }
void triggerInvaraint() { }
}
try
HasInvariant().triggerInvaraint();
@PetarKirov
PetarKirov / app.d
Last active November 16, 2020 07:00
LDC 1.24.0. / CT string compare / druntime symbols
extern(C) void _start() {
static if ("a" == "a") {}
}
@PetarKirov
PetarKirov / range_primitives_example.d
Last active September 27, 2020 18:23
Example usage of D range primitives
module range_primitives_example;
/++
Takes a slice of the first `n` elements from `input` and advances it by the
same amount.
+/
inout(int[]) readN_version1(ref inout(int)[] input, size_t n)
{
auto result = input[0 .. n];
input = input[n .. $];
@PetarKirov
PetarKirov / global-immutable-aa-example.d
Created September 23, 2020 19:54
Global immutable associative array variables
void main() {}
immutable string[ubyte] valueTypes;
// This is a shared module constructor. Code in this block will be executed
// exactly once at application startup. It is similar to running code in the
// global scope in JavaScript. Shared module constructors are allowed to
// "mutate" immutable global variables in order to initialize them.
shared static this()
{
@PetarKirov
PetarKirov / enum-aa-example.d
Created September 23, 2020 19:11
Enum associative arrays
void main() {}
enum string[ubyte] valueTypes = [
0x7F: "i32",
0x7E: "i64",
0x7D: "f32",
0x7C: "f64"
];
pure unittest
@PetarKirov
PetarKirov / example.d
Last active September 2, 2020 17:35
Attaching UDAs to enum members in dlang
void main()
{
import std.stdio : writeln;
import std.traits : EnumMembers;
foreach (SymbolKind kind; [EnumMembers!SymbolKind])
kind.enumUdatoString.writeln;
`----------------------`.writeln;
@PetarKirov
PetarKirov / tree.d
Last active August 13, 2020 10:22
tree formatting
void main()
{
import std.json : parseJSON;
import std.stdio : writeln;
const parsed = mySampleJson.parseJSON;
const tree = parsed.jsonToTree;
(*tree).writeln;
}
@PetarKirov
PetarKirov / results.txt
Created June 7, 2020 21:01
Running Dub's testsuite on Android (under Termux) with LDC 1.22.0-beta2
[INFO] Running /data/data/com.termux/files/home/code/repos/dlang/dlang/dub/test/0-init-fail-json.sh...
[INFO] Running /data/data/com.termux/files/home/code/repos/dlang/dlang/dub/test/0-init-fail.sh...
[INFO] Running /data/data/com.termux/files/home/code/repos/dlang/dlang/dub/test/0-init-interactive.sh...
Package recipe format (sdl/json) [json]: Name [0-init-interactive]: Description [A minimal D application.]: Author name [u0_a160]: License [proprietary]: Copyright string [Copyright © 2020, author]: Add dependency (leave empty to skip) []: Successfully created an empty project in '/data/data/com.termux/files/home/code/repos/dlang/dlang/dub/test/0-init-interactive'.
Package successfully created in 0-init-interactive
[INFO] Running /data/data/com.termux/files/home/code/repos/dlang/dlang/dub/test/0-init-multi-json.sh...
Successfully created an empty project in '/data/data/com.termux/files/home/code/repos/dlang/dlang/dub/test/0-init-multi-pack'.
Package successfully cre
@PetarKirov
PetarKirov / azure.md
Last active April 9, 2020 09:53
Azure CLI tips

Useful Azure CLI commands

  • Register all Microsoft resource providers
    az provider list --query "[? starts_with(@.namespace, 'Microsoft')].namespace" -o tsv \
      | xargs -I{} sh -c 'echo "Registering provider {}" && az provider register -n {}'
@PetarKirov
PetarKirov / hello-node.Dockerfile
Created April 8, 2020 22:28
Test Node.js Docker container
ARG BASE_IMAGE=node:lts-alpine
FROM $BASE_IMAGE
WORKDIR /src
ENV HOST=0.0.0.0 PORT=8080
RUN echo $'\
"use strict";\n\
const http = require("http");\n\
const server = http.createServer((request, response) => {\n\ response.writeHead(200, {"Content-Type": "text/plain"});\n\
response.end("Hello, World!");\n\
});\n\