22
\$\begingroup\$

Let's say a challenge is asking for a program without arguments or function without parameters. Are we allowed to take an empty parameter instead we won't use anywhere?

As example: In Java 8+ when we take no input in our lambda we do this:

()->doSomething

And when we have a lambda which does take a single input, we do this:

n->doSomething

As you can see, the second is one byte shorter than the first. Because of that I created this meta-post with the following question: Are we allowed to use the second (where n is 0, "", null, or any random value at all, which we don't use in the function), when a program/function without arguments is asked?

EDIT: Please note that by unused empty parameter, I indeed mean unused. For example, you could use an empty parameter which you don't use directly in your code, but still use it for it's static functions, like (where String s=null) s->s.join(...), which should be s->"".join(...) instead. For Java Void might be useful to use as unused parameter, since it has no additional static uses.
Other unused parameters could be used in other languages, as long as they are indeed empty and unused.

\$\endgroup\$
12
  • \$\begingroup\$ Related. (Similar question for programs instead of functions.) \$\endgroup\$ Jun 1 '17 at 14:51
  • \$\begingroup\$ The second one seems a bit weird, but I'd be wary of disallowing it, because I'm sure if I look hard enough I'll find a language that doesn't have parameterless functions... \$\endgroup\$ Jun 1 '17 at 14:52
  • 3
    \$\begingroup\$ We already do this in JS so, by extension, it should be allowed in Java. \$\endgroup\$
    – Shaggy
    Jun 1 '17 at 14:58
  • 1
    \$\begingroup\$ I've usually gone by the rule of if a challenge doesn't explicitly disallow taking input then you can do this, if it does don't. \$\endgroup\$ Jun 1 '17 at 14:59
  • 7
    \$\begingroup\$ @Shaggy But in JavaScript you can still call the function without arguments (they'll just end up being undefined). That's not possible in more strongly typed languages. So in those languages, having a 1-argument function with an ignored argument makes the calling convention weirder. \$\endgroup\$ Jun 1 '17 at 15:09
  • \$\begingroup\$ @MartinEnder In a language like Standard ML, every function has exactly one parameter. \$\endgroup\$ Jun 1 '17 at 17:47
  • \$\begingroup\$ @MartinEnder: In Unlambda, all functions take a parameter (and can effectively take more via currying). In practice, it's usual to use i as the parameter if there's no reason to use something else. \$\endgroup\$
    – user62131
    Jun 2 '17 at 15:11
  • \$\begingroup\$ This comment will not be developed enough to be an answer, but the way I see it, the same defaults of programs apply to functions (according to the standard I/O post). Therefore, you may assume an empty string of input, which is analagous to an empty stdio stream. \$\endgroup\$ Jun 2 '17 at 18:10
  • \$\begingroup\$ Those things aren't the same though. An empty string is not the same as a variable with the value null/undefined, and in some languages, is not the same as not passing a variable at all. Writing _=> instead of ()=> in JavaScript changes nothing about the way the function is called, and it will behave the same no matter what it is passed. I don't think you can say that for Java. \$\endgroup\$ Jun 5 '17 at 23:26
  • \$\begingroup\$ @MartinEnder In J, every function must have at least one argument. There is no way to call a function without an argument. \$\endgroup\$
    – Adám
    Jun 6 '17 at 21:38
  • 1
    \$\begingroup\$ Since I gather that you're concerned about abuse of the proposed option, you could consider clarifying that in the case of languages which have multiple types the correctness of the code should be independent both of the value and of the type of n. (And in Java you could propose that Void is a good default for the purposes of documenting that you don't intend to rely on any useful property of the parameter). \$\endgroup\$ Sep 13 '17 at 14:31
  • \$\begingroup\$ @PeterTaylor Thanks or the suggestions of Void, will use that from now on. I've been using Object o=null so far in my answers. \$\endgroup\$ Sep 13 '17 at 20:24
20
\$\begingroup\$

Yes

Building off of Martin's great post:

If your program takes extra input, that input must be empty.

In the case of Java, this is easy: Objects must be null, and primitives must have their default values.

That said, it is far harder to abuse extra parameters for golfing because lambdas require types:

(int n)->doSomething  //5 extra bytes

\$\endgroup\$
17
  • \$\begingroup\$ "If your program takes extra input, that input must be empty." - is an empty array empty? What about an array of empty arrays? Can I check the length of that array of empty arrays? \$\endgroup\$
    – Stephen
    Jun 2 '17 at 19:01
  • \$\begingroup\$ A 2D array is the following int[][] arr = new int[][]{}; \$\endgroup\$ Jun 2 '17 at 19:03
  • 5
    \$\begingroup\$ Arrays are objects, so they should be null by your rules. \$\endgroup\$
    – user45941
    Jun 2 '17 at 23:07
  • 1
    \$\begingroup\$ @Mego just tested it: You're totally right. This means I can make a List<int[]>. How gross is that. \$\endgroup\$ Jun 3 '17 at 3:33
  • 1
    \$\begingroup\$ @NathanMerrill You could have a List<<T>[]> if you really wanted to. Just to make Java have an aneurysm (and if I am recalling the declarative syntax correctly). \$\endgroup\$ Jun 5 '17 at 18:44
  • 3
    \$\begingroup\$ No they don't, n->doSomething is valid, no type required. \$\endgroup\$
    – Pavel
    Jun 6 '17 at 18:11
  • \$\begingroup\$ I agree with this until That said, it is far harder to abuse extra parameters for golfing because lambdas require types. They don't require types. \$\endgroup\$ Jun 7 '17 at 10:42
  • 1
    \$\begingroup\$ @Pheonix No, types are required \$\endgroup\$ Jun 7 '17 at 12:47
  • \$\begingroup\$ @NathanMerrill You say they are but +12/-7 is hardly a consensus. Plus languages like this are already never going to win a golfing challenge requiring extra bytes, for literally no point (as they are not needed), only makes the gap even larger. \$\endgroup\$ Jun 7 '17 at 15:09
  • \$\begingroup\$ @TheLethalCoder you are right, but its the current top answer. If you have a better answer, feel free to post it. \$\endgroup\$ Jun 7 '17 at 15:19
  • 4
    \$\begingroup\$ @NathanMerrill Well, it's not the current top answer anymore. I would like to kindly remove that extra note from your answer as it is still debated and you say it in your answer as it was a fact, but it isn't. Thank you :) \$\endgroup\$ Jun 9 '17 at 8:31
  • 1
    \$\begingroup\$ The only empty input to a function is not passing any arguments at all, which n->doSomething won't allow in Java. \$\endgroup\$
    – Dennis
    Jun 13 '17 at 7:54
  • \$\begingroup\$ By specifying allowed values for empty input, this answer seems to imply that programs may use empty input. This is not consistent with the question. \$\endgroup\$
    – Jakob
    Aug 18 '17 at 15:47
  • \$\begingroup\$ Who says they can't use it? If I pass in an integer (which defaults to 0), I see no problem with them using that value. \$\endgroup\$ Aug 18 '17 at 18:01
  • 2
    \$\begingroup\$ @NathanMerrill, I don't know whether there are any languages in which that's true, but it's certainly not true in Java. See docs.oracle.com/javase/specs/jls/se8/html/… and in particular "Example 15.12.4.1-1. ... When a target reference is computed and then discarded because the invocation mode is static, the reference is not examined to see whether it is null" \$\endgroup\$ Sep 13 '17 at 14:21

You must log in to answer this question.

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