11
\$\begingroup\$

In Java, all "basic" input types are immutable with very few tools at our disposal. Some other class that still represent the same value exist and allow us to do more.

In trying to golf Java code, I always check if StringBuilder/StringBuffer can help me. In this case, a StringBuilder still represents the Java String type. They're useful, but very verbose. Can we use them as input parameters directly?

Given the following code:

void blah(String s) {
  StringBuilder t = new StringBuilder(s);
  // do something super golfy
}

May I answer a golfing question with such code?

void blah(StringBuilder t) {
  // do something super golfy
}

In another instance, the Java array type exists (examples: String[]), but in some cases, it might be shorter to use a java.util.stream.Stream as input parameter.

Before:

void blah(String[]a){
  Stream<String> s = java.util.Arrays.stream(a);
  // do something super golfy
}

After:

void blah(java.util.Stream<String>s) {
  // do something super golfy
}

I guess that the question can be boiled down to:

May I transform my objects to other representations of the same objects before passing them as input parameters to the golfed function?

If I have to provide a list of elements I'd like to be found acceptable as input parameters, here is it: (when no package, assume java.lang, just like normal Java code)

  • basic types (primitives + String + arrays): String, int, long, double, String[], int[], etc.
  • Mutable/Helper: StringBuilder, StringBuffer, Integer, Long, Double, etc.
  • IO: java.io.Reader, java.io.BufferedReader, java.io.InputStream, etc.
  • Collections: Iterable, java.util.Iterator, java.util.Collection, and friends.
  • Streams: java.util.stream.Stream, java.util.stream.IntStream, etc.
\$\endgroup\$
  • \$\begingroup\$ Related reading \$\endgroup\$ – Geobits Sep 15 '16 at 13:09
  • \$\begingroup\$ To clarify the difference between @Geobits' linked reading and this question: the link is about different values, while this question is about different representations of the same values, and specifically for Java. \$\endgroup\$ – Olivier Grégoire Sep 15 '16 at 13:31
  • \$\begingroup\$ Related \$\endgroup\$ – Peter Taylor Sep 16 '16 at 7:21
5
\$\begingroup\$

As long as your transformation doesn't add any additional information to the input than what would be present normally (for a String, that information would be the length of the string and the sequence of characters that form the string), it should be fine. You should check with challenge authors on specific cases, though, as they may decide that a given transformation would trivialize the challenge too much to be acceptable.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .