15
\$\begingroup\$

I'm arguing with the poster of this answer. The function literal as is doesn't cause the function to be defined, it has to be assigned to a variable, giving it a name or wrapping it like (function(n){...}(1)).

Here is the function literal in question:

function(n){for(var c=Math.pow(10,(''+Math.floor(n)).length),v=0,l;(l=v*v<n),c>1e-10;l!=v*v<n?c/=10:0)v*v<n?v+=c:v-=c;return v}

As I suggested in the comments, it would only take 2 bytes to make this into a proper function: replace function(n) with function s(n).

I personally don't consider this acceptable, as it doesn't make sense to me. And I haven't seen anyone else doing it so far. If it's allowed, however, it could a few bytes off some present, past and future golf submissions.

Edit: In meantime, the poster changed their code. I'd still be interested in people's opinion, though.

\$\endgroup\$
0

4 Answers 4

32
\$\begingroup\$

This is obviously a named function.

def g(h):
  return h * 2

However, not all functions are named. This is an anonymous function, which is still a function.

lambda h: h * 2

You may easily notice that you can assign the function.

g = lambda h: h * 2

However, even without this, the function is still a function. It doesn't matter if it's named or unnamed. It wastes bytes for something rather arbitrary.

Normally, the functions don't have to be named.

However, the question you linked to is a special case, and I agree that function should be named in this task. The reason is in question itself.

Define a function, s, which takes a number and returns the square root.

Because the task asks for particular function name, unnamed anonymous function is incorrect solution. Therefore, something like this wrong.

lambda x:x**.5

Instead, the function should be named, as asked.

s=lambda x:x**.5

My personal rule would be that if the task asks for particular function name, it shouldn't be anonymous. Otherwise, anonymous functions are perfectly fine, it's not that anyone cares about their name.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ +1, interesting approach. Also, now I see your point that when the question doesn't ask for it, it's not really necessary to use a named function or assign a lambda to a variable, because in some languages (e.g. C), function-only answers won't compile/execute alone anyway. \$\endgroup\$
    – user344
    Commented Apr 30, 2014 at 15:02
  • \$\begingroup\$ If your speak is general: you have to subtract the name of function names to the languages that have not anonymous function too... \$\endgroup\$
    – user58988
    Commented Dec 4, 2016 at 19:15
  • \$\begingroup\$ @RosLuP That complicates things for new users, and we're never going to be able to level the playing field completely for languages anyway. \$\endgroup\$ Commented May 3, 2017 at 3:47
5
\$\begingroup\$

My answer is yes. Function literals are functions. Functions need no names.

When a question asks, "Define a function t", I find no requirement to use the name t within the program. The name t allows the question to make requirements like, "t(0) must return 0". In the actual program, the function might have a different name. Also, t(0) might not be valid syntax to call the function.

Different languages have different rules for naming functions. In dc(1), every name is a single character, so we may make a t function, but not a triple function. In Common Lisp, t is a constant for the true value, so we may not make a t function.

If the function must have a name, then we have a question of scope, because not all languages use a single global namespace for all functions. Let us make a function s (not t) in Common Lisp to triple a value:

(flet((s(x)(* 3 x))))

What? The name s is local to the flet! We can't call this function! This s is good enough for me, because one may call it by inserting code inside the flet, before the last parenthesis. (For code golf, we can save 2 characters by doing (defun s(x)(* 3 x)), which defines s in the current package. If we want two named functions, then flet is shorter.)

Someone might attempt to do (set's(lambda(x)(* 3 x))), which sets the variable name s, not the function name s. Now we can't write (s 4). We must write (funcall s 4). It still counts as a function named s, I guess.

The removal of function names is a legitimate way to golf code. In Factor, why would I write : s ( x -- x ) 3 * ; and then [ s ] map when I can just write [ 3 * ] map?

A few extreme languages have no functions because they have no call stack or returns. I mean sed(1). (For evidence that sed is a programming language, see math.sed.) If a question asks for a function, my opinion is that you may not answer with a sed function. You may answer with a shell-script function that calls sed.

\$\endgroup\$
2
\$\begingroup\$

This is in some sense a specialisation of a more general issue: when the question's spec assumes that all answers will be in ALGOL-like languages, how should users of other languages handle it?

In this particular case, the question asks for "a function". But GolfScript doesn't have functions: it has blocks, which are anonymous pieces of code and can be given a name by storing them in a variable. J doesn't have functions: it has verbs, which are equally anonymous but can be given a name.

In the interests of fairness, the convention has been established (via comments; there may not be a corresponding meta post) that GolfScript answers to questions which request "a function" should give it a name and pop it: { BODY HERE }:f;. I'm not sure whether J has a similar convention: I have the impression that the J users have a stronger tendency to answer with anonymous verbs, but I don't want to look at lots of answers to check that.

I think that this convention should be extended to other languages with anonymous chunks of code: when a question asks for a function the answers should provide a named function or the closest equivalent. I would also favour editing questions (although not en masse, obviously, because an edit bumps a question) to replace requests for "a function" with requests for "a named function (or procedure, block, verb, etc. as appropriate to the language chosen)".

\$\endgroup\$
6
  • \$\begingroup\$ Off-topic, but out of curiosity, does an edit to an answer bump the question? \$\endgroup\$
    – user344
    Commented Apr 30, 2014 at 16:05
  • \$\begingroup\$ @nyuszika7h, yes, it does. \$\endgroup\$ Commented Apr 30, 2014 at 16:06
  • \$\begingroup\$ So I shouldn't be editing a lot of answers to add syntax highlighting then, okay. :P \$\endgroup\$
    – user344
    Commented Apr 30, 2014 at 16:07
  • \$\begingroup\$ In my opinion, which has some support (currently +10/-3), you shouldn't edit answers just to add syntax highlighting anyway. \$\endgroup\$ Commented Apr 30, 2014 at 16:10
  • \$\begingroup\$ I'll keep that in mind in the future, although I did get many such edits accepted recently. \$\endgroup\$
    – user344
    Commented Apr 30, 2014 at 16:11
  • \$\begingroup\$ Looks like 9 accepted, 6 rejected. \$\endgroup\$ Commented Apr 30, 2014 at 16:15
-7
\$\begingroup\$

For me anonymous functions are not valid. They have to add the name to the function.if they not add its name, something one can call, for me it is not valid, it is not, one complete answer. Not seem to me ok that for call one function one has to copy all the function as in {this is a function} {this is a function} argument

And not simply this f<-{this is a function}

f(argument)

\$\endgroup\$
3
  • 4
    \$\begingroup\$ The validity of anonymous functions is a well-established consensus. While your personal opinion on the matter may differ, valid isn't subjective; they are valid because the community decided that they are valid. \$\endgroup\$
    – Dennis Mod
    Commented Dec 4, 2016 at 21:10
  • \$\begingroup\$ i see that i'm alone in think that... it is only for my little vote: i not vote anonimous functions \$\endgroup\$
    – user58988
    Commented Dec 4, 2016 at 21:27
  • \$\begingroup\$ I think your argument assumes limitations on languages which don't apply universally. \$\endgroup\$ Commented Dec 5, 2016 at 15:16

You must log in to answer this question.