FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

complex foreach agregators

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
antonio



Joined: 23 Aug 2005
Posts: 10

PostPosted: Mon Aug 29, 2005 3:14 pm    Post subject: complex foreach agregators Reply with quote

Object relations could be seen as hierarchycal structures like XML documents and could be iterated using XQuery sintax (or similar) that obtains a collection of elements .

I think that foreach could solve "complex iterators" like:

Code:

foreach(string name; houses[rooms>2].Owners[age>18 && married].name)
  dosomething(name);


that is "equivalent" to

Code:

foreach(House house in houses)
  if(house.rooms>2)
    foreach(Person owner; house.Owners)
      if(owner.age>18 && owner.married) {
        name = owner.name;
        dosomething(name);
     }


or more complex explesions like

Code:

foreach(Person person; City[country.cities[citizens[name=='Andrew'].count>100 ].citizens)


I'm not sure about that, but I think that new version of c# introduce this feature...
Back to top
View user's profile Send private message
antonio



Joined: 23 Aug 2005
Posts: 10

PostPosted: Mon Aug 29, 2005 4:27 pm    Post subject: Reply with quote

rectification:

Code:

foreach(Person person; houses[rooms>2].Owners[age>18 && married])
  dosomething(person.name);


Code:

foreach(House house in houses)
  if(house.rooms>2)
    foreach(Person person; house.Owners)
      if(person.age>18 && person.married) {


        dosomething(person.name);
     }
Back to top
View user's profile Send private message
antonio



Joined: 23 Aug 2005
Posts: 10

PostPosted: Mon Sep 05, 2005 6:18 am    Post subject: Reply with quote

and this?:

Person[] p = City[.name=="Madrid"].Population[.age>18 && .name=="Peter"];


Waiting opinions
Back to top
View user's profile Send private message
AgentOrange



Joined: 21 Jul 2005
Posts: 61

PostPosted: Mon Sep 05, 2005 7:11 am    Post subject: Reply with quote

man i really like the idea in general, its kind of like an sql. I dont know what negative effect this would have on the language, but I like the idea of having such a simple yet complex way to fill the languge defined arrays. the more i think about it the more i like it.
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Fri Sep 09, 2005 3:49 am    Post subject: Reply with quote

antonio wrote:

Person[] p = City[.name=="Madrid"].Population[.age>18 && .name=="Peter"];


I kind of like this, although I think there should be something in the syntax that makes it stick out. Instead of just []'s maybe something else? Or maybe it could mimic the template syntax which uses !() rather than just (). So then this becomes:

Code:
Person[] p = City![name == "Madric"].Population![age > 18 && name == "Peter"];


Note that I also took out those "." operators... they're not really neccessary, as presumably sugar like this that makes an implicit compound foreach can also make an implicit with(){} around the internals. So then this is equivelant to something like:

Code:
Person[] p;

foreach (City x; cities) {
  with (x) {
    if (name == "Madrid") {
      foreach (Person y; Population) {
        with (y) {
          if (age > 18 && name == "Peter")
            p ~= y;
        }
      }
    }
  }
}

_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
antonio



Joined: 23 Aug 2005
Posts: 10

PostPosted: Fri Sep 09, 2005 5:14 pm    Post subject: Reply with quote

csauls wrote:

I kind of like this, although I think there should be something in the syntax that makes it stick out. Instead of just []'s maybe something else? Or maybe it could mimic the template syntax which uses !() rather than just ().


The main reason is to use an extension of the basic array[key] functionallity.

Actually, I can acces 1 element on the array using a key (the index):
Code:
Person someone = population[ 1 ];

I think the idea is I can obtain an array result with the same sintax:
Code:
Person[] people = population[ 1 ];
(left side is an array... then the result is an array of 1 element).

If we assume that [1] referes to "elements on position 1 of the array", whe can asume that each element in a array has a implicit property named "position" or "index" or something similar:
Code:
Person[] people = population[.index==1];

the same for associative arrays... we can asume a property named "key" for each element in a array:
Code:
Person[] people = population[.key=="Peter"];

in fact, we are using "implicit" expressions that could be expressed in the usual way: [1] as [.index==1] or ["Peter"] as [.key=="Peter"].

The next steep is evident:
Code:
Person[] people = populatoin[.index>0 && .index <2];

or this:
Code:
Person[] people = population[.index>50 && .index<100];

or this:
Code:
Person[] people = population[.key=="Peter" or .index<50];


The main idea is:
    The sintax on the right side always evaluates to an array of results.

    The left side of the "=" is an element or an array of elements.

    When the left side is an element we could assume "first element of the resulting right array".


csauls wrote:
Note that I also took out those "." operators... they're not really neccessary, as presumably sugar like this that makes an implicit compound foreach can also make an implicit with(){} around the internals. So then this is equivelant to something like:
Code:

Person[] p;

foreach (City x; cities) {
  with (x) {
    if (name == "Madrid") {
      foreach (Person y; Population) {
        with (y) {
          if (age > 18 && name == "Peter")
            p ~= y;
        }
      }
    }
  }
}



hey... the foreach expands is really prety Smile, although introduces an implicit problem easily solved by my syntax proposal:
Code:
int age=18;
Person[] adults = People[.age>age];

whe have to diferenciate the scope of properties and the scope of other things with the same name.

other reason: dot is used to express "property of something" using the syntax "something.property". Why not "something[.property]".

About with(){};, I can't understand why D is not using the "dot" prefix like:
Code:

age = 10;
with(someone) {
 .age = age;
}
I'm newbe in D language... how do you solve this kind of conficts?


FINALLY: Something to think about
Code:
/* people named "Peter" will be renamed to "Antonio" */
population[.name=="Peter"].name="Antonio"


Code:
Person peter = new Person("Peter");
/*Peter is the friend of people with some child named "Peter". */
population[.children[.name=="Peter"].length>0].friends~=peter;

or
Code:
/* people with a child named like them */
Person people = population[.children[.name==..name]]
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Thu Sep 15, 2005 1:29 am    Post subject: Reply with quote

antonio wrote:

About with(){};, I can't understand why D is not using the "dot" prefix like:
Code:

age = 10;
with(someone) {
 .age = age;
}
I'm newbe in D language... how do you solve this kind of conficts?

There is already a meaning to a hanging "dot" operator; it means "search the global scope first." For example, in the following:
Code:

module foo;

int bar = 123;

void main () {
  int bar = 456;
  writefln(bar);
  writefln(.bar);
}


This code will print out 456 before 123, because the hanging "dot" tells it to use the "global" scope 'bar' instead of the local. Make sense?

I'm still iffy about the dot being used in these expression-indices (or whatever they'd end up being called), and that's one reason I propose the ![] syntax, its a flag to the compiler (and other parsers) that there's going to be some special stuff going on in the following expression.
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
qbert



