[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: accumulator generator (Java)
Anton van Straaten wrote:
>
> > I'm especially interested in knowing what, if any,
> > would be the canonical implementation in Java and C++...
>
> "Canonical" in Java would simply be to use a class:
>
> public class foo {
> private int n;
> public foo( int n ) { this.n = n; }
> public int inc( int i ) { n += i; return n; }
> }
>
> Which is invoked as:
>
> foo f = new foo( 4 );
> f.inc( 3 );
> f.inc( 2 );
>
> Of course, this uses an object ("to simulate a closure", if you like) as
> opposed to "pure" functions.
>
> Can we do better in Java? Well, sort of yes, but mostly no.
I don't understand how you can do "better" than using a core language
feature exactly as it was intended to be used, in a manner that is
completely clear. The only sense in which your other solution is better
is in that it more clearly apes the Lisp way of solving the problem.
That's my overall problem with this particular microbenchmark.
Paul Prescod