Templates problem
Russel Winder via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Sep 7 04:29:18 PDT 2016
On Wed, 2016-09-07 at 20:32 +1200, rikki cattermole via Digitalmars-d-
learn wrote:
>
[…]
> Ok, I have it mostly compiling.
>
> void run_mean() {}
> void run_mode() {}
> void run_stdDev() {}
For my code the functions have to be declared within the main function
rather than being at module level. But this turns out to be a non-
issue.
> void main() {
> import std.datetime;
> import std.algorithm : map;
> import std.conv;
> import std.array;
I am still not a fan of this import all symbols approach, like the map
import, I would do the same for the other modules' functions used.
> const results = benchmark!(run_mean, run_mode, run_stdDev)(1);
> const times = map!((TickDuration t) { return
> (to!Duration(t)).total!"seconds"; })(results[]);
It turns out my problem was here. See below.
> import std.stdio;
> foreach(v; times) {
> writeln(v);
> }
> }
>
> However times really can't be const. Since times is an input range
> which
> gets modified as part of the iterations.
Indeed. I keep forgetting that ranges are use once mutable entities,
and there is no option. s/const/auto/ solves the problem, but the more
likely general solution is to retain the const and reify the array. But
it depends on usage, obviously.
> So what exactly do you want times to be? If you want an array .array
> from std.algorithm it which can be const. Either way there is
> something
> I'm missing as to why const is so important here.
In this case it isn't as you can't use a constant range. The problem I
am having is when I cannot use const or better immutable. I'm a single
assignment sort of person, but D ranges break all that orthodoxy.
--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder at ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: russel at winder.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20160907/d596e599/attachment-0001.sig>
More information about the Digitalmars-d-learn
mailing list