auto getFive = functor!(() => 5)(); assert(getFive() == 5); auto getValue = functor!(n => n)(5); assert(getValue() == 5); // Functor construction is a bit like currying, though mutation of // curried arguments (here, state) is explicitly allowed. auto addValue = functor!((n, i) => n + i)(2); assert(addValue(5) == 7); auto accumulator = functor!((ref n, i) => n += i)(0); accumulator(2); accumulator(5); assert(accumulator.state[0] == 7);
// Regular D closures are still supported. Not @nogc! auto n = 2; auto addValue = functor!(i => i + n)(); assert(addValue(5) == 7); auto m = 3; auto addMul = functor!(i => addValue(i * m))(); assert(addMul(5) == 17);
Constructs a functor with statically-defined behavior (using an alias), with optional state.