你对在LiveScript打高尔夫球有什么一般的建议?我正在寻找一些可以应用于代码高尔夫问题的想法,这些问题至少在一定程度上是针对LiveScript的(例如,“删除注释”不是一个答案)。请给每一个答复发一条小费。
发布于 2014-08-24 23:25:52
如果该函数已经被匆忙处理,则其总体上可能会更短。另外,Prelude.ls是标准库,它的功能通常非常有用。这意味着,除非另有规定,这个库是100%允许的。示例:
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发布于 2014-08-24 22:47:11
.n etc [n],\n etc ['n']等这不适用于计算的索引和成员,但是对于静态的索引和成员,它可以减少字节数。示例:
f[0]
f.0
f['str']
f\str
f['str with spaces']
f'str with spaces'这将不适用于其他文字,如RegExps、booleans、null、void等。
发布于 2014-08-24 22:58:34
部分函数可以大大缩短许多操作。示例:
f=->it^2
f=(^2)
s=->&0.length==&1.length
s=over(==),(.length)不过,情况并不总是更好。示例:
# [[a, b], [c, d], [e, f]] -> (a+b)*(c+d)*(e*f)
f=(|>map sum|>product)
f=product map sum # these are actually curriedhttps://codegolf.stackexchange.com/questions/26715
复制相似问题