Code Snippets D

Short texts D 🧑‍💻 Code snippets D ✍️ Reading lists D 👀

Code shared from run.dlang.io.

import std.range; import std.stdio; import std.format; import std.traits; import std.math; import std.conv; import std.array; import std.random; import std.algorithm; import std.string; auto bisectionMethod(alias fun, T)(T a, T b, T eps) if (isFloatingPoint!T) { assert(fun(a) * fun(b) < 0); T c = 0.0; T prevC = a; while (true) { c = (a + b) / 2; […]

Code shared from run.dlang.io.

import std; void main() { string name; write(“Whats your’s name: “); readf(“%s\n”, name); writeln(“Hello “, name); }

Serialization.d

module agora.test.Serialization; version (unittest): import agora.crypto.Types; import agora.utils.Test; import agora.consensus.data.Params; import agora.consensus.data.Transaction; import agora.consensus.data.Block; import agora.serialization.Serializer; import agora.test.Base; import vibe.data.json; import vibe.data.serialization; import vibe.web.common; import core.thread; import std.stdio; public string toHexString(ref ubyte[] data) { static immutable LHexDigits = `0123456789abcdef`; string str = “”; char[2] hex; for (int i; i < data.length; i++) { hex[0] = […]

Code shared from run.dlang.io.

import std.stdio, std.format; struct Set (T) { bool[T] _set; void toString (scope void delegate (const char[]) @safe sink) const { formattedWrite(sink, “[%(%s, %)]”, this._set.byKey()); } } void main () { Set!string set; set._set[“Hello”] = true; set._set[“World”] = false; set.writeln; }