people[name=="Andrew"].friends ~= peter

Daniel Keep daniel.keep.lists at gmail.com
Thu May 4 19:14:56 PDT 2006



antonio wrote:
> 
> As I introduced in http://www.dsource.org/forums/viewtopic.php?t=967
> object relations could be seen as hierarchycal structures.
> 
> ¿Why not to introduce native syntax to "navigate into"/"Selec from" this
> kind of hierarchies?
> 
> 
> ex 1: Add Peter to the friends of people named Andrew and older than 18.
> 
> Person peter = ...;
> Person[] people = ...;
> 
> people[name=="Andrew" && age>18].friends ~= peter;
> 
> 
> ex 2: Obtain the array of not married people childs.
> ex 2.a: with duplicates:
> 
> Person[] modernChilds = people[!married].childs;
> 
> ex 2.b: without duplicates:
> 
> Person[] modernChilds = Distinct(people[!married].childs);
> 
> ex 3: Do something with the married childs of people with friends named
> Andrew
> ex 3.a: using foreach (1 by 1 evaluation)
> 
> foreach( Person someone;
> people[friends[name=="Andrew"].length!=0].childs[married] ){
>   someone.doSomething();
> }
> 
> ex 3.b: Only 1 stament:
> 
> people[friends[name=="Andrew"].length!=0].childs[married].doSomething();
> 
> ex 3.c: without duplicates
> Distinct(people[friends[name=="Andrew"].length!=0].childs[married].doSomething();
> 
> 
> (¿could Distinct be solved with a Template?)
> ---
> 
> These examples could be solved using "D" standard syntax.
> 
> ex: (thanks to csauls)
> 
> Person[] p = cities[name=="Madrid"].population[age>18 && name=="Peter"];
> 
> is equivalent to
> 
> Person[] p;
> foreach (City x; cities) {
>   with (x) {
>     if (name == "Madrid") {
>       foreach (Person y; population) {
>         with (y) {
>           if (age > 18 && name == "Peter")
>             p ~= y;
>         }
>       }
>     }
>   }
> }
> 
> Basically:
> 
> ARRAY[CONDITION].SOMETHING;
> 
> could be traslated to:
> 
> foreach(T x; ARRAY)
>   with(x)
>     if( CONDITION ) {
>     SOMETHING;
>     }
> 
> When CONDITION contains sub-ARRAY evaluations, it could be expanded as
> 
> foreach(T x; ARRAY)
>   with(x) {
>     bool b;
>     """Expand CONDITION and put result into b""";
>     if( b ) {
>     DOSOMETHING;
>     }
>   }
> 
> Of course, this "expanding" method doesn't solve all the posibilities...
> 
>     people[age>10] = new Person("Foo");
> 
> ---
> 
> The main discussion about this idea was focused in 2 points:
> 1. "dot" or "not dot":
>     people[.married && .age>18]
>     vs
>     people[married && age>18]
> 
>    "not dot" is more "D" syntax compliat (thanks to csaul).
> 
> 2.Array syntax vs "Template" syntax:
>     people[married && age>18]
>     vs
>     people![married && age>18]
> 
>   Personally, I prefer "Array syntax":
>     Person p = people[5];
>     Person[] p = people[3..5]; // 3..5 is a "condition"
>     Person[] p = people[5]; // ¿why not?
>     Person[] p = people[married];
>     Person[] p = people[age>18 && married];
>      
>     
> ---
> In the forum, we told about C# 2.0 similar sintax based on XPath I think
> than this "D" proposal is more powerful.
> 
> 
> I have a very poor english... sorry:  be comprensive :-)
> Antonio
> 

I like the idea very much; it's similar to Python generator expressions.
 The example:

	people[married && age>18]

Would be written in Python as:

	(p for p in people if p.married and p.age>18)

With the advantage that you can also do operations on the "p"s:

	(p.firstName for p in people if ...)

Which would return the person's first name instead of the person object
itself.  But I digress.

As I said, I agree with the idea: it's a very nice piece of syntactic
sugar.  The problem is with the syntax you've chosen.  One of D's
strengths is that the grammar is context-free, making it easy to implement.

But, without knowing anything about context, what does this mean:

	people[i]

Well, if "i" is an integer, then it's indexing the array.  If "i" is a
member of the elements of the people array, then it would be a
conditional expression.  But what if it's a member, and "i" is an
integer?  Is it a conditional or an index then?

And what if both are defined?

Even if the compiler can work out a way to distinguish this, the syntax
in its current form looks very hard for humans to parse in certain
fringe cases.

As you said, an alternative is "templateish" syntax:

	people![i]

I like this more, since it's *explicit* about what's going on.  That "!"
means "Hey, don't actually index the array; select elements of it only".

The other alternative is to come up with something like Python's
notation, where the condition is written "outside" of the array.  The
advantage with this is that you can also perform transformations on the
data (which is also a very common thing to do).

At any rate, nice proposal, and I look forward to seeing something come
of it :)

Oh, one other thing that suddenly occured to me: what if "people" isn't
an array?  You use 'foreach' in your expansions, but what if "people"
CAN be iterated over, but isn't an array in of itself?  Then the syntax
becomes downright misleading!

	-- Daniel "Just so long as it doesn't look like SQL" Keep

P.S.  Your English is better than many people I've seen who don't know
any other languages.  Also, that upside-down "?" is nifty :)

-- 

v1sw5+8Yhw5ln4+5pr6OFma8u6+7Lw4Tm6+7l6+7D
a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP    http://hackerkey.com/



More information about the Digitalmars-d mailing list