5
\$\begingroup\$

Example:

Write a function which returns number passed into it as an argument.

Solution:

f=x=>x

What is its length?
Is it 4 as a length of the lambda x=>x only or 6 as a length of whole statement with assignment?

What if there are 2 functions?

g=x=>x/2
f=x=>g(x)+g(x)
\$\endgroup\$

2 Answers 2

18
\$\begingroup\$

Unless the challenge explicitly asks for a named function unnamed functions are completely legitimate. That means you don't count the assignment either. So your example

f=x=>x

would be four bytes, and I wouldn't even include f= in the post, so that it's clear how I've counted.

What if there are two functions? That depends. If the questions actually asks for two functions (e.g. a decoder and an encoder), count them separately just like you would count a single function. This means that neither assignment would be counted. For instance, if your two submissions were

f=x=>x*x
g=x=>sqrt(x)

I'd count that as 6 + 10 = 16. Again I would not actually write f= and g= in the post.

However, if you define one function to be used in another, like you did in your example, then the first assignment definitely needs to be counted. Your f wouldn't work without the assignment, because it references g by name. So I would count the assignment to g but not the assignment to f. I would present the solution like this:

g=x=>x/2
x=>g(x)+g(x)

and therefore count it as 21 bytes.

Likewise, if a function is recursive, you must also count the assignment:

f=x=>x&&f(x-1)

Would count as 14 bytes since the function wouldn't work unless f was defined.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I not agree, the function has to be easy utilizable so has to have its name \$\endgroup\$
    – user58988
    Commented Dec 4, 2016 at 18:54
2
\$\begingroup\$

Apparently anonymous functions are the norm, so you don't count the assignment

The length of the lambda is the same as with a full program: the length of your function is the number of characters including the assignment. You want something that can be inserted as a line, not proceeding or preceding any other code on that line.

So, in Python:

f=lambda x:x*2

Is 14 bytes and:

f=lambda x:x*2
g=lambda y:y*3

is 29 bytes.

This is akin to the language handicaps: keep it simple and easy to police.

\$\endgroup\$
3
  • \$\begingroup\$ "the length of your function is the number of characters within the code block" - but you can place lambla witout assignment iside of code block. I so a lot of ansers with such way, so desided to ask. \$\endgroup\$
    – Qwertiy
    Commented Sep 8, 2015 at 11:01
  • \$\begingroup\$ @Qwertiy Edited \$\endgroup\$
    – Beta Decay
    Commented Sep 8, 2015 at 11:02
  • \$\begingroup\$ The latter has a newline, so it would be 29. But if anonymous functions are allowed for the question (they seem to be by default), then lambda x:x*2 is 12. \$\endgroup\$
    – Sp3000
    Commented Sep 8, 2015 at 11:23

You must log in to answer this question.

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