Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2 3 ... 5
  ignore  |  Print  
  Another language feature I'd like  (Read 15510 times)
0 Members and 1 Guest are viewing this topic.
Online princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« 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());
      // 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:
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 Smiley

Offline Roquen
« Reply #1 - Posted 2009-07-01 13:41:58 »

Think functional.  Or are they not "proper"?  Wink
Offline cylab

JGO Ninja


Medals: 38



« Reply #2 - Posted 2009-07-01 13:58:15 »

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

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/

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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 Smiley
Offline zammbi

JGO Coder


Medals: 4



« 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...

Current project - Rename and Sort
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« 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
Offline elias4444

Junior Member





« 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*

Offline zammbi

JGO Coder


Medals: 4



« 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...

Current project - Rename and Sort
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« 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).

Online princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« 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 Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Martin Strand

Junior Member





« 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.
Online princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« 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 Smiley

Offline appel

JGO Wizard


Medals: 50
Projects: 4


I always win!


« 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 Smiley

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.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline quixote_arg

Junior Member


Projects: 1


Jengibre


« Reply #13 - Posted 2009-07-03 00:40:45 »

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

JGO Coder




Where's the Kaboom?


« 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.

Online princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« 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 Smiley

Cas Smiley

Offline Martin Strand

Junior Member





« Reply #16 - Posted 2009-07-03 03:36:40 »

I'd just like... a better language Smiley
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.
Offline 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.
Offline fletchergames

Senior Member





« 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;
} //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.
Offline Mr_Light

Senior Member




shiny.


« 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.
Offline 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());
      // We just want the available powerups
     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.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« 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...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline 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).
Online princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« 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 Sad

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 Smiley

Offline 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 Sad

What about using Retrotranslator or Retroweaver?
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 74
Projects: 15


★★★★★


« 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 Smiley
Online princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #26 - Posted 2009-07-03 17:28:53 »

Not sure if I completely trust it Smiley

Cas Smiley

Offline 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).
Online princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #28 - Posted 2009-07-03 23:51:21 »

How does one do that in Eclipse?

Cas Smiley

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« 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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1] 2 3 ... 5
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (26 views)
2014-07-24 01:59:36

Riven (25 views)
2014-07-23 21:16:32

Riven (19 views)
2014-07-23 21:07:15

Riven (22 views)
2014-07-23 20:56:16

ctomni231 (51 views)
2014-07-18 06:55:21

Zero Volt (46 views)
2014-07-17 23:47:54

danieldean (37 views)
2014-07-17 23:41:23

MustardPeter (40 views)
2014-07-16 23:30:00

Cero (56 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!