princec
|
 |
«
Posted
2009-07-01 13:36:10 » |
|
So much of my code ends up being stuff like this: 1 2 3 4 5 6 7 8 9 10
| List powerups = PowerupFeature.getPowerups(); ArrayList available = new ArrayList(powerups.size()); 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: 1 2 3 4 5 6 7 8 9
| 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 
|
|
|
|
Roquen
|
 |
«
Reply #1 - Posted
2009-07-01 13:41:58 » |
|
Think functional. Or are they not "proper"? 
|
|
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Tunedbeats
Junior Newbie
|
 |
«
Reply #3 - Posted
2009-07-01 14:00:42 » |
|
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
|
|
|
|
|
zammbi
|
 |
«
Reply #4 - Posted
2009-07-01 14:27:15 » |
|
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...
|
|
|
|
trembovetski
|
 |
«
Reply #5 - Posted
2009-07-02 00:32:09 » |
|
You can do something like this pretty neatly in JavaFX script: 1
| var stuff = for (pf in PowerupFeature.getPowerups() where pf.isAvailable() or DEBUG) pf; |
(stuff is now a sequence of Powerups satisfying that requirement) Alternatively: 1 2
| 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
|
|
|
|
|
elias4444
|
 |
«
Reply #6 - Posted
2009-07-02 01:25:09 » |
|
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*
|
|
|
|
zammbi
|
 |
«
Reply #7 - Posted
2009-07-02 09:47:23 » |
|
You can do something like this pretty neatly in JavaFX script: 1
| var stuff = for (pf in PowerupFeature.getPowerups() where pf.isAvailable() or DEBUG) pf; |
(stuff is now a sequence of Powerups satisfying that requirement) Oh cool I didn't know that...
|
|
|
|
trembovetski
|
 |
«
Reply #8 - Posted
2009-07-02 20:53:21 » |
|
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*
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).
|
|
|
|
|
princec
|
 |
«
Reply #9 - Posted
2009-07-02 20:58:09 » |
|
I just want to be able to type SQL queries directly in java source against Collections. Cas 
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Martin Strand
|
 |
«
Reply #10 - Posted
2009-07-02 22:02:41 » |
|
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.
|
|
|
|
|
princec
|
 |
«
Reply #11 - Posted
2009-07-02 23:25:50 » |
|
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 
|
|
|
|
appel
|
 |
«
Reply #12 - Posted
2009-07-02 23:44:38 » |
|
I just want to be able to type SQL queries directly in java source against Collections. Cas  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.
|
|
|
|
|
|
swpalmer
|
 |
«
Reply #14 - Posted
2009-07-03 01:31:33 » |
|
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.
|
|
|
|
princec
|
 |
«
Reply #15 - Posted
2009-07-03 02:46:00 » |
|
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  Cas 
|
|
|
|
Martin Strand
|
 |
«
Reply #16 - Posted
2009-07-03 03:36:40 » |
|
I'd just like... a better language  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.
|
|
|
|
|
kaffiene
|
 |
«
Reply #17 - Posted
2009-07-03 04:00:59 » |
|
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.
I agree - it's not the kind of thing that is worth making the language more complicated for.
|
|
|
|
|
fletchergames
|
 |
«
Reply #18 - Posted
2009-07-03 04:15:01 » |
|
when what would just look so much nicer is SQL: 1 2 3 4 5 6 7 8 9
| buildShopItems ( select pf from PowerupFeature.getPowerups() pf where pf.isAvailable() || DEBUG ); |
Why not write a class that does just that? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 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; } 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.
|
|
|
|
|
Mr_Light
|
 |
«
Reply #19 - Posted
2009-07-03 04:58:57 » |
|
So if we look at the example I gave, and if you understand SQL, which is the nicer, easier to understand, cleaner code? the first.
|
It's harder to read code than to write it. - it's even harder to write readable code.
The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
|
|
|
EgonOlsen
|
 |
