13
\$\begingroup\$

This is an extension of this meta question.

I've always assumed that function submissions were allowed under the premise that the code would be evaluated once and would in the process either:

  • Define a named function which can be used from then on.
  • End with an expression which evaluates to an unnamed function, such that if it would be captured in a name could be reused arbitrarily often.

Is that actually what everyone else thinks function submissions should be like? This encompasses several subquestions:

  • Do function submissions have to consist of a single definition/expression, or can they be accompanied by additional code which defines helper functions or variables outside the function itself?
  • Are function submissions allowed if it's necessary to repeat the entire function definition/expression each time the function should be used? (Or does it have to be possible to store the function in a name once and reuse it from the on.)
  • If functions are accompanied by additional code, and it's allowed to require the function definition to be repeated, is it allowed to require the accompanying code to be repeated, too?
\$\endgroup\$
19
\$\begingroup\$

As mentioned in the question, I do have my own opinion on this, so I'm putting it up for votes here.

I've used the argument that function submissions should be reusable several times for other policies which are generally accepted consensus now. Hence, I think that the whole point of a function submission should be that it can be reused arbitrarily often, without having to repeat any code to redefine globals or the function itself.

In summary, I would propose the following set of rules:

  1. The submitted code should either define a named function or evaluate to an unnamed function which can be captured in a name if desired (or if the language provides other mechanisms which allow the function to be used multiple times without repeating its definition).
  2. Function submissions may consist of several statements, in order to define helper functions or global variables used by the function, as long as Rule 1 is met.
  3. Using only the name that results from Rule 1 the function has to be reusable arbitrarily often, without a) restating the function definition or b) any other code accompanying the submission.
  4. The function may rely on globals as long as it doesn't break them itself (see the linked meta question). That is, you should assume that the globals you're using aren't touched by any other code between function calls. These may be defined outside the function as per this consensus.

If the code would only work by evaluating the entire code each time, make it a full-program submission instead.

\$\endgroup\$
  • 1
    \$\begingroup\$ In reality, the point of a function is to be reused arbitrarily often. But for code-golf, I'd say a function is acceptable only because it is standardized. And a whole program isn't always the most natural way for standardized code in every language. \$\endgroup\$ – jimmy23013 Nov 30 '15 at 17:56
  • \$\begingroup\$ What about languages that only have unnamed functions? e.g. SKI calculus? \$\endgroup\$ – isaacg Dec 2 '15 at 19:56
  • \$\begingroup\$ @isaacg They can sort of be given a name by passing them to another function though, I think? \$\endgroup\$ – Martin Ender Dec 2 '15 at 20:21
  • \$\begingroup\$ @MartinBüttner Either way, you should say what you mean by a name. \$\endgroup\$ – isaacg Dec 2 '15 at 20:40
  • \$\begingroup\$ @isaacg how is this? \$\endgroup\$ – Martin Ender Dec 3 '15 at 9:19
  • \$\begingroup\$ This applies specifically to global variables in c, which are initialized to 0 by the language. If function calls change the value of such a global variable, then we cannot rely on the auto-zero-init behavior for multiple function calls \$\endgroup\$ – Digital Trauma Dec 4 '15 at 23:26
  • 2
    \$\begingroup\$ @DigitalTrauma and Martin: See also "Mutable Default Arguments" at docs.python-guide.org/en/latest/writing/gotchas . It's pretty plausible that you might want to initialize a list to be empty at the beginning of a function. Putting x=[] inside the body of a Python function requires a newline plus a space, while putting x=[] as a default argument requires only a comma, so it's one byte less. But if you do it that way (as a default argument) then your function will (likely) only work correctly the first time you call it. Hence your entry would be invalid...right? \$\endgroup\$ – mathmandan Dec 10 '15 at 1:51
  • \$\begingroup\$ @mathmandan Indeed. Thanks for pointing that out, I wasn't aware mutable default arguments worked like that in Python. \$\endgroup\$ – Martin Ender Dec 10 '15 at 9:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .