tags:

views:

87

answers:

2

Hi,

Is there any syntax/package allowing quick filling of java arrays with ranges of numbers, like in perl?

e.g.

int[] arr = new int[1000];
arr=(1..500,301..400,1001..1400); // returns [1,2,3,4,...,500,301,302,...,400,1001,1002,...1400]

Also, it here a package that allows getting the n-th number in such list of numbers as the above, without actually creating the array (which can be huge)?

e.g.

BunchOfRangesType bort = new BunchOfRangesType("1..500","301..400","1001..1400");
bort.get(0); // return 1
bort.get(500); // return 301
bort.get(501); // return 302

It's not too difficult to implement, but I guess it might be common so maybe it was already done.

+7  A: 

There is dollar:

// build the List 10, 11, 12, 13, 14
List<Integer> list2 = $(10, 15).toList();
True Soft
Whoow!! Never seen this before! Never... And this is Java. Interesting...
Martijn Courteaux
Is it available as a maven dependency? I can't find anything like that..
f1sh
I can't find anything like this either. This is only valid in Java if you define the $ method yourself.
Erick Robertson
After further review, it appears that "dollar" is a library which defines the $ method as a static method of a Dollar class. With the link dead, it's nearly impossible to actually find the code to this. So this answer is pretty useless. Full java syntax would require calling this as Dollar.$(10, 15).toList().
Erick Robertson
@Erick, yes, `$` is a method. What do you mean by *'the link dead'*? The source code for this library is available, too.
True Soft
@Erick - not if you `import static com.humaorie.dollar.Dollar.*`.
Andrzej Doyle
@True Soft: The bitbucket.org link is dead. So currently, nothing is available. @Andrzej: Just because you can, doesn't mean you should. At issue is code clarity. No performance is gained by doing that, just code obfuscation.
Erick Robertson
The link is ok. I mean, if I click on the link in my answer, I go to that page.
True Soft
@True link works fine here too
Jared Russell
+3  A: 

Not quite as clean as True Soft's answer, but you can use Google Guava to the same effect:

public class Test {

    public static void main(String[] args) {
        //one liner
        int[] array = toArray(newLinkedList(concat(range(1, 10), range(500, 1000))));

        //more readable
        Iterable<Integer> values = concat(range(1, 10), range(500, 1000));
        List<Integer> list = newLinkedList(values);
        int[] array = toArray(list);

    }

    public static List<Integer> range(int min, int max) {
        List<Integer> list = newLinkedList();
        for (int i = min; i <= max; i++) {
            list.add(i);
        }

        return list;
    }

}

Note you need a few static imports for this to work.

Jared Russell
Even if more verbose, that's much much clearer than the dollar approach.
Noel M
I don't think static imports are very clean. I would prefer a custom implementation than to do this.
Erick Robertson
@Erick "I don't think static imports are very clean." You can of course use the fully qualified method names, however using the static imports does make the code less verbose.
Jared Russell
Verbosity can improve readability, which is desired. I always reference the class name when calling static methods. I want to make it as easy as possible for the next programmer to understand. I don't see a benefit to making the code less verbose, or compact enough to fit in one line. These things do nothing to improve performance, and only obfuscate the code.
Erick Robertson