Joined: 30 Mar 2004
Posts: 209
Location: Dallas, Texas

PostPosted: Thu Oct 20, 2005 11:49 am    Post subject: Reply with quote

Wow this is cool , you say this is going to be included in C# ? Playing devils advocate , the only bad thing about this is it looks like alot of work to implement, and Im not sure how often it would be used , though this could be one of language features that alters the way programs are written.

My vote goes to the template syntax too.

Did yall mention this on the Newsgroup ?

Charlie
Back to top
View user's profile Send private message MSN Messenger
antonio



Joined: 23 Aug 2005
Posts: 10

PostPosted: Sat Nov 05, 2005 5:43 pm    Post subject: Reply with quote

qbert wrote:
Wow this is cool , you say this is going to be included in C# ?

Not really, I think this is an idea used into C "omega", an experimental language by Microsoft: they expose this as a XQUERY port to the languaje.. for "foreach" bucles. The idea exposed in this forum is most general... a whay to evaluate arrays (not only iterations)
qbert wrote:

Playing devils advocate , the only bad thing about this is it looks like alot of work to implement, and Im not sure how often it would be used

yes, I agree: Usually, the main need is a "foreach" with an "If" evaluation into... and "algebraic" syntax is a simplification for this structres...
qbert wrote:
though this could be one of language features that alters the way programs are written.

It's most a new paradigm and a new thinking way for developers... and not only a new "tool". D could be prefect for introducing this paradigm.
qbert wrote:

My vote goes to the template syntax too.

Yes... this is the most simplest way, but no the most efficient...
It's possible to give compilers a way for optimizacion? (diferent order evaluation can inprove performance... like relational databases plan analyzer).
qbert wrote:

Did yall mention this on the Newsgroup ?

mmmm... no Smile

Antonio
Back to top
View user's profile Send private message
AgentOrange



Joined: 21 Jul 2005
Posts: 61

PostPosted: Sat Nov 05, 2005 10:36 pm    Post subject: Reply with quote

Yeah I believe it is going to be in the newest version of C#.
Back to top
View user's profile Send private message
antonio



Joined: 23 Aug 2005
Posts: 10

PostPosted: Mon Feb 13, 2006 6:12 pm    Post subject: Reply with quote

csauls wrote
Quote:

I'm still iffy about the dot being used in these expression-indices (or whatever they'd end up being called), and that's one reason I propose the ![] syntax, its a flag to the compiler (and other parsers) that there's going to be some special stuff going on in the following expression.


I always thought dot signifies "property of" or "method of"... basically, "member of".

Personally, I don't like how "D" is using de dot for "global scope" out of the with clausule because it brokes the standard "member of" syntax:

Example:
Code:

object.Member;
object.Member2;

could be "compressed" using
Code:

with( object ) {
  .Member;
  .Member2;
}

dot signifies the same: member of an object.

In fact, when you are in a very large code block, the dot "." helps you to recognize that your are talking about a member of the "with" object.

Code:

  with( object ) {
    bla;
    bla;
    bla;
    .......   something = .Member; .....
    bla;
    bla;
  }


I'm experienced with Pascal that uses the same sintax than D... People doesn't use the WITH clausule because it's really dificult to recognize the members of the object/structure into the block... I prefere the Visual Basic sintax... more clean.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group