# Tips for golfing in LiveScript

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.

• Agh. That’s a really annoying name for a language. JavaScript was LiveScript originally.
– Ry-
May 7 '14 at 16:19
• @minitech It was actually in part inspired by JavaScript's former name. Aug 24 '14 at 22:38

# 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


# Positional function arguments

In LiveScript, & is a shorthand for arguments. arguments[0] can be shortened to &[0], which can be shortened to &.0, which in turn can be shortened to &0.

Before:

f=(x,y)->x+y


After:

f=->&0+&1


# .n over [n], \n over ['n'], etc.

This won't work for computed indices and members, but with static ones, it shaves bytes. Examples:

f[0]
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, null, void, etc.

# 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


# it instead of explicit function argument

If you have a function with a single argument and you only refer to it at most twice, use it instead.

Before:

f=(x)->x+x


After:

f=->it+it

• I know this isn't the point, but this specific example would be best written as f=(*2). Aug 24 '14 at 23:28

# Short form of switch

Use | instead of case and |_ instead of default. Also, pipes themselves terminate statements.

Before:

switch x;case 1=>f;case 2=>g;default=>h


After:

switch x|1=>f|2=>g|_=>h


Avoid explicit commas and dots

This:

foo(1, 2, 3).bar


can be just:

foo(1 2 3)bar


Use the shortest function syntax possible

this:

->&0+&1


can be written:

(+)


and this:

->it.foo - it.bar


can be written:

(.foo- it.bar) # notice the dash position

• I just wrote a more detailed answer on this (and the goal is to use the shortest number of bytes) Aug 24 '14 at 23:00