Tips for golfing in Cheddar

12

2

Cheddar

Cheddar is a high-level, functional + object-oriented programming language created by our user Downgoat designed to make programming easier, faster, and more intuitive.

What general tips do you have for golfing in Cheddar? I'm looking for ideas which can be applied to problems and which are also at least somewhat specific to Cheddar (e.g. "Remove unnecessary whitespace." is not an answer).

Leaky Nun

Posted 2016-08-02T06:46:01.963

Reputation: 45 011

Answers

3

Use Functionized Properties

If you ever use just a property in a function:

A->A.long.property(n).foo.bar

You can use functionized properties to save some bytes:

@.long.property(n).foo.bar

You can reference the variable in the functionized property with $0.

Downgoat

Posted 2016-08-02T06:46:01.963

Reputation: 27 116

2

Use the String Operator for string sequences

I'm talking about the @" operator which does different things, all of which deal with strings. This has quite a bit of uses but this is one of my favorite uses:

Take a look at this:

@" [103, 111, 97, 116, 115] === "goats"

not that useful but the opposite is:

@"'goats' === [103, 111, 97, 116, 115]
'goats'.bytes // compare the above too

This is especially useful to generate the alphabet:

65@"90    // Uppercase Alphabet
97@"122   // Lowercase Alphabet
65@"90+97@"122 // Both cases
String.letters // Compare 97@"122 to this

Downgoat

Posted 2016-08-02T06:46:01.963

Reputation: 27 116

1

Curry

No not red curry (what other curry would you be thinking about ¬_¬). I mean this type of curry:

a->b->

If you have a function taking two arguments. It's shorter to curry than to not:

(a,b)->
a->b->

Note: This is only shorter when you have exactly two arguments.

Downgoat

Posted 2016-08-02T06:46:01.963

Reputation: 27 116

1

Use default arguments

Declaring Cheddar variables can be quite the byte-waster:

->{var a=b+1}

luckily, you can (ab)use function default values for creating variables:

b=a+1->b

Here are some examples of uses:

let f= (a,b=a+1)->b
f(5) // 6

let f= a=Math.rand()->a
f() // 0.8757450950797647

Downgoat

Posted 2016-08-02T06:46:01.963

Reputation: 27 116

1

Use Functionized Operators and Bonding

This is a simple one. If you have anything like:

i->1+i

or any similar operation. You can shorten using functionized operators + bonding:

1&(+)

Downgoat

Posted 2016-08-02T06:46:01.963

Reputation: 27 116

1

Use the mapping operator

The => maps LHS to RHS, due to it's precedence, this also means you can use it with ranges and use it multiple times:

a=>f
(a).map(f)

Additionally:

a=>f=>g           // This is equivilant to...
(a).map(f).map(g) // this

Downgoat

Posted 2016-08-02T06:46:01.963

Reputation: 27 116