3
\$\begingroup\$

From what I have seen so far the usual way of scoring in java is by using lambda expressions. I often see Try it online used for it. My actual question comes now:

Can you reuse the name of a named lambda to define a variable, thus allowing you to remove the var? For example, normally you'd do:

f -> {var s = ""; /* rest of the code */}

Are you allowed to make f a String to save a few characters? It'd then look like this:

f -> {f = ""; /* rest of the code */}
\$\endgroup\$
7
  • 2
    \$\begingroup\$ As for the second question, so long as it works as intended (aka, gives the correct output), there's almost no limitations aside from our standard loopholes \$\endgroup\$ Aug 19 at 13:07
  • \$\begingroup\$ That helps a lot, Thanks!! \$\endgroup\$
    – Jadefalke
    Aug 19 at 13:07
  • \$\begingroup\$ One more related thing. If my task is to print the string "Foo", I'd not be allowed to pass that string in through the lambda right? That'd be nonsensical. \$\endgroup\$
    – Jadefalke
    Aug 19 at 13:09
  • 2
    \$\begingroup\$ @Jadefalke no, your answer needs to be self-contained in that respect. \$\endgroup\$
    – pxeger
    Aug 19 at 13:09
  • 2
    \$\begingroup\$ I've removed the first question from the question, as that's the only duplicate. The second question is a legitimate question that I'm not sure has been asked before, so I've changed the focus of the question to that. Feel free to revert any changes you dislike \$\endgroup\$ Aug 19 at 13:14
  • 1
    \$\begingroup\$ I would say that the second shouldn't be allowed because although you're not using the value of f, you're taking advantage of its type. The input f shouldn't be mentioned anywhere in your lambda other than the parameter list because it's supposed to not even exist. \$\endgroup\$
    – rues
    Aug 19 at 13:16
  • \$\begingroup\$ I get your point. That was my struggle in the first place. The previous answer thought it was ok. I'm a little confused right now, especially because I just scored using this method. \$\endgroup\$
    – Jadefalke
    Aug 19 at 13:33
4
\$\begingroup\$

Writing f->{/*code*/} is absolutely alright, as agreed upon in the question Dude coinheringaahing linked.

However, I would say that the second shouldn't be allowed because although you're not using the value of f, you're taking advantage of its type. In my opinion, the input f shouldn't be mentioned anywhere in your lambda other than the parameter list because it's really not even supposed to exist - it's just meant to save a byte over ()->{...}.

\$\endgroup\$
3
  • \$\begingroup\$ Oh hang on, does Java require you to pass a string to the lambda for the answer to work? I was thinking from a JS perspective where it's work fine if you didn't pass anything. \$\endgroup\$ Aug 19 at 13:52
  • \$\begingroup\$ To the answer: I believe that solves it. I'm gonna go with that now and change my answer! \$\endgroup\$
    – Jadefalke
    Aug 19 at 13:53
  • \$\begingroup\$ @BrowncatPrograms Yes (or null, since strings are objects). \$\endgroup\$
    – rues
    Aug 19 at 14:11

You must log in to answer this question.

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