Topic: Drum roll - twenty liners rules discussion |
|
---|---|
Author | Thread |
Paranoid (IV) Inmate From: Switzerland |
posted 01-23-2008 08:49
Hey, people, just a tip about communication to begin with : |
Paranoid (IV) Inmate From: Umeå, Sweden |
posted 01-23-2008 10:02
I think the question about chained functions is should be quite easy to settle: code: function f(someval){ return somecodedoingsomethingwithsomeval; } var finalval=f(initialval); is equivalent to code: var finalval=function(someval){ return somecodedoingsomethingwithsomeval; }(initialval);
|
Paranoid (IV) Inmate From: Norway |
posted 01-23-2008 10:21
That was my impression, but using the first formulation makes it clearer for everyone. Let's call this a tip for future N liners. |
Paranoid (IV) Inmate From: Switzerland |
posted 01-23-2008 11:49
What about "forming those tips in a very simple and readable way" for further reference? |
Paranoid (IV) Inmate From: Norway |
posted 01-23-2008 18:04 |
Paranoid (IV) Inmate From: Switzerland |
posted 01-23-2008 23:10 |
Paranoid (IV) Inmate From: cell 3736 |
posted 01-24-2008 13:37
quote:
code: if (!myFunctionA() && !myFunctionB() && !myFunctionC() && !myFunctionD() && !myFunctionE()) {}
code: function f(m) { document.write(m); } ((condition) && !f("1") && !f("2"))?f("3"):f("4");
code: ((condition) && !f("1") && !f("2") || (f("4") || f("5")))?f("3"):f("6");
|
Bipolar (III) Inmate From: Cranleigh, Surrey, England |
posted 01-24-2008 14:28
Hmmm...I think it would have to be...what about: code: function f(m) { eval(m); } ((condition) && !f("alert('hello')") && !f("alert('Rule')"))?f("alert('abuse!')"):f("alert('....twice!')");
|
Paranoid (IV) Inmate From: Umeå, Sweden |
posted 01-24-2008 17:46
Funny, I was going to bring up that chaining of function calls through the shortcut evaluation operators. Yes, I think we should only count it as one line, because it would be ridiculous to make so intricate rules that you actually prevent this atrocity from being counted as a single line while still making sensible one liners using the shortcut evaluators count as only one line. It opens up for some potential exploitation of the rules, yes, but I can't recall the rules dealing with it any better earlier. Anyway, I think there's some things that aren't worth detailing, where it's better to simply go by a case-by-case discussion. code: var a=[null,'param1,param2,param3','alert(param1);alert(param2);alert(param3);return function(){return [param1,param2,param3];}'], s=Function.prototype.call.apply(Function,a)('This','is','madness!')().join(' '); It's a two liner, right? One line is an array initiator, one line is a complex series of function calls. |
Paranoid (IV) Inmate From: Norway |
posted 01-24-2008 18:50
liorean: that piece of code is pure madness |
Bipolar (III) Inmate From: Cranleigh, Surrey, England |
posted 01-24-2008 19:42 |
Paranoid (IV) Inmate From: Umeå, Sweden |
posted 01-24-2008 19:56
Well, old example of course: code: function f(lop){ return function(rop){ return Number(lop)+Number(rop); }; } var n=f(1)(2); I think it should count as three lines. It could be used with an arbitrary but finite depth using some old Scheme tricks, but you still pay at least one line per level and one for the initial invocation. |
Paranoid (IV) Inmate From: cell 3736 |
posted 01-24-2008 20:32
Maybe the best way would be to not think about rules too much If someone uses a questionable technique let it stay on his/her conscience. |
Nervous Wreck (II) Inmate From: |
posted 01-24-2008 21:20
I concur with Arthurio. Line counting isn't in the spirit of the contest -- it's all about doing fun stuff with little code. |
Paranoid (IV) Inmate From: Norway |
posted 01-24-2008 21:47
wrayal: Actually I never used jQuery, or any JS framework/library, but from the time I was coding in C/C++ I always liked chained calls and that a big part of jQuery's syntax. It ends up with things like: code: $("p.surprise").addClass("ohmy").show("slow"); 3 function calls, 1 line code: $("#orderedlist").find("li").each(function(i) { $(this).append( " BAM! " + i ); }); 5 function calls, 2 lines code: $("dd:not(:first)").hide(); $("dt a").click(function(){ $("dd:visible").slideUp("slow"); $(this).parent().next().slideDown("slow"); return false; }); 10 functions calls + a return, 5 lines |
Paranoid (IV) Inmate From: Umeå, Sweden |
posted 01-24-2008 21:58
Poi: Well, actually in the 20 liner you'd have not only the function calls, but the actual implementation code as well. Now tell me those chained 10 function calls aren't actually many more lines if you count the implementation behind them |
Paranoid (IV) Inmate From: cell 3736 |
posted 01-24-2008 21:58
This is my opinion: |
Paranoid (IV) Inmate From: Norway |
posted 01-24-2008 22:04 |
Paranoid (IV) Inmate From: Umeå, Sweden |
posted 01-24-2008 22:44
Poi: Built-ins or host functions, you mean For the use of the word "native" that the ECMAScript spec uses, a native function is one defined using ECMAScript, not one that is part of the host environment. |
Paranoid (IV) Inmate From: Norway |
posted 01-24-2008 23:17
quote: Agreed. code: /* */ function fn( func ) /* */ { /* C */ return function() /* */ { /* D */ return func.apply( this, arguments )||this; /* */ } /* */ } /* A */ for( chainThat in {methodA:1,methodB:1} ) /* */ { /* B */ anObject.prototype[chainThat] = /* E */ fn( anObject.prototype[chainThat] ); /* */ } Can be used to make native methods, on a same type of object/element, chainable |
Paranoid (IV) Inmate From: Umeå, Sweden |
posted 01-25-2008 03:46
Only if they propagare the this value, or at least propagate an object of the same type. Most built-ins don't, which is why I was stating quote: Of course, you get around that since you can apply it to all object types. (Still breaking on null or undefined return values, though...) |
Paranoid (IV) Inmate From: Norway |
posted 01-25-2008 09:27
quote: Whic is the very purpose of the code snippet I'm talking about : To "extend" some native functions and have them propagate this when they don't return anything ( or anything assimilated to false ) |
Paranoid (IV) Inmate From: Switzerland |
posted 01-26-2008 16:22
quote:
|