6
\$\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.

asked May 6, 2014 at 18:41
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Agh. That’s a really annoying name for a language. JavaScript was LiveScript originally. \$\endgroup\$ Commented May 7, 2014 at 16:19
  • 1
    \$\begingroup\$ @minitech It was actually in part inspired by JavaScript's former name. \$\endgroup\$ Commented Aug 24, 2014 at 22:38

8 Answers 8

2
\$\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
answered Jul 1, 2014 at 11:29
\$\endgroup\$
1
\$\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
answered Jun 23, 2014 at 11:12
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I know this isn't the point, but this specific example would be best written as f=(*2). \$\endgroup\$ Commented Aug 24, 2014 at 23:28
1
\$\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
Claudia
1,7081 gold badge18 silver badges31 bronze badges
answered Jun 23, 2014 at 11:17
\$\endgroup\$
1
\$\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
answered Aug 24, 2014 at 23:25
\$\endgroup\$
0
\$\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.

answered Aug 24, 2014 at 22:47
\$\endgroup\$
0
\$\begingroup\$

Avoid explicit commas and dots

This:

foo(1, 2, 3).bar

can be just:

foo(1 2 3)bar
answered Aug 24, 2014 at 22:55
\$\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
answered Aug 24, 2014 at 22:58
\$\endgroup\$
1
  • \$\begingroup\$ I just wrote a more detailed answer on this (and the goal is to use the shortest number of bytes) \$\endgroup\$ Commented Aug 24, 2014 at 23:00
0
\$\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
answered Aug 24, 2014 at 22:58
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.