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.