On Fri, 2015-09-25 at 09:14 +0000, mzfhhhh via Digitalmars-d-learn
wrote:
> > If we worry about the string form and instead try:
> >
> > double reduce_string_loop() {
> > return reduce!"1.0 / (a * a)"(iota(1, 100));
> > }
> >
> > double reduce_function_loop() {
> > return reduce!((n) => 1.0 / (n * n))(iota(1, 100));
> > }
> >
>
> it should be write :
> double reduce_loop() {
> //return iota(1, 100).map!"1.0 / (a * a)".sum;
> return iota(1, 100).reduce!"a + 1.0 / (b * b)";
> }
Aha, bingo, spot on. Thanks. Amended now to:
double reduce_string_loop() {
return reduce!"a + 1.0 / (b * b)"(iota(1, 100));
}
double reduce_function_loop() {
return reduce!((t, n) => t + 1.0 / (n * n))(iota(1, 100));
}
which both work as they should. I am sure I will be able to find a
reason why I missed that reduce takes a function of two parameters not
one.
Interesting question on style is whether to use function application or
method call:
reduce!"a + 1.0 / (b * b)"(iota(1, 100))
vs.
iota(1, 100).reduce!"a + 1.0 / (b * b)"
The debate may well turn into a bikeshed one, but it would be good to
know what the opinions are.
--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:[email protected]
41 Buckmaster Road m: +44 7770 465 077 xmpp: [email protected]
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
signature.asc
Description: This is a digitally signed message part
