Skip to content

Instantly share code, notes, and snippets.

@Biotronic
Biotronic / algebra.d
Last active February 9, 2018 17:04
A template for defining algebras
import std.algorithm : among;
import std.array : appender;
import std.string : split, isNumeric;
import std.meta : allSatisfy, aliasSeqOf, Filter, templateNot, Repeat;
import std.conv : to, text;
import std.format : format;
alias complex = Algebra!(
float,
"1,i",
@Biotronic
Biotronic / head-mutable.md
Last active June 15, 2020 13:48
A Pattern for Head-mutable Structures

A Pattern for Head-mutable Structures

Summary

When Andrei Alexandrescu [introduced][on-iteration] ranges to the [D programming language][D], the gap between built-in and user-defined types (UDTs) narrowed, enabling new abstractions and greater composability. Even today though, UDTs are still second-class citizens in D. One example of this is support for head-mutability - the ability to manipulate a reference without changing the referenced value(s). This document details a pattern that will further narrow the UDT gap, by introducing functions for defining and working with head-mutable user-defined types.

Introduction

[D][D] is not [Kernel][kernel] or [Scheme][scheme] - it has first-class and second-class citizens. Among its first-class citizens are arrays and pointers. One of the benefits these types enjoy is implicit conversion to head-mutable - for instance, const(T[]) is implicitly convertible to const(T)[]. Partly to address this difference, D has many ways to define ho

@Biotronic
Biotronic / headmutable.d
Last active September 7, 2022 13:36
Head-mutable implementation for D
import std.traits;
import std.typecons : rebindable, Rebindable;
alias HeadMutable(T) = typeof(T.init.headMutable());
alias HeadMutable(T, ConstSource) = HeadMutable!(CopyTypeQualifiers!(ConstSource, T));
auto headMutable(T)(T value) {
static if (isPointer!T) {
// T is a pointer, and decays naturally.
return value;
@Biotronic
Biotronic / challenge.d
Last active December 26, 2017 02:37
Expressive C++ challenge in D
/*
Expressive C++17 challenge, in D
http://www.bfilipek.com/2017/09/the-expressive-cpp17-challenge.html
D features used:
Ranges (MmText, zip, map)
UFCS (all over)
String mixins (in find)
Wysiwyg strings
Scoped imports
// The desired width of the image.
var desiredSize = UnitValue(prompt("Please type the desired width of your map (in pixels).", "2048"), "px");
var generatedSize = UnitValue("2048", "px");
// How many times to apply the difference clouds filter to the background.
// This can be lower for smaller maps, and probably should be bigger for ginormous ones.
var numClouds = 6;
// How many times to apply the difference clouds filter to the highlight layer.
@Biotronic
Biotronic / MapGenerator.jsx
Last active July 9, 2017 14:59
Map generation script. Results on https://imgur.com/a/j11Cm
// The desired width of the image.
var desiredSize = UnitValue(prompt("Please type the desired width of your map (in pixels).", "2048"), "px");
var generatedSize = UnitValue("2048", "px");
// How many times to apply the difference clouds filter to the background.
// This can be lower for smaller maps, and probably should be bigger for ginormous ones.
var numClouds = 6;
// How many times to apply the difference clouds filter to the highlight layer.
@Biotronic
Biotronic / revComp.d
Created May 22, 2017 06:30
DNA Reverse Complement
import std.exception;
import std.stdio;
import std.conv;
import std.range;
import std.algorithm;
import std.datetime;
import std.meta;
string randomDna(int length) {
import std.random;
final class PolymorphicMonostate(T) if (is(T == class)) {
static T _instance;
static void Create(T2 : T = T, U...)(U args) if (is(typeof(new T2(args)))) {
_instance = new T2(args);
}
T get() {
return _instance;
}
alias get this;
} version (unittest) {
class PlayerBehavior : MonoBehavior
{
void Start()
{
// Debug.Log("Testing...");
// this.foo = bar;
import std.typecons;
import std.traits;
import std.typetuple;
import std.stdio : writeln;
struct Maybe(T) {
T value = void;
bool b = true;
@property static Maybe None() {