What general tips do you have for golfing in LiveScript? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to LiveScript (e.g. "remove comments" is not an answer). Please post one tip per answer.
Take advantage of curried functions and Prelude.ls
If the function is already curried, it can be much shorter overall. Also, Prelude.ls is the standard library, and its functions are often very useful. This means, unless otherwise specified, this library is 100% allowed. Examples:
f=->[1 to &0].reduce (*) # space required f=->product [1 to &0] s=(.reduce (+)) s=sum # better yet, avoid defining this when possible d=(.map (*2)) d=map (*2) # Sometimes even shorter than native methods r=->&0.reduce (-&1-) r=->fold1 (-&1-),&0 # Equal if starting value is required. a.=sort! a=sort a # sometimes the same length a.=sort ->&1.length-&0.length # space before arrow required a.=sort over (-),(.length) # slightly more functional a=sortBy (.length) # specific builtin, always prefer CamelCase
This won't work for computed indices and members, but with static ones, it shaves bytes. Examples:
f f.0 f['str'] f\str f['str with spaces'] f'str with spaces'
This will not work with other literals, such as RegExps, booleans,
Use partial functions when possible (with discretion)
Partial functions can drastically shorten many operations. Examples:
f=->it^2 f=(^2) s=->&0.length==&1.length s=over(==),(.length)
It isn't always better, though. Example:
# [[a, b], [c, d], [e, f]] -> (a+b)*(c+d)*(e*f) f=(|>map sum|>product) f=product map sum # these are actually curried