Another language feature I'd like

So much of my code ends up being stuff like this:


		List powerups = PowerupFeature.getPowerups();
		ArrayList available = new ArrayList(powerups.size());
		// We just want the available powerups
		for (int i = 0; i < powerups.size(); i ++) {
			ShopItem pf = (ShopItem) powerups.get(i);
			if (pf.isAvailable() || DEBUG) {
				available.add(pf);
			}
		}
		buildShopItems(available);

when what would just look so much nicer is SQL:


		buildShopItems
			(
				select 
					pf
				from 
					PowerupFeature.getPowerups() pf
				where 
					pf.isAvailable() || DEBUG
			);

I believe something like this is in C# (LINQ isn’t it?)

Set operations are just such a fundamental way of dealing with data I wonder why they haven’t been integrated into proper languages much sooner.

Cas :slight_smile:

Think functional. Or are they not “proper”? :wink:

Yes and it’s pretty cool…

In java I can only think of some libraries like

http://commons.apache.org/jxpath/
http://josql.sourceforge.net/

It’s not a language feature but you could take a look at the CollectionUtils from the Apache Commons project. SetUtils.predicatedSet looks like a nice candidate, for example. Or ofcourse you could write something yourself :slight_smile:

Yes I like Linq, makes things a little easy to read and manage. I wish Java had something like that for its collections, even if its just of JavaFx, it would be nice, hmm I wonder if its been suggested…

You can do something like this pretty neatly in JavaFX script:


var stuff = for (pf in PowerupFeature.getPowerups() where pf.isAvailable() or DEBUG) pf;

(stuff is now a sequence of Powerups satisfying that requirement)

Alternatively:


  var allPowerups =  PowerupFeature.getPowerups();
  var somePowerupsSlice = allPowerups[pf | pf.isAvailable() or DEBUG];

Now, if you throw in ‘bind’, you can have those sequences maintained automatically…

More about sequence’s select clause:
http://openjfx.java.sun.com/current-build/doc/reference/ch06s11.html#d4e1923

Well, keep in mind that your first example is a loop-through. Your SQL example is a select (indexed retrieval). Hashmaps get a little closer, but no, they’re not exactly what you’re looking for either.

You could stick a database on the backend of your game and use JDBC I suppose. shrug

Oh cool I didn’t know that…

I don’t know if princec wanted the whole “indexed retrieval” functionality of proper db request, or just a nice syntax (to me his post indicated the latter).

I just want to be able to type SQL queries directly in java source against Collections.

Cas :slight_smile:

I personally feel that one of Java’s strongest features is the simplicity of the language.
If you’re working on something where you really need special syntax, perhaps you could use another vm language such as groovy, jython or scala?
I wouldn’t want to see Java turn into C# because that could make it more difficult for newcomers to adopt it.

I used to think that too, until I tried to teach a 10 year old how to code using Java the other day. It was that point that I realised it’s already about as complicated as it can be without becoming C++.

So if we look at the example I gave, and if you understand SQL, which is the nicer, easier to understand, cleaner code?

Cas :slight_smile:

Intriguing idea.

However, I don’t really agree that this should be a language feature. It provides no useful benefit to the execution of a program other than providing (some) satisfaction to certain type of programmers.

This could be accomplished with a library.

Would this do?
http://josql.sourceforge.net/

Well the JavaFX equivalent looks decent… and it does run on the JVM and mix well with Java, etc… It would still be a bit awkward to mix at the level needed to do what Cas is after. Maybe a small language extension to more easily mix Java and JavaFX could handle it though.

Nah, I’m sick of rubbish libraries that pretend to make something better in java but actually just add a bunch more crap to get around some missing thing in the language. I’d just like… a better language :slight_smile:

Cas :slight_smile:

[quote=“princec,post:16,topic:33789”]
I often use Python to quickly throw together simple tools.
Perhaps Jython would suit you? It compiles to regular Java bytecode so you don’t have write the entire application with Jython, only the parts where the Java language becomes a barrier.

I agree - it’s not the kind of thing that is worth making the language more complicated for.

Why not write a class that does just that?

public abstract class Selector <E> {
public ArrayList<E> select(final List list) {
   ArrayList<E> selectedList = new ArrayList<E>();
   for (int i = 0; i < list.size(); i ++) {
      E object = list.get(i);
      if(isConditionMet(object))
         selectedList.add(object);
   }

   return selectedList;
} //end select

//subclasses must overide this method with whatever code is needed
public abstract boolean isConditionMet(E object);
}

See, that’s not too bad. It’s just like a FileFilter.

Using it won’t be quite as pretty as your SQL solution, but it’s something you can do now without changing Java.

[quote=“princec,post:12,topic:33789”]
the first.