Results 1 to 3 of 3
  1. #1
    notivago is offline Heavy Coffe Drinker
    Join Date
    Feb 2014
    Location
    São Paulo, Brazil
    Posts
    29
    Rep Power
    0

    Default Syntax sugar for collections

    Hi there folks a friend suggested posting this here so:

    I am here before you in search of wise input and maybe a little of slave work ;-)

    I started a new OSS project mostly to add a little bit of syntax sugar to the collections by extending it and maybe give a little bit more of an OO feel to the task of iterating over collections, I don't know if it will be interesting or useful to anyone, I have not settled on the interface yet, I was also made aware that some of what I want is coming with Java 8.

    The objectives are:
    1. Provide an interface to execute code over a collection instead of using loops(on the outside at least) so instead of:
      Java Code:
      for( Item item : items ) {
          {some code}
      }
      we wold have:
      Java Code:
      items.each({some code wrapped in class});
    2. Add matcher(I am going with hamcrest matchers for now) syntax to collections, for example supose we have a collection holding Food.class and we want to pick a item that is a FrenchFries.class:
      [code]
      ff = foodCounter.select( instanceOf(FrenchFries.class) );
      [/codel]

      Or get a subset of the collection with all the Candy.class that have no property called sugar:
      Java Code:
      candies = foodCounter.filter( allOf(instanceOf(Candy.class), not(hasProperty("sugar")));
    3. I would like to make it fluent so to be able:
      Java Code:
      Food toEat = foodCounter.filter( instanceOf(Candy.class).select(isInstanceOf(Healthy.class));
    4. Allow code command dispatching without horrible switches or even maps using the select semantics to pick the element and invoke the method.


    Well, there it is, those are more or less the objectives.
    The source code so far(except for a revision that is in my home computer) is here

    The maven generated project was generated with the code that is hot on my home computer, you can see by inspecting the source xref that much is changed in the interface for I am not sure on how I want it to be yet.

    I am open to suggestions, opinions, rantings, etc. If anyone has the inclination to collaborate, I will gladly accept help.

    "May the code be with you."
    -- notivago

  2. #2
    akhikhl is offline Member
    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Default Re: Syntax sugar for collections

    What you suggest looks much like what Groovy JDK does to Java Collections. Doc:

    Groovy JDK

    Maybe you can borrow some ideas from there...

    Good luck!

    Quote Originally Posted by notivago View Post
    Hi there folks a friend suggested posting this here so:

    I am here before you in search of wise input and maybe a little of slave work ;-)

    I started a new OSS project mostly to add a little bit of syntax sugar to the collections by extending it and maybe give a little bit more of an OO feel to the task of iterating over collections, I don't know if it will be interesting or useful to anyone, I have not settled on the interface yet, I was also made aware that some of what I want is coming with Java 8.

    The objectives are:
    1. Provide an interface to execute code over a collection instead of using loops(on the outside at least) so instead of:
      Java Code:
      for( Item item : items ) {
          {some code}
      }
      we wold have:
      Java Code:
      items.each({some code wrapped in class});
    2. Add matcher(I am going with hamcrest matchers for now) syntax to collections, for example supose we have a collection holding Food.class and we want to pick a item that is a FrenchFries.class:
      [code]
      ff = foodCounter.select( instanceOf(FrenchFries.class) );
      [/codel]

      Or get a subset of the collection with all the Candy.class that have no property called sugar:
      Java Code:
      candies = foodCounter.filter( allOf(instanceOf(Candy.class), not(hasProperty("sugar")));
    3. I would like to make it fluent so to be able:
      Java Code:
      Food toEat = foodCounter.filter( instanceOf(Candy.class).select(isInstanceOf(Healthy.class));
    4. Allow code command dispatching without horrible switches or even maps using the select semantics to pick the element and invoke the method.


    Well, there it is, those are more or less the objectives.
    The source code so far(except for a revision that is in my home computer) is here

    The maven generated project was generated with the code that is hot on my home computer, you can see by inspecting the source xref that much is changed in the interface for I am not sure on how I want it to be yet.

    I am open to suggestions, opinions, rantings, etc. If anyone has the inclination to collaborate, I will gladly accept help.

    "May the code be with you."
    -- notivago

  3. #3
    notivago is offline Heavy Coffe Drinker
    Join Date
    Feb 2014
    Location
    São Paulo, Brazil
    Posts
    29
    Rep Power
    0

    Default Re: Syntax sugar for collections

    Yes, something like that, in fact I started thinking on bringing such ideas to java from studying Groovy, Python and Clojure. The idea is not new, I even expected to find it in Apache's commons-collections, unfortunately it is not there, at least not fully there.

    But Apache the right course would be submit the code to complement Apache's, but for now I want a bit more of freedom to change it and experiment, when things become more stable probably I will offer them.

Similar Threads

  1. Collections
    By suhaas.mohandos@gmail.com in forum New To Java
    Replies: 9
    Last Post: 08-20-2013, 01:32 PM
  2. Collections
    By NatureFreshMilk in forum New To Java
    Replies: 12
    Last Post: 05-23-2011, 08:12 AM
  3. Collections help.
    By YoungJavaBoy in forum New To Java
    Replies: 3
    Last Post: 03-14-2011, 11:14 PM
  4. Collections
    By Cbani in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •