On Thursday, 30 August 2012 at 17:40:16 UTC, Philippe Sigaud
wrote:
On Thu, Aug 30, 2012 at 7:10 PM, Danny Arends
<[email protected]> wrote:
I wrote a blog post about the stuff I've been doing last
weekend using
CTFE.
All comments are welcome, you can find the blog post at:
http://www.dannyarends.nl/index.cgi?viewDetailed=00029
Nice article, Danny!
A few remarks:
degToRad!(float,int) 45
First, it seems like you missed a parenthesis pair?
Indeed, fixed
The compiler will be able to determine V in degToRad, you can
call it like this:
degToRad!(float)(45)
Following bearophile's use of isFloatingPoint, you can use a
default
value, if that's what you need most of the time:
import std.traits;
pure U degToRad(U = float, V)(in V deg) if (isFloatingPoint!U
&& isIntegral!V)
{ return (deg * PI) / 180.0; }
Interesting, I knew about defaults but I tend to forget about
them, when I can use them...
Then, to call it:
degToRad(45) => automatically expand to detToRad!(float,int)(45)
And the same type deduction for cordic gives you
cordic( degToRad(45) );
instead of
cordic!(float)(degToRad!(float,int) 45);
In gen_trigonometric, I think the float call should be a T:
result ~= cordic!T(degToRad!(float,int)(i), iter);
Indeed, fixed !
=>
result ~= cordic( degToRad!(T)(i), iter);
And, since you know the result's size in advance, you might
want to
generate it at once:
T[2][] result = new (T[2][])(iter);
foreach(i; 0 .. 360)
result[i] = cordic(degToRad!(T)(i), iter);
return result;
(no need for braces for a one-expression foreach)
Again valid point. Though the compile time benefits will be minor
with all the memory CTFE is gobbling up anyway.
Or even, using map:
import std.algorithm, std.array;
return map!( i => cordic(degToRag!(T)(i), iter) )(result).array;
I like the map syntax, that's prob. because I've got an R
background
where we have lapply (1D) and apply (2D)
Still I don't seem to get used to the => syntax...
Thanks for the feedback,
Gr,
Danny
If I get round to it I'll also update the code to use default
return types.
Though I like being explicit with types, if you got them flaunt
them...