M201370367 / beanshell2

Automatically exported from code.google.com/p/beanshell2
0 stars 1 forks source link

collection extensions #14

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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 fschm...@gmail.com on 9 May 2007 at 2:19

GoogleCodeExporter commented 9 years ago
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 dlee.cal...@gmail.com on 9 May 2007 at 12:42

GoogleCodeExporter commented 9 years ago
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 moor...@gmail.com on 29 Mar 2008 at 1:29

GoogleCodeExporter commented 9 years ago
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 fschm...@gmail.com on 29 Mar 2008 at 5:36

GoogleCodeExporter commented 9 years ago
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 pejob...@gmail.com on 21 Feb 2011 at 11:25

GoogleCodeExporter commented 9 years ago
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 fschm...@gmail.com on 21 Feb 2011 at 3:47

GoogleCodeExporter commented 9 years ago
> numberNames = map(entry(1, "one"), entry(2, "two"), entry(3, "three"));

vote +1

Original comment by pejob...@gmail.com on 2 Mar 2011 at 8:11

GoogleCodeExporter commented 9 years ago
> numberNames = map(entry(1, "one"), entry(2, "two"), entry(3, "three"));

vote +1

numbers = list( 1, 2, 3 );

vote +1

Original comment by javadba@gmail.com on 29 May 2011 at 9:42