pi program

Russel Winder via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 25 05:51:02 PDT 2015


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: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: 181 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150925/0309e9d0/attachment.sig>


More information about the Digitalmars-d-learn mailing list