std.range should support recursion (Was: One-line FFT, nice!)

Mehrdad wfunction at hotmail.com
Tue Sep 25 01:22:27 PDT 2012


I thought I'd take the opportunity to point this out:

The one-line FFT in D is pretty inefficient because it allocates 
memory.

If std.range supported recursion (i.e. by providing a different 
implementation for ranges that can be implemented without 
creating new times, i.e. Stride of Stride == Stride), then it 
would make the library a lot more usable and less bloated.


My one-line FFT illustrates it perfectly:


import std.algorithm;
import std.math;
import std.range;
typeof(R.init.stride(0)) dft(R)(R v)
{
	return v.length > 1 ?
	  (p => chain(map!(q => q[0] + q[1])(p), map!(q => q[0] - 
q[1])(p)))
	  (zip(dft(v.stride(2)),
	       map!(p => p[1] * expi(p[0] * -2 * PI / v.length))
	       (zip(iota(v.length / 2), dft(v.drop(1).stride(2)))))) : v;
}
void main() { dft([1.0, 2, 3]); }


Side note: the error messages are also hard to read.


More information about the Digitalmars-d mailing list