digitalmars.D - Exquisite code samples
- Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> Jul 09 2012
- "Paulo Pinto" <pjmlp progtools.org> Jul 09 2012
- Don Clugston <dac nospam.com> Jul 10 2012
- Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> Jul 09 2012
- "Tobias Pankrath" <tobias pankrath.net> Jul 09 2012
- "renoX" <renozyx gmail.com> Jul 10 2012
- "renoX" <renozyx gmail.com> Jul 10 2012
- "renoX" <renozyx gmail.com> Jul 10 2012
- "SomeDude" <lovelydear mailmetrash.com> Jul 16 2012
- "renoX" <renozyx gmail.com> Jul 17 2012
--e0cb4efe33902552fc04c463bf1d
Content-Type: text/plain; charset=UTF-8
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;
}
--
Bye,
Gor Gyolchanyan.
--e0cb4efe33902552fc04c463bf1d
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
I've put together a code sample, which could demonstrate the awesome po=
wer of D when it comes to getting good results very quickly and safely. Per=
haps it could end up on display for=C2=A0newcomers:<div><br></div><div><div=
import std.traits;</div>
by non-empty set p of d-dimensional points, where t : [0, 1] and d > 1.=
</div><div>real[d] bezier(size_t d, Number)(Number[d][] p, Number t)</div>
<div>=C2=A0 =C2=A0 if(d > 1 && isFloatingPoint!Number)</div><div=
in</div><div>{</div><div>=C2=A0 =C2=A0 assert(p.length > 0);</div><div>=
</div><div>body</div><div>{</div>
<div>=C2=A0 =C2=A0 return p.length > 1 ? (1 - t) * p[0..$-1].bezier(t) +=
t * p[1..$].bezier(t) : p[0];</div><div>}</div><div><br></div><div>/// Ret=
urns k unidistant points on the bezier curve, defined by non-empty set p of=
d-dimensional points, where k > 0 and d > 1.</div>
<div>real[d][] bezier(size_t d, Number)(Number[d][] p, size_t k)</div><div>=
=C2=A0 =C2=A0 if(d > 1 && isFloatingPoint!Number)</div><div>in</=
div><div>{</div><div>=C2=A0 =C2=A0 assert(p.length > 0);</div><div>=C2=
=A0 =C2=A0 assert(k > 0);</div>
<div>}</div><div>body</div><div>{</div><div>=C2=A0 =C2=A0 Number[d][] resul=
t =3D new Number[d][k];</div><div>=C2=A0 =C2=A0 foreach(i; 0..k)</div><div>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 result[k] =3D p.bezier(i * (1.0L / k));</div><d=
iv>=C2=A0 =C2=A0 return result;</div><div>}</div><div>
<br></div>-- <br>Bye,<br>Gor Gyolchanyan.<br>
</div>
--e0cb4efe33902552fc04c463bf1d--
Jul 09 2012
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
Jul 09 2012
On 10/07/12 09:49, renoX wrote:On Monday, 9 July 2012 at 11:40:37 UTC, Gor Gyolchanyan wrote: [cut]You're right. This is a bit advanced code sample, which uses templates,template constraints, contract programming among syntax advantages of D.
Hum it show the power of D sure, but IMHO it also show its syntax deficiencies.. For me this "real[d] bezier(size_t d, Number)(Number[d][] p, Number t) if(d > 1 && isFloatingPoint!Number)" is difficult to read, and a better syntax would be: real[d] bezier!(size_t d && d > 1, Number && isFloatingPoint!Number)(Number[d][] p, Number t) The template parameter would be indicated in a !() (as in a call), and the template constraints inside the template parameter: this way the template parameters are clearly indicated and separated from the function parameter. renoX
Well it used to work vaguely in that way, but it gets very ugly once you leave the simplest cases. Even that one you've listed is hard for me to read. And the idea that constraints apply to individual parameters is wrong. If you have a constraint that depends on two template parameters, where do you put it? int bezier (int A, int B)(int t) if ( A + B == 10 )
Jul 10 2012
--bcaec554deb2943fde04c464147e Content-Type: text/plain; charset=UTF-8 On Mon, Jul 9, 2012 at 3:30 PM, Paulo Pinto <pjmlp 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. --bcaec554deb2943fde04c464147e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Mon, Jul 9, 2012 at 3:30 PM, Paulo Pinto <spa= n dir=3D"ltr"><<a href=3D"mailto:pjmlp progtools.org" target=3D"_blank">= pjmlp progtools.org</a>></span> wrote:<br><blockquote class=3D"gmail_quo= te" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"=
UTC, Gor Gyolchanyan wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> I've put together a code sample, which could demonstrate the awesome po= wer<br> of D when it comes to getting good results very quickly and safely. Perhaps= <br> it could end up on display for newcomers:<br> <br> import std.traits;<br> <br> /// Returns the t-th point on the bezier curve, defined by non-empty set p<= br> of d-dimensional points, where t : [0, 1] and d > 1.<br> real[d] bezier(size_t d, Number)(Number[d][] p, Number t)<br> =C2=A0 =C2=A0 if(d > 1 && isFloatingPoint!Number)<br> in<br> {<br> =C2=A0 =C2=A0 assert(p.length > 0);<br> =C2=A0 =C2=A0 assert(t >=3D 0.0L && t <=3D 1.0L);<br> }<br> body<br> {<br> =C2=A0 =C2=A0 return p.length > 1 ? (1 - t) * p[0..$-1].bezier(t) + t *<= br> p[1..$].bezier(t) : p[0];<br> }<br> <br> /// Returns k unidistant points on the bezier curve, defined by non-empty<b= r> set p of d-dimensional points, where k > 0 and d > 1.<br> real[d][] bezier(size_t d, Number)(Number[d][] p, size_t k)<br> =C2=A0 =C2=A0 if(d > 1 && isFloatingPoint!Number)<br> in<br> {<br> =C2=A0 =C2=A0 assert(p.length > 0);<br> =C2=A0 =C2=A0 assert(k > 0);<br> }<br> body<br> {<br> =C2=A0 =C2=A0 Number[d][] result =3D new Number[d][k];<br> =C2=A0 =C2=A0 foreach(i; 0..k)<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 result[k] =3D p.bezier(i * (1.0L / k));<br> =C2=A0 =C2=A0 return result;<br> }<br> </blockquote> <br></div></div> I would not show this to newcomers, as they would probably go running for G= o.<br> <br> This type of code is quite nice and the reason why I think I am better serv= ed with D than Go, but newcomers without strong generic programming backgro= und in other languages might get scared.<br> <br> --<br> Paulo<br> </blockquote></div><br>You're right. This is a bit advanced code sample= , which uses templates, template constraints, contract programming among sy= ntax advantages of D.<br clear=3D"all"><div><br></div>-- <br>Bye,<br>Gor Gy= olchanyan.<br> --bcaec554deb2943fde04c464147e--
Jul 09 2012
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
And for people that have no such background the advantages need some explanation. It's not obvious, but with some explanation it is a good example.
Jul 09 2012
On Monday, 9 July 2012 at 11:40:37 UTC, Gor Gyolchanyan wrote: [cut]You're right. This is a bit advanced code sample, which uses templates,template constraints, contract programming among syntax advantages of D.
Hum it show the power of D sure, but IMHO it also show its syntax deficiencies.. For me this "real[d] bezier(size_t d, Number)(Number[d][] p, Number t) if(d > 1 && isFloatingPoint!Number)" is difficult to read, and a better syntax would be: real[d] bezier!(size_t d && d > 1, Number && isFloatingPoint!Number)(Number[d][] p, Number t) The template parameter would be indicated in a !() (as in a call), and the template constraints inside the template parameter: this way the template parameters are clearly indicated and separated from the function parameter. renoX
Jul 10 2012
On Monday, 9 July 2012 at 11:40:37 UTC, Gor Gyolchanyan wrote: [cut]You're right. This is a bit advanced code sample, which uses templates,template constraints, contract programming among syntax advantages of D.
Hum it show the power of D sure, but IMHO it also show its syntax deficiencies.. For me this "real[d] bezier(size_t d, Number)(Number[d][] p, Number t) if(d > 1 && isFloatingPoint!Number)" is difficult to read, and a better syntax would be: real[d] bezier!(size_t d && d > 1, Number && isFloatingPoint!Number)(Number[d][] p, Number t) or maybe: real[d] bezier!(size_t d, Number; d > 1 && isFloatingPoint!Number)(Number[d][] p, Number t) The template parameter would be indicated in a !() (as in a call), and the template constraints inside the template parameter: this way the template parameters are clearly indicated and separated from the function parameter. renoX
Jul 10 2012
On Monday, 9 July 2012 at 11:40:37 UTC, Gor Gyolchanyan wrote: [cut]You're right. This is a bit advanced code sample, which uses templates, template constraints, contract programming among syntax advantages of D.
Hum it shows the power of D sure, but IMHO it also shows its syntax deficiencies.. For me this "real[d] bezier(size_t d, Number)(Number[d][] p, Number t) if(d > 1 && isFloatingPoint!Number)" is difficult to read, and a better syntax would be for example: real[d] bezier!(size_t d && d > 1, Number && isFloatingPoint!Number)(Number[d][] p, Number t) or: real[d] bezier!(size_t d, Number; d > 1 && isFloatingPoint!Number)(Number[d][] p, Number t) The template parameter would be indicated in a !() (as in a call), and the template constraints inside the template parameter renoX
Jul 10 2012
On Monday, 9 July 2012 at 11:40:37 UTC, Gor Gyolchanyan wrote:On Mon, Jul 9, 2012 at 3:30 PM, Paulo Pinto <pjmlp 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.
At least, with a main() and an input, it would be a bit more interesting and illustrative of the "modeling power" of D than the examples of the http://dlang.org/index.html home page, which are stupid and mostly don't work at all. (even the simplest example gives the ridiculous result of 895 until one manually breaks the input text with carriage returns).
Jul 16 2012
On Tuesday, 10 July 2012 at 09:24:42 UTC, Don Clugston wrote:On 10/07/12 09:49, renoX wrote:On Monday, 9 July 2012 at 11:40:37 UTC, Gor Gyolchanyan wrote: [cut]You're right. This is a bit advanced code sample, which uses templates,template constraints, contract programming among syntax advantages of D.
Hum it show the power of D sure, but IMHO it also show its syntax deficiencies.. For me this "real[d] bezier(size_t d, Number)(Number[d][] p, Number t) if(d > 1 && isFloatingPoint!Number)" is difficult to read, and a better syntax would be: real[d] bezier!(size_t d && d > 1, Number && isFloatingPoint!Number)(Number[d][] p, Number t) The template parameter would be indicated in a !() (as in a call), and the template constraints inside the template parameter: this way the template parameters are clearly indicated and separated from the function parameter. renoX
Well it used to work vaguely in that way, but it gets very ugly once you leave the simplest cases. Even that one you've listed is hard for me to read.
IMHO, the "normal" way is even harder to read..And the idea that constraints apply to individual parameters is wrong. If you have a constraint that depends on two template parameters, where do you put it? int bezier (int A, int B)(int t) if ( A + B == 10 )
How about: int bezier!(int A, int B; A + B == 10)(int t) ? I think that grouping together template parameters and constraints helps the readability YMMV. BR, renoX PS: Sorry for the multiple posting, the posting didn't seem to work so I retried..
Jul 17 2012









Don Clugston <dac nospam.com> 