15
From what I've seen throughout my time here on PPCG, most JavaScript entries involving fat arrow functions tend to be one of two camps:
The simple ones that are capable of running as a single statement and returning an answer, straight off the bat, like
x=(a,b)=>a*a+b
The more complex ones that usually have curly braces because of the use of loops, and as a result require the use of a
return
statement.. likep=b=>{m=b;for(a=1;~-m;)--m,a*=m*m;return a%b}
Taking the above example from category 2 with the curly braces concept as proof-of-concept... Would there be a way to re-golf this code (or similar) like this so as to eliminate the curly braces as well as the return
? I'm only asking this as this could potentially (not saying this will happen all the time) eliminate 8 bytes from a JS golfer's code. Are there any techniques that one could use in this instance? I've tried recursion, but the m=b
statement has proven to be a bit of a bugbear, as I can't seem to shake it.
For the above code, how would one golf that further so as to eliminate the return
statement, regardless of whether it golfs shorter or not?
1You've missed a big saving in simplifying the original code.
~-m
ism-1
, so the loop can befor(m=b,a=1;--m;a*=m*m)a%b;
and the recursive version can be (untested)b=>(f=a=>--m?f(a*=m*m):a%b)(1,m=b)
– Peter Taylor – 2016-08-08T06:23:21.4301Sometimes you just have to use a different algorithm but in this case the best I could do was the same length as @PeterTaylor's answer:
b=>b>1&(f=a=>--a<2||b%a&&f(a))(b)
– Neil – 2016-08-08T09:24:11.750