Java Mailing List Archive

http://www.gg3721.com/

Home » user.groovy »

[groovy-user] Closure vs functions in scripts -- are there Groovy idioms?

Russel Winder

2008-08-31

Replies: Find Java Web Hosting

Author LoginPost Reply
I am wondering about whether it is better to use closures or functions
in scripts, and whether there are extant "best idioms".

Take for example drawing the von Koch Snowflake -- basic recursive
fractal curve, so uses recursion for implementation (well there are
other ways but recursive is probably the easiest). Using closures you
get:

    def smallestLine = 1.0
    def vonKochCurve
    vonKochCurve = { g -> . . . }

etc, etc. Using functions, you have to use the binding because of the
(in my view) inconsistent and counter-intuitive scope rules:

    smallestLine = 1.0
    def vonKochCurve ( g ) { . . . }

I guess the question is what do people consider the better coding style
and why?

Thanks.

--
Russel.
====================================================
Dr Russel Winder           Partner

Concertant LLP             t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,         f: +44 8700 516 084
London SW11 1EN, UK.         m: +44 7770 465 077

Attachment: signature.asc (zipped)
©2008 gg3721.com - Jax Systems, LLC, U.S.A.