«
Reply #20 - Posted
2009-07-03 07:08:41 » |
|
Any why not use the language features that are already there first before asking for new ones? That code is pretty 1.4ish. Why not do 1 2 3 4 5 6 7 8 9
| List<ShopItem> powerups = PowerupFeature.getPowerups(); List<ShopItem> available = new ArrayList<ShopItem>(powerups.size()); for (ShopItem pf:powerups) { if (pf.isAvailable() || DEBUG) { available.add(pf); } } buildShopItems(available); |
At least it simplifies the loop, gets rid of the cast/get-combination and is type-safe.
|
|
|
|
Riven
|
 |
«
Reply #21 - Posted
2009-07-03 08:23:34 » |
|
What Thought was always missing from the Collection classes, are internal iterators. Your average iterator is pretty inefficient, as both hasNext() and next() require exactly the same checks.
If we'd have:
someList.traverse(new ElementTraverser<T>() { public void element(T t) {
} });
and
someMap.traverse(new MapTraverser<K, V>() { public void mapping(K k, V v) {
} });
we could traverse collections much faster.
Also much easier to implement filters...
|
|
|
|
jezek2
|
 |
«
Reply #22 - Posted
2009-07-03 09:13:51 » |
|
What Thought was always missing from the Collection classes, are internal iterators. Your average iterator is pretty inefficient, as both hasNext() and next() require exactly the same checks.
You can use GNU Trove, it provides such methods and other good stuff (like more efficient hash maps/sets and primitive collections).
|
|
|
|
|
princec
|
 |
«
Reply #23 - Posted
2009-07-03 10:10:46 » |
|
Oh, I'm still stuck at 1.4 for game code thanks to the Mac legacy  But seriously, anyone who's used SQL would tend to find the expressiveness of SQL select/insert/delete statements far more easy to understand than mentally having to work out what an iterator's doing, and so on and on. It'd hide all that tedious stuff and just let you get on with succinctly specifying the logic of the operation rather than the minutae of how it actually goes about doing it. And let's face it, C# is making some wonderful inroads in this area, as it is in all sorts of other areas. We're only talking an optional syntax here that the compiler can support too, not changing any existing stuff. Anyway - I can but dream. Cas 
|
|
|
|
jezek2
|
 |
«
Reply #24 - Posted
2009-07-03 11:52:42 » |
|
Oh, I'm still stuck at 1.4 for game code thanks to the Mac legacy  What about using Retrotranslator or Retroweaver?
|
|
|
|
|
kappa
|
 |
«
Reply #25 - Posted
2009-07-03 13:29:56 » |
|
yup, no reason to still be stuck with 1.4, just code for 1.5 and stick retroweaver in your Ant build file for 1.4 compatibility 
|
|
|
|
|
princec
|
 |
«
Reply #26 - Posted
2009-07-03 17:28:53 » |
|
Not sure if I completely trust it  Cas 
|
|
|
|
pjt33
|
 |
«
Reply #27 - Posted
2009-07-03 22:40:05 » |
|
Why bother with Retroweaver nowadays when javac will do it for you? Just need to compile with -target jsr14. Of course, you'll still need to check that you don't use post-1.4 methods. I don't know a tool to do that, although writing one is easy (assuming you have the 1.4 libraries to test against, and that you're not using reflection).
|
|
|
|
|
princec
|
 |
«
Reply #28 - Posted
2009-07-03 23:51:21 » |
|
How does one do that in Eclipse? Cas 
|
|
|
|
Riven
|
 |
«
Reply #29 - Posted
2009-07-03 23:55:21 » |
|
I do it as a post-compile step. I 'never bothered to get it working in Eclipse.
Retroweave cd /path/to/retroweaver-libs/ /path/to/java -cp ./retroweaver-2.0.5.jar;./retroweaver-rt-2.0.5.jar;./asm-3.1.jar;./asm-commons-3.1.jar;./asm-util-3.1.jar net.sourceforge.retroweaver.Weaver -jar /path/to/myApp160.jar
Launch /path/to/java -cp ./myApp142.jar;./retroweaver-rt-2.0.5.jar my.package.SomeClass
|
|
|
|
|