beanshell2 icon indicating copy to clipboard operation
beanshell2 copied to clipboard

collection extensions

Open GoogleCodeExporter opened this issue 10 years ago • 7 comments

I suggest adding 2 commands:

List list(Object... objs) {
    return new ArrayList(Arrays.asList(objs));
}

Map map(Map.Entry... entries) {
    Map map = new HashMap();
    for( Map.Entry entry : entries ) {
        map.put( entry.getKey(), entry.getValue() );
    }
    return map;
}

And I suggest adding one binary operator => which would create a Map.Entry
.  So now we could do things like:

numbers = list( 1, 2, 3 );
numberNames = map( 1 => "one", 2 => "two", 3 => "three" );

Original issue reported on code.google.com by [email protected] on 9 May 2007 at 2:19

GoogleCodeExporter avatar Apr 22 '15 19:04 GoogleCodeExporter

Very good idea.  this would be very useful.  

However I suggest that language enhancements, espeically ones that dont conform 
to 
java specs, be either defered or in a new fork/branch.


Original comment by [email protected] on 9 May 2007 at 12:42

GoogleCodeExporter avatar Apr 22 '15 19:04 GoogleCodeExporter

I can see how this syntax would be useful and convenient.  However, Beanshell 
is a
java-like language, and I don't think that it is such a good idea to be adding
non-java syntax.  

Original comment by [email protected] on 29 Mar 2008 at 1:29

GoogleCodeExporter avatar Apr 22 '15 19:04 GoogleCodeExporter

I agree that adding a lot of non-java syntax is a bad idea.  But the 2 global 
methods
I suggested here, list() and map(), are possible without any syntax additions 
as long
as we have varargs, which is part of java since jdk 1.5 .  The only addition to
syntax which is suggested is a "=>" binary operator to create Map.Entry 
objects.  And
this is very minor addition, just one more binary operator.

Original comment by [email protected] on 29 Mar 2008 at 5:36

GoogleCodeExporter avatar Apr 22 '15 19:04 GoogleCodeExporter

Perhaps another command ("pair(key, value)") would be sufficient?

Than you could do

 numberNames = map(pair(1, "one"), pair(2, "two"), pair(3, "threee"));

No new symbols ;)

Original comment by [email protected] on 21 Feb 2011 at 11:25

GoogleCodeExporter avatar Apr 22 '15 19:04 GoogleCodeExporter

That works but maybe entry is better than pair() as in:

 numberNames = map(entry(1, "one"), entry(2, "two"), entry(3, "three"));

Original comment by [email protected] on 21 Feb 2011 at 3:47

GoogleCodeExporter avatar Apr 22 '15 19:04 GoogleCodeExporter

> numberNames = map(entry(1, "one"), entry(2, "two"), entry(3, "three"));

vote +1

Original comment by [email protected] on 2 Mar 2011 at 8:11

GoogleCodeExporter avatar Apr 22 '15 19:04 GoogleCodeExporter

> numberNames = map(entry(1, "one"), entry(2, "two"), entry(3, "three"));

vote +1

numbers = list( 1, 2, 3 );

vote +1

Original comment by [email protected] on 29 May 2011 at 9:42

GoogleCodeExporter avatar Apr 22 '15 19:04 GoogleCodeExporter