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.

  • Related. (Similar question for programs instead of functions.) – Martin Ender Jun 1 '17 at 14:51
  • 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... – Martin Ender Jun 1 '17 at 14:52
  • 3
    We already do this in JS so, by extension, it should be allowed in Java. – Shaggy Jun 1 '17 at 14:58
  • 1
    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. – TheLethalCoder Jun 1 '17 at 14:59
  • 7
    @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. – Martin Ender Jun 1 '17 at 15:09
  • @MartinEnder In a language like Standard ML, every function has exactly one parameter. – musicman523 Jun 1 '17 at 17:47
  • @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. – user62131 Jun 2 '17 at 15:11
  • 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. – Rohan Jhunjhunwala Jun 2 '17 at 18:10
  • 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. – Steve Bennett Jun 5 '17 at 23:26
  • @MartinEnder In J, every function must have at least one argument. There is no way to call a function without an argument. – Adám Jun 6 '17 at 21:38
  • 1
    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). – Peter Taylor Sep 13 '17 at 14:31
  • @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. – Kevin Cruijssen Sep 13 '17 at 20:24
up vote 16 down vote accepted

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

  • "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? – Stephen Jun 2 '17 at 19:01
  • A 2D array is the following int[][] arr = new int[][]{}; – Nathan Merrill Jun 2 '17 at 19:03
  • 5
    Arrays are objects, so they should be null by your rules. – Mego Jun 2 '17 at 23:07
  • 1
    @Mego just tested it: You're totally right. This means I can make a List<int[]>. How gross is that. – Nathan Merrill Jun 3 '17 at 3:33
  • 1
    @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). – Draco18s Jun 5 '17 at 18:44
  • 3
    No they don't, n->doSomething is valid, no type required. – Pavel Jun 6 '17 at 18:11
  • 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. – TheLethalCoder Jun 7 '17 at 10:42
  • 1
    @Pheonix No, types are required – Nathan Merrill Jun 7 '17 at 12:47
  • @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. – TheLethalCoder Jun 7 '17 at 15:09
  • @TheLethalCoder you are right, but its the current top answer. If you have a better answer, feel free to post it. – Nathan Merrill Jun 7 '17 at 15:19
  • 4
    @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 :) – Olivier Grégoire Jun 9 '17 at 8:31
  • 1
    The only empty input to a function is not passing any arguments at all, which n->doSomething won't allow in Java. – Dennis Jun 13 '17 at 7:54
  • 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. – Jakob Aug 18 '17 at 15:47
  • 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. – Nathan Merrill Aug 18 '17 at 18:01
  • 2
    @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" – Peter Taylor 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 .