3
\$\begingroup\$

following this meta codegolf stackexchange post, I've stopped using declarations for lambda functions, but one problem this creates is that since you're only inputting the lambda body and not its header, you can't run it in try it online. What would be your suggestions to solve this? can we ignore whatever it says about the bytes on the tio website and just mind whatever the answer says?

edit: I personally use kotlin

\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

In Javascript, you can put f= into the header, the lambda in the body, and then call f in the footer to test:

JavaScript (Node.js), 6 bytes

x=>x+1

Try it online!

In Python, you can put f=\ into the header and then do the same (you need the backslash to escape the newline):

Python 3, 12 bytes

lambda x:x+1

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ this answers it. thanks \$\endgroup\$ Commented Jan 28, 2022 at 16:33
  • \$\begingroup\$ i got another question and I'd be happy if you can answer, currently, i have a lambda function for kotlin which works, however it requires the math package of kotlin, but the problem is, i can't include it in the body as it already has a header (lambda definition) so the only place that I can put the import is in the header, which doesn't count for the byte count. what should i do in this case? \$\endgroup\$ Commented Jan 30, 2022 at 12:24
5
\$\begingroup\$

Not all languages allow the sort of formatting that is permitted by Javascript and Python.

For example in Haskell which does not have a way to start a function on a newline without significantly modifying the behavior of the compiler I add an extra assignment to the body f= but remove it from my answer and the byte count.

Haskell, 7 bytes

map(+1)

Try it online!

If you follow the link to the TIO you will see that there are an extra 2 bytes at the beginning. I often also remove the code formatting since it tends not to like anonymous functions.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Same thing with e.g K \$\endgroup\$
    – emanresu A
    Commented Jan 28, 2022 at 19:00
  • \$\begingroup\$ Same thing with the current Language of the Month, tinylisp. \$\endgroup\$
    – DLosc
    Commented Feb 1, 2022 at 16:48
  • 1
    \$\begingroup\$ You actually can separate out the f= assignment in Haskell if you use the -cpp flag: Try it online! \$\endgroup\$
    – xnor
    Commented Feb 2, 2022 at 0:55
  • \$\begingroup\$ @xnor That's what I meant by "without significantly modifying the behavior of the compiler". The -cpp flag does a lot of other stuff too. \$\endgroup\$
    – Wheat Wizard Mod
    Commented Feb 6, 2022 at 0:00
  • \$\begingroup\$ @WheatWizard Is there anything with it that affects how a program I write for golf might run? I've just been putting it in all my TIO Haskell code. \$\endgroup\$
    – xnor
    Commented Feb 6, 2022 at 1:39

You must log in to answer this question.

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