Exquisite code samples

Gor Gyolchanyan gor.f.gyolchanyan at gmail.com
Mon Jul 9 04:40:27 PDT 2012


On Mon, Jul 9, 2012 at 3:30 PM, Paulo Pinto <pjmlp at progtools.org> wrote:

> On Monday, 9 July 2012 at 11:16:45 UTC, Gor Gyolchanyan wrote:
>
>> I've put together a code sample, which could demonstrate the awesome power
>> of D when it comes to getting good results very quickly and safely.
>> Perhaps
>> it could end up on display for newcomers:
>>
>> import std.traits;
>>
>> /// Returns the t-th point on the bezier curve, defined by non-empty set p
>> of d-dimensional points, where t : [0, 1] and d > 1.
>> real[d] bezier(size_t d, Number)(Number[d][] p, Number t)
>>     if(d > 1 && isFloatingPoint!Number)
>> in
>> {
>>     assert(p.length > 0);
>>     assert(t >= 0.0L && t <= 1.0L);
>> }
>> body
>> {
>>     return p.length > 1 ? (1 - t) * p[0..$-1].bezier(t) + t *
>> p[1..$].bezier(t) : p[0];
>> }
>>
>> /// Returns k unidistant points on the bezier curve, defined by non-empty
>> set p of d-dimensional points, where k > 0 and d > 1.
>> real[d][] bezier(size_t d, Number)(Number[d][] p, size_t k)
>>     if(d > 1 && isFloatingPoint!Number)
>> in
>> {
>>     assert(p.length > 0);
>>     assert(k > 0);
>> }
>> body
>> {
>>     Number[d][] result = new Number[d][k];
>>     foreach(i; 0..k)
>>         result[k] = p.bezier(i * (1.0L / k));
>>     return result;
>> }
>>
>
> I would not show this to newcomers, as they would probably go running for
> Go.
>
> This type of code is quite nice and the reason why I think I am better
> served with D than Go, but newcomers without strong generic programming
> background in other languages might get scared.
>
> --
> Paulo
>

You're right. This is a bit advanced code sample, which uses templates,
template constraints, contract programming among syntax advantages of D.

-- 
Bye,
Gor Gyolchanyan.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20120709/8480b8a2/attachment.html>


More information about the Digitalmars-d mailing list