Queries
jQuery filters, sorts, groups and totals a Ring list or a Java collection with one chain of calls.
jQuery(source) starts a chain, like LINQ in .NET or streams in Java. The source is a Ring list, a Java collection, array, map or stream, or another query. Each operator gives a new query and leaves the source as it was; toList() or one of the value operators ends the chain.
load "java.ring"
aPeople = [["Ali","Cairo",31],["Sara","Dubai",27],["Omar","Cairo",45]]
? jQuery(aPeople).filter("x[3] > 25").sortByDesc("x[3]").map("x[1]").join(", ")A Java source is copied to a Ring list once (one call into Java, not one per item), and then the chain runs in Ring.
The code of an operator#
The code is Ring text, or the name of a Ring function.
- Text:
xis the item andythe second value ofreduce. A text such as"x[3] > 25"is an expression; a text that starts withreturnor has several lines is a function body. The text is compiled once withevaland kept, so a chain in a loop does not compile again. - A function name:
filter("isSmall")callsfunc isSmall xwith the item. - The text sees global variables and functions only, not the local variables of the function that builds the chain.
- A Java
Stringis a Ring text and a JavaIntegera Ring number when it comes out of a collection; other objects stay Java objects, sox.compareTo(y)and their methods work.
Operators that give a query#
| Operator | Meaning |
|---|---|
filter(code) | the items for which the code is true |
map(code) | the results of the code |
flatMap(code) | the lists that the code gives, joined into one |
sortBy(code), sortByDesc(code) | sorted by the key that the code gives (stable) |
thenBy(code), thenByDesc(code) | a second (third ...) key after sortBy |
sorted(), reverse() | sorted by the items themselves; reversed |
skip(n), limit(n) | drop or keep the first n |
takeWhile(code), dropWhile(code) | keep or drop the start while the code is true |
distinct() | each value once |
concat(list), union(list), intersect(list), except(list) | sets and joins |
groupBy(code) | a list of [key, items] in the order of the first item of each key |
Operators that give a value#
| Operator | Meaning |
|---|---|
toList(), toJava() | a Ring list; a Java ArrayList |
toMap(keyCode, valueCode) | a list of [key, value] (empty valueCode means the item) |
count(), countWhere(code), isEmpty() | the number of items |
anyMatch(code), allMatch(code), noneMatch(code), contains(value) | true or false |
first(), firstWhere(code), firstOr(value), last(), elementAt(n) | one item; first() of nothing is an error |
sum(code), average(code), min(code), max(code) | an empty code means the items themselves |
reduce(start, code) | x is the result so far and y the item |
join(text) | the items as one text |
each(code) | run the code for each item |
jRange(from, to) is the numbers from from to to (down when to is smaller).
Comparing and equality#
- Numbers compare as numbers, texts case-sensitively with
strcmp, and Java objects withcompareTo. distinct,union,intersect,except,groupByandtoMaptreat numbers and texts by value and Java objects byhashCodeandequals, so two equalArrayListobjects are one value.- A query is eager: each operator runs when you call it.
An error has the text Java Error: ... like the other errors of the package, for example first() of an empty query: no items.