Tips for golfing in Pike

3

Pike is a dynamically typed interpreted scripting language, heavily influenced by C, which typically compiles to C99 with a few #defines. (It even has a preprocessor!)

What general tips do you have for golfing in Pike? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Pike (e.g. "remove comments" is not an answer).

cat

Posted 2016-02-29T03:50:25.930

Reputation: 4 989

Answers

1

Array operators.

Pike's arrays have some operators that act ... weird.

  • ({1})+({2}) returns ({1,2}) (concatenation)
  • ({1,3,8,3,2}) - ({3,1}) returns ({8,2}) (the former argument without elements in the latter argument)
  • ({1,3,7,9,11,12}) & ({4,11,8,9,1}) will return: ({1,9,11}) (intersection)
  • ({1,2,3}) | ({1,3,5}) will return ({1,2,3,5}) (union)
  • ({1,3,5,6}) ^ ({4,5,6,7}) will return ({1,3,4,7}) (symmetric difference)
  • ({1,2,3,4,5})/({2,3}) will return ({ ({1}), ({4,5}) }) (split at occurences)
  • ({1,2,3,4})/2 will return ({ ({1,2}), ({3,4}) }) (split by length, truncates the excess.)
  • ({1,2,3,4,5})/2.0 will return ({ ({1,2}), ({3,4}), ({5}) }) (split by length, keeps the excess.)
  • ({1,2,3,4,5})%3 will return ({4,5}) (excess of split by length).

Zacharý

Posted 2016-02-29T03:50:25.930

Reputation: 5 710

1

Abuse mixed.

Pike is unfortunately not exactly identical to C; for instance the type of a list of strings is array(string), not char[][] or even string[].

Any time you need a type declarator and it's not int, mixed (the "any" type, which gets promoted to the actual type of the RHS at runtime) is liable to be shorter.

A function that takes a list of ints and sorts it, then returns it, for a whopping 41 bytes:

array(int)s(array(int)l){return sort(l);}

Or, the type-inferencing version, which is 31 bytes:

mixed s(mixed l){return sort(l);}

My answer to Manage Trash So uses a lot of mixed.

cat

Posted 2016-02-29T03:50:25.930

Reputation: 4 989