I recently got into a golf kata that asked to check if a letter is uppercase or not in no more than 9 characters.
The users answers were kind of scary (those example also works with > in reverse order):
f=s=>s<{}
f=c=>c<{}
f=_=>_<f
f=Z=>Z<f
Some variable names don't works with f:
f=a=>a<f
f=z=>z<f
Why and how does < and > operators works when comparing functions to basic strings? Maybe pointers comparison?
Try by yourself:
// working
f1=s=>s<{}
f2=c=>c<{}
f3=_=>_<f3
f4=Z=>Z<f4
// not working
f5=s=>s<f5
console.log('f1', f1('A'))
console.log('f1', f1('a'))
console.log('f2', f2('A'))
console.log('f2', f2('a'))
console.log('f3', f3('A'))
console.log('f3', f3('a'))
console.log('f4', f4('A'))
console.log('f4', f4('a'))
console.log('f5', f5('A'))
console.log('f5', f5('a'))
Comparing a string to {} is effectively comparing the string to the results of ({}).toString(), which is always "[object Object]". The important part is the [ character, which is the first code point after the last upper-case letter (Z). Thus, if you know that the input string is some letter, comparing to a string beginning with [ will tell you that it's an upper-case letter.
To be clear,
s => s<{}
is effectively
s => s<"[object Object]"
It has nothing to do with variable names chosen when you use an object ({}), but it's important for function-reference versions. In those, the "_" is important because it's a character after "Z" but before all the lower-case letters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With