Tips for golfing in Proton

4

0

Proton is a quite new language created by PPCG user Hyper Neutrino.

It is very similar to Python, and has similar syntax to the language Cheddar, hence there are some clever tricks you can use in order to reduce your byte count. Let's get those cool tricks listed here. As usual, please post one tip per answer and avoid obvious things (e.g. remove spaces). If you have any further questions about any of the tips listed, there is a dedicated chat room for Proton.

Mr. Xcoder

Posted 2017-09-12T18:27:45.547

Reputation: 39 774

2

To any potential close voters, tips questions are perfectly on-topic here.

– caird coinheringaahing – 2017-09-14T14:08:14.940

Answers

3

Use currying when you need a lambda with 2 arguments

If you want a function with 2 arguments, currying syntax always saves 1 byte. For example:

f=(a,b)=>a+b

Is one byte longer than:

f=a=>b=>a+b

The first function can be called with f(number_1, number_2), whilst the latter is called using f(number_1)(number_2).

Try it online!

Mr. Xcoder

Posted 2017-09-12T18:27:45.547

Reputation: 39 774

2

Take advantage of the fun features: operator functions

(+) for all binary operators where the + is evaluates to a function that performs the exact same task as the operator itself. Where you'd use int.__add__ when golfing in Python, use (+) for many bytes saved.

HyperNeutrino

Posted 2017-09-12T18:27:45.547

Reputation: 26 575

1

Use ..-ranges

Instead of using range(lower_bound, upper_bound), use lower_bound..upper_bound. For unary ranges, use 0..number instead of range(number). Some examples:

range(1,10)

Can be replaced by (-6 bytes):

1..10

And:

range(10)

Can be replaced by (-4 bytes):

0..10

Mr. Xcoder

Posted 2017-09-12T18:27:45.547

Reputation: 39 774

1

Use inline-assignment

Consider the following two:

a=>(a+5)*2/(a+5)
a=>(b=a+5)*2/b

You can use inline assignments (where you treat an assignment as an expression) in Proton. The value given will be the right side of the assignment. This can sometimes help you take off a few bytes; more the longer the repeated expression, and especially if it has brackets already (because you need the brackets around the expression).

HyperNeutrino

Posted 2017-09-12T18:27:45.547

Reputation: 26 575

1

Use point-free notation

Even though lambda notation has a much shorter overhead than Python (a=> vs lambda a:), point-free notation is still often shorter, where you define a function as a composition of other functions. For example, consider the following chain of golfing:

a=>b=>dict(a)[b]
a=>dict(a).get
dict+((.)&get)

The last one currently isn't any shorter than the second last, but it does become shorter in some cases, for example, this:

a=>b=>[dict(a).get(f)for f:b]
a=>b=>map(dict(a).get,b)
a=>map(dict(a).get)
dict+((.)&get)+map

You can get some pretty short code with that. a & b will return a function *q => a(*q, b) if a is a function and otherwise *q => b(a, *q) if b is a function, and a + b will return a function *q => b(a(*q)).

HyperNeutrino

Posted 2017-09-12T18:27:45.547

Reputation: 26 575