|
|
Subject: [RFE] scala.xml.NodeSeq - msg#00011
List: lang.scala
hi burak,
here is another request for the scala xml libs.
would you consider adding an overloaded version of
apply as an alias for the filter method.
def apply(f: (Node) => Boolean): NodeSeq
this would make for a very xpath like syntax.
if apply on Node could be overloaded too as this
def apply(s: Symbol): String
you would get something that is very compact.
biblio \ "book" \ "title" { n => n('text) == "Data on ze web"}
now if i could use the at-sign in identifers too
then i could also write
biblio \ "book" \ "title" { @ => @('text) == "Data on ze web"}
i guess it wont get any much xpathlisher than that (just dreaming).
ciao robertj
Was this page helpful?
Thread at a glance:
Previous Message by Date:
click to view message preview
Multi-dimensional arrays in Scala
Multi-dimensional arrays almost work in Scala ... I wonder if the following
"little" change could be made.
The basic idea is to use a * parameter for the accessor and update function,
thus:
==========================
object Test3 {
class MyArray[a](init:a, val x:int*) {
var v:a = init; // backing store.
def apply(x:int*) : a = {
// TBD: map from x to offset
v
}
def update(d:a, x:int*) : Unit = {
// TBD: map from x to offset
v = d;
}
}
def main(args: Array[String]) = {
val z = new MyArray[double](0.0,5,5);
Console.println("Initial value=" + z(2,2));
Console.println("Bounds: " + z.x);
z(2,2) = -41;
Console.println("After update value=" + z(2,2));
}
}
=====================
Now of course the little problem is the text in the Lang Spec 1.0 on top of P
64:
======
An assignment f(args) = e with a function application to the left of the "="
operator is interpreted as f.update(args,e), i.e. the invocation of an update
function defined by f.
======
Could this be modified to:
An assignment f(args) = e with a function application to the left of the "="
operator is interpreted as f.update(e,args), i.e. the invocation of an update
function defined by f.
(The point is to pass the new value first in the call to update.)
Best,
Vijay
PS: The above code compiles and runs but does the wrong thing, of course.
________________________________________________________________
Sent via the WebMail system at mail.saraswat.org
Next Message by Date:
click to view message preview
stressing the parser again
hi,
i know i am stressing that poor ol' parser of yours.
but here is something that should work as far as i am concerned.
class Node
{
def /(_right: Node):Node = this;
def apply(f: (Node) => boolean): Node = this;
def apply(s: Symbol): String = "";
}
var d1 = new Node;
var d2 = new Node;
var d3 = new Node;
d1 / d2 { d => true} / d3 { d => true} { d => true};
this gives me the following error:
[scalac] TestPermissionXml.scala:90: missing parameter type
[scalac] d1 / d2 { d => true} / d3 { d => true} { d => true};
[scalac] ^
now i have 2 options. i please the compiler by giving the
second closure an type as asked
d1 / d2 { d => true} / d3 { d:Node => true} { d => true};
OR (that one got me stumped)
i remove the overloaded apply method.
def apply(s: Symbol): String = "";
comments?
ciao robertj
Previous Message by Thread:
click to view message preview
Multi-dimensional arrays in Scala
Multi-dimensional arrays almost work in Scala ... I wonder if the following
"little" change could be made.
The basic idea is to use a * parameter for the accessor and update function,
thus:
==========================
object Test3 {
class MyArray[a](init:a, val x:int*) {
var v:a = init; // backing store.
def apply(x:int*) : a = {
// TBD: map from x to offset
v
}
def update(d:a, x:int*) : Unit = {
// TBD: map from x to offset
v = d;
}
}
def main(args: Array[String]) = {
val z = new MyArray[double](0.0,5,5);
Console.println("Initial value=" + z(2,2));
Console.println("Bounds: " + z.x);
z(2,2) = -41;
Console.println("After update value=" + z(2,2));
}
}
=====================
Now of course the little problem is the text in the Lang Spec 1.0 on top of P
64:
======
An assignment f(args) = e with a function application to the left of the "="
operator is interpreted as f.update(args,e), i.e. the invocation of an update
function defined by f.
======
Could this be modified to:
An assignment f(args) = e with a function application to the left of the "="
operator is interpreted as f.update(e,args), i.e. the invocation of an update
function defined by f.
(The point is to pass the new value first in the call to update.)
Best,
Vijay
PS: The above code compiles and runs but does the wrong thing, of course.
________________________________________________________________
Sent via the WebMail system at mail.saraswat.org
Next Message by Thread:
click to view message preview
Re: [RFE] scala.xml.NodeSeq
robert kuzelj wrote:
hi burak,
here is another request for the scala xml libs.
would you consider adding an overloaded version of
apply as an alias for the filter method.
def apply(f: (Node) => Boolean): NodeSeq
this would make for a very xpath like syntax.
This is quite a nice idea. Although in practice it will not work out as
nice.
if apply on Node could be overloaded too as this
def apply(s: Symbol): String
you would get something that is very compact.
biblio \ "book" \ "title" { n => n('text) == "Data on ze web"}
Have you tried running that code? scalac says "title" cannot be applied
to ...
but the following would work. First we add a 'text' function, that
converts any XML to a string (XPath has the same)
(biblio \ "book") { n: Node => n \ "title" == "Data on ze web" }
There is some magic in NodeSeq that calls 'text' on n\"title" before
comparing.
We will need the type annotation, as you discovered in your other mail,
because there is already the apply method inherited from scala.Seq and
apply thus becomes overloaded.
now if i could use the at-sign in identifers too
then i could also write
biblio \ "book" \ "title" { @ => @('text) == "Data on ze web"}
i guess it wont get any much xpathlisher than that (just dreaming).
If I had the '@', I would certainly use it for attributes. I even tried
that once, using the fancy `backquoting mechanism` for arbitrary
identifiers (you can even have spaces...). It becomes a bit cryptic
though if you don't have a proper syntactic category for attributes in
your language.
Using apply for predicates is quite a cool idea. Thanks for that, this
is going to be in the next release for sure.
cheers,
Burak
--
Burak Emir
http://lamp.epfl.ch/~emir
|
|