Skip to content

Instantly share code, notes, and snippets.

@Biotronic
Biotronic / algebra.d
Last active Feb 9, 2018
A template for defining algebras
View algebra.d
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 Jun 15, 2020
A Pattern for Head-mutable Structures
View head-mutable.md

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 Feb 7, 2020
Head-mutable implementation for D
View headmutable.d
import std.traits;
import std.typecons : rebindable, Rebindable;
alias HeadMutable(T) = ReturnType!((T t) => t.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 Dec 26, 2017
Expressive C++ challenge in D
View challenge.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
View MapGeneratorCS3.jsx
// 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 Jul 9, 2017
Map generation script. Results on https://imgur.com/a/j11Cm
View MapGenerator.jsx
// 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
DNA Reverse Complement
View revComp.d
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;
View Polymorphic monostate
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) {
View My fucking eyes!!
class PlayerBehavior : MonoBehavior
{
void Start()
{
// Debug.Log("Testing...");
// this.foo = bar;
View maybe in D
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() {