RingJavafor Ring

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.

Ring
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.

Operators that give a query#

OperatorMeaning
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#

OperatorMeaning
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#

An error has the text Java Error: ... like the other errors of the package, for example first() of an empty query: no items.

RingJava - use Java classes and methods from Ring programs.