3
\$\begingroup\$

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.

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

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
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

.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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ I know this isn't the point, but this specific example would be best written as f=(*2). \$\endgroup\$ – Isiah Meadows Aug 24 '14 at 23:28
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Avoid explicit commas and dots

This:

foo(1, 2, 3).bar

can be just:

foo(1 2 3)bar
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ I just wrote a more detailed answer on this (and the goal is to use the shortest number of bytes) \$\endgroup\$ – Isiah Meadows Aug 24 '14 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.