Lists, Arrays. etc

0 views
Skip to first unread message

Chris Double

unread,
Sep 25, 2007, 9:10:53 PM9/25/07
to The Vodka Language Discussion Group
What are the options for dynamic collections like lists and arrays in
Vodka? Should I use the Java collections stuff?

For example, I have a result set returned from a JDBC query. I'm
processing the result set like this (rs is the JDBC ResultSet):

def result(rs):
{
new gen;
def gen(yield):
{
if rs->next():
{
yield(rs->getString(1));
gen(yield);
}
return ();
}
return gen;
}

This example only yields the first item in the row of the result set.
I'd like to return a collection of some sort of all the items in that
row. I can get the count of the columns with:

metadata = rs->getMetaData();
columns = metadata->getColumnCount();

Can I create a tuple of length 'columns' containing the results of
getString up to that count? Or a record containing fields with the
column name and value? Or a list of all the items? What's a good
'Vodka-ish' approach?

BTW, I notice these examples based on ones from the thesis don't seem
to work:

>>> a=(1,2);
>>> print(a[0]);
Exception:nice.lang.AssertionFailed: `instanceof`(tuple, Tuple) failed
at transformDesugar.nice:422

>>> b = {} ++ name: "john";
Exception:vodka.parser.parser.ParserException: [1,5] expecting: '(',
'[', 'fn', 'if', const bool, const int, const real, identifier, '"',
'"""'

Chris.
--
http://www.bluishcoder.co.nz

tiark...@googlemail.com

unread,
Sep 26, 2007, 7:04:32 PM9/26/07
to The Vodka Language Discussion Group
> Can I create a tuple of length 'columns' containing the results of
> getString up to that count? Or a record containing fields with the
> column name and value? Or a list of all the items? What's a good
> 'Vodka-ish' approach?

In general, the structure of tuples and records must be known at
compile time. So if you knew all the columns in the table, you could
use

yield(rs->getString(1), rs->getString(2)) # yield a tuple

or

yield(id: rs->getString(1), name: rs->getString(2)) # yield a
record

If, however, you retrieve the column info from the metadata, one way
would be to use a Java collection to buffer the data and then access
it via generators on a by-need basis. If your're yielding ArrayLists,
e.g., your accessor code could then be something like this:

for col in line->iter() for line in queryDB(): ...

Another approach would be to create a nested generator for each line
in the result set directly, e.g. with

yield(rs->getString(i)) for i in 1..(metadata->getColumnCount())

This way is perhaps a bit more "Vodka-ish" and you avoid assembling
and traversing temporary collection objects. But of course you're
bound to a sequential access pattern...

> BTW, I notice these examples based on ones from the thesis don't seem
> to work:
>
> >>> a=(1,2);
> >>> print(a[0]);

> >>> b = {} ++ name: "john";

That's right, these are examples of operations at the core language
level (i.e. what the VM interprets) which are not accessible to high-
level code. In high-level Vodka code, you can e.g. access tuple fields
only by pattern matching in assignments. I think section 4.2 in the
thesis contains some more information, especially 4.2.2 and 4.2.3.

- Tiark

tiark...@googlemail.com

unread,
Sep 26, 2007, 7:13:05 PM9/26/07
to The Vodka Language Discussion Group
> yield(rs->getString(i)) for i in 1..(metadata->getColumnCount())

actually I meant this:

yield(rs->getString(i) for i in 1..(metadata->getColumnCount()))

- Tiark

Reply all
Reply to author
Forward
0 new messages