Skip to content

stdlib.rgr

Extra library operators for arrays, maps and vectors: map, filter, reduce, any, all, slice. Also the case and cast operators for union type values.

To use these operators, add the import to the program:

Import "stdlib.rgr"

Source: lib/stdlib.rgr.

This file holds two operator mechanisms. The template operators write target code from a string per language. The type methods are Ranger code. The compiler compiles them for every target that loads the library.

Operator Arguments Gives
case arg: T, item2: boolean, code: block void
case arg: T, item2: double, code: block void
case arg: T, item2: int, code: block void
case arg: T, item2: JSONArrayObject, code: block void
case arg: T, item2: JSONDataObject, code: block void
case arg: T, item2: string, code: block void
case arg: T, item: T, code: block void
cast arg: T, target: Any Any
identical a: T, b: T boolean
is arg: T, item: T boolean
to to: T, item: T T

Target support marks

  • Own template
  • Default template
  • No template

The operator works for a target with an own template and for a target with the default template. The default template is the * entry of the definition: it writes the same code for every target that has no template of its own. A target with no template writes no code, and a program that uses the operator does not compile for that target.

case

(case arg item2 code) → void
case(arg: T, item2: boolean, code: block)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: no template
  • Kotlin: own template
  • Dart: own template
  • Swift: no template
  • C#: no template
  • C++: own template
  • PHP: no template
  • Scala: no template

Definition: lib/stdlib.rgr, line 17

case

(case arg item2 code) → void
case(arg: T, item2: double, code: block)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: no template
  • Kotlin: own template
  • Dart: own template
  • Swift: no template
  • C#: no template
  • C++: own template
  • PHP: own template
  • Scala: no template

Definition: lib/stdlib.rgr, line 125

case

(case arg item2 code) → void
case(arg: T, item2: int, code: block)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: no template
  • Kotlin: own template
  • Dart: own template
  • Swift: no template
  • C#: no template
  • C++: own template
  • PHP: own template
  • Scala: no template

Definition: lib/stdlib.rgr, line 68

case

(case arg item2 code) → void
case(arg: T, item2: JSONArrayObject, code: block)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: lib/stdlib.rgr, line 361

case

(case arg item2 code) → void
case(arg: T, item2: JSONDataObject, code: block)

A member of a system union -- the JSON unions over JSONDataObject and JSONArrayObject -- is an ordinary object of the target language. It carries no shape tag, so the generic overload below, which narrows on __rg_kind / _rg_kind / .tag, can never be true for one. These two overloads keep the class test that a system union needs; the generic overload keeps the tag test that a shape case needs. Every `@serialize` deserializer emits `case item oo:JSONDataObject`, so without these the reader of an array of objects reads nothing.

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: lib/stdlib.rgr, line 251

case

(case arg item2 code) → void
case(arg: T, item2: string, code: block)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: no template
  • Kotlin: own template
  • Dart: own template
  • Swift: no template
  • C#: no template
  • C++: own template
  • PHP: own template
  • Scala: no template

Definition: lib/stdlib.rgr, line 183

case

(case arg item code) → void
case(arg: T, item: T, code: block)

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: lib/stdlib.rgr, line 471

cast

(cast arg target) → Any
cast(arg: T, target: Any)

Target support

  • JavaScript: no template
  • TypeScript: no template
  • Go: own template
  • Rust: no template
  • Python: no template
  • Java: no template
  • Kotlin: no template
  • Dart: no template
  • Swift: no template
  • C#: no template
  • C++: no template
  • PHP: no template
  • Scala: no template

Definition: lib/stdlib.rgr, line 11

identical

(identical a b) → boolean
identical(a: T, b: T)

Reference identity: are these two names the same object? `==` means something different on every target — structural on Kotlin, a trait bound on Rust, pointer on C++ — so identity gets its own operator rather than a per-target reading of equality. Used by the generated equality of a shape's `@(reference)` cases (PLAN_SHAPES.md S4), and useful on its own.

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: default template
  • Rust: own template
  • Python: own template
  • Java: default template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: default template
  • PHP: own template
  • Scala: own template

Definition: lib/stdlib.rgr, line 626

is

(is arg item) → boolean
is(arg: T, item: T)

`is v Shape.Case` — the kind test of `case` with the binding and the block taken away. `case v x:Shape.Case { … }` answers "is it this case?" and then hands the arm a narrowed `x`; an arm that only wants the answer pays for a binding it never reads. On Rust that binding costs a `.clone()` of the scrutinee (a refcount pair for every reference case), on Go a declared-and-discarded local, on ES6/Python a dead assignment. This is an expression, so it composes: `return (is self EvalValue.Number)` replaces the whole `case self n:… { return true } return false` shape. Every template below is the same discriminant test the `case` operator above already lowers to on that target — nothing new is invented, and no target evaluates (e 1) more than once.

Target support

  • JavaScript: own template
  • TypeScript: own template
  • Go: own template
  • Rust: own template
  • Python: own template
  • Java: own template
  • Kotlin: own template
  • Dart: own template
  • Swift: own template
  • C#: own template
  • C++: own template
  • PHP: own template
  • Scala: own template

Definition: lib/stdlib.rgr, line 590

to

(to to item) → T
to(to: T, item: T)

Target support

  • JavaScript: default template
  • TypeScript: default template
  • Go: default template
  • Rust: default template
  • Python: default template
  • Java: default template
  • Kotlin: default template
  • Dart: default template
  • Swift: default template
  • C#: default template
  • C++: default template
  • PHP: default template
  • Scala: default template

Definition: lib/stdlib.rgr, line 643

A type method is an operator of the receiver type. The call is receiver.name(…). The body is Ranger code, so the compiler writes it for every target that compiles the library.

Method Arguments Gives Targets
forEach cb: (fn:void (item:T index:string)) void every target
forKeys cb: (fn:void (index:string)) void every target
get_or key: string, fallback: T T every target
map_length int every target
values [T] every target

forEach

receiver.forEach(cb) → void
forEach(cb: (fn:void (item:T index:string)))

The compiler writes this method for every target.

The Ranger source of the method
fn forEach:void (cb:(_:void (item:T index:string )) ) {
def list (keys self)
for list kk:string i {
def value (unwrap (get self kk))
cb( value kk )
}
}

Definition: lib/stdlib.rgr, line 664

forKeys

receiver.forKeys(cb) → void
forKeys(cb: (fn:void (index:string)))

The compiler writes this method for every target.

The Ranger source of the method
fn forKeys:void (cb:(_:void (index:string))) {
def list (keys self)
for list it:string i {
cb(it)
}
}

Definition: lib/stdlib.rgr, line 671

get_or

receiver.get_or(key fallback) → T
get_or(key: string, fallback: T)

Value at `key`, or `fallback` when absent

The compiler writes this method for every target.

Values, size and default value of a map

docs/examples/lib/stdlib/map-helpers.rgr
Import "stdlib.rgr"
class Main {
sfn main:void () {
def ages:[string:int]
set ages "ada" 36
set ages "alan" 41
def all_ages (ages.values())
print ("count " + (array_length all_ages))
print ("keys " + (ages.map_length()))
print ("grace " + (ages.get_or("grace" 0)))
}
}
main function, JavaScript
let ages = {};
ages["ada"] = 36;
ages["alan"] = 41;
const all_ages = operatorsOf.values_2(ages);
console.log("count " + all_ages.length);
console.log("keys " + operatorsOf.mapc95length_2(ages));
console.log("grace " + operatorsOf.getc95or_3(ages, "grace", 0));
The complete file
class Main {
constructor() {
}
}
class operatorsOf {
constructor() {
}
}
operatorsOf.values_2 = function(__self) {
let res = [];
const list = Object.keys(__self);
for ( const kk of list) {
res.push(( Object.prototype.hasOwnProperty.call(__self, kk) ? __self[kk] : undefined ));
}
return res;
};
operatorsOf.mapc95length_2 = function(__self) {
const list_1 = Object.keys(__self);
return list_1.length;
};
operatorsOf.getc95or_3 = function(__self, key, fallback) {
if ( ( typeof(__self[key] ) != "undefined" && Object.prototype.hasOwnProperty.call(__self, key) ) ) {
return ( Object.prototype.hasOwnProperty.call(__self, key) ? __self[key] : undefined );
}
return fallback;
};
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
let ages = {};
ages["ada"] = 36;
ages["alan"] = 41;
const all_ages = operatorsOf.values_2(ages);
console.log("count " + all_ages.length);
console.log("keys " + operatorsOf.mapc95length_2(ages));
console.log("grace " + operatorsOf.getc95or_3(ages, "grace", 0));
}
__js_main();
The Ranger source of the method
fn get_or:T (key:string fallback:T) @doc('Value at `key`, or `fallback` when absent') {
if( has self key ) {
return (unwrap (get self key))
}
return fallback
}

Definition: lib/stdlib.rgr, line 692

map_length

receiver.map_length() → int

Number of keys in the map

The compiler writes this method for every target.

Values, size and default value of a map

docs/examples/lib/stdlib/map-helpers.rgr
Import "stdlib.rgr"
class Main {
sfn main:void () {
def ages:[string:int]
set ages "ada" 36
set ages "alan" 41
def all_ages (ages.values())
print ("count " + (array_length all_ages))
print ("keys " + (ages.map_length()))
print ("grace " + (ages.get_or("grace" 0)))
}
}
main function, JavaScript
let ages = {};
ages["ada"] = 36;
ages["alan"] = 41;
const all_ages = operatorsOf.values_2(ages);
console.log("count " + all_ages.length);
console.log("keys " + operatorsOf.mapc95length_2(ages));
console.log("grace " + operatorsOf.getc95or_3(ages, "grace", 0));
The complete file
class Main {
constructor() {
}
}
class operatorsOf {
constructor() {
}
}
operatorsOf.values_2 = function(__self) {
let res = [];
const list = Object.keys(__self);
for ( const kk of list) {
res.push(( Object.prototype.hasOwnProperty.call(__self, kk) ? __self[kk] : undefined ));
}
return res;
};
operatorsOf.mapc95length_2 = function(__self) {
const list_1 = Object.keys(__self);
return list_1.length;
};
operatorsOf.getc95or_3 = function(__self, key, fallback) {
if ( ( typeof(__self[key] ) != "undefined" && Object.prototype.hasOwnProperty.call(__self, key) ) ) {
return ( Object.prototype.hasOwnProperty.call(__self, key) ? __self[key] : undefined );
}
return fallback;
};
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
let ages = {};
ages["ada"] = 36;
ages["alan"] = 41;
const all_ages = operatorsOf.values_2(ages);
console.log("count " + all_ages.length);
console.log("keys " + operatorsOf.mapc95length_2(ages));
console.log("grace " + operatorsOf.getc95or_3(ages, "grace", 0));
}
__js_main();
The Ranger source of the method
fn map_length:int () @doc('Number of keys in the map') {
def list (keys self)
return (array_length list)
}

Definition: lib/stdlib.rgr, line 687

values

receiver.values() → [T]

All values of the map

The compiler writes this method for every target.

Values, size and default value of a map

docs/examples/lib/stdlib/map-helpers.rgr
Import "stdlib.rgr"
class Main {
sfn main:void () {
def ages:[string:int]
set ages "ada" 36
set ages "alan" 41
def all_ages (ages.values())
print ("count " + (array_length all_ages))
print ("keys " + (ages.map_length()))
print ("grace " + (ages.get_or("grace" 0)))
}
}
main function, JavaScript
let ages = {};
ages["ada"] = 36;
ages["alan"] = 41;
const all_ages = operatorsOf.values_2(ages);
console.log("count " + all_ages.length);
console.log("keys " + operatorsOf.mapc95length_2(ages));
console.log("grace " + operatorsOf.getc95or_3(ages, "grace", 0));
The complete file
class Main {
constructor() {
}
}
class operatorsOf {
constructor() {
}
}
operatorsOf.values_2 = function(__self) {
let res = [];
const list = Object.keys(__self);
for ( const kk of list) {
res.push(( Object.prototype.hasOwnProperty.call(__self, kk) ? __self[kk] : undefined ));
}
return res;
};
operatorsOf.mapc95length_2 = function(__self) {
const list_1 = Object.keys(__self);
return list_1.length;
};
operatorsOf.getc95or_3 = function(__self, key, fallback) {
if ( ( typeof(__self[key] ) != "undefined" && Object.prototype.hasOwnProperty.call(__self, key) ) ) {
return ( Object.prototype.hasOwnProperty.call(__self, key) ? __self[key] : undefined );
}
return fallback;
};
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
let ages = {};
ages["ada"] = 36;
ages["alan"] = 41;
const all_ages = operatorsOf.values_2(ages);
console.log("count " + all_ages.length);
console.log("keys " + operatorsOf.mapc95length_2(ages));
console.log("grace " + operatorsOf.getc95or_3(ages, "grace", 0));
}
__js_main();
The Ranger source of the method
fn values:[T] () @doc('All values of the map') {
def res:[T]
def list (keys self)
for list kk:string i {
push res (unwrap (get self kk))
}
return res
}

Definition: lib/stdlib.rgr, line 678

Method Arguments Gives Targets
all cb: (fn:boolean (item:T)) boolean every target
any cb: (fn:boolean (item:T)) boolean every target
clone [T] every target
contains cb: (fn:boolean (item:T)) boolean every target
count cb: (fn:boolean (item:T)) int every target
filter cb: (fn:boolean (item:T index:int)) [T] every target
find cb: (fn:boolean (item:T)) T every target
forEach cb: (fn:void (item:T index:int)) void every target
groupBy cb: (fn:string (item:T)) [T] every target
has el: T boolean every target
map cb: (fn:S (item:T index:int)), to: [S] [S] every target
map cb: (fn:T (item:T index:int)) [T] every target
reduce cb: (fn:K (left:K right:T index:int)), initialValue: K K every target
reduce cb: (fn:T (left:T right:T index:int)), initialValue: T T every target
slice start: int, end: int [T] every target

all

receiver.all(cb) → boolean
all(cb: (fn:boolean (item:T)))

True when `cb` holds for every item (true when empty)

The compiler writes this method for every target.

Test the items of an array and take a part of it

docs/examples/lib/stdlib/tests-and-slice.rgr
Import "stdlib.rgr"
class Main {
sfn main:void () {
def numbers:[int] ([] _:int (1 2 3 4 5))
if (numbers.any({ return (item > 4) })) {
print "one item is greater than four"
}
if (numbers.all({ return (item > 0) })) {
print "every item is positive"
}
def part (numbers.slice(1 3))
print ("part " + (array_length part))
}
}
main function, JavaScript
const numbers = [1, 2, 3, 4, 5];
if ( operatorsOf.any_2(numbers, ((item) => {
return item > 4;
})) ) {
console.log("one item is greater than four");
}
if ( operatorsOf.all_2(numbers, ((item) => {
return item > 0;
})) ) {
console.log("every item is positive");
}
const part = operatorsOf.slice_3(numbers, 1, 3);
console.log("part " + part.length);
The complete file
class Main {
constructor() {
}
}
class operatorsOf {
constructor() {
}
}
operatorsOf.any_2 = function(__self, cb) {
for ( const it of __self) {
if ( cb(it) ) {
return true;
}
}
return false;
};
operatorsOf.all_2 = function(__self, cb) {
for ( const it_1 of __self) {
if ( cb(it_1) == false ) {
return false;
}
}
return true;
};
operatorsOf.slice_3 = function(__self, start, end) {
let res = [];
const __len = __self.length;
let from = start;
let to = end;
if ( from < 0 ) {
from = 0;
}
if ( to > __len ) {
to = __len;
}
let i_2 = from;
while (i_2 < to) {
res.push(__self[i_2]);
i_2 = i_2 + 1;
};
return res;
};
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const numbers = [1, 2, 3, 4, 5];
if ( operatorsOf.any_2(numbers, ((item) => {
return item > 4;
})) ) {
console.log("one item is greater than four");
}
if ( operatorsOf.all_2(numbers, ((item) => {
return item > 0;
})) ) {
console.log("every item is positive");
}
const part = operatorsOf.slice_3(numbers, 1, 3);
console.log("part " + part.length);
}
__js_main();
The Ranger source of the method
fn all:boolean (cb:(_:boolean (item:T))) @doc('True when `cb` holds for every item (true when empty)') {
for self it@(lives):T i {
if( (cb(it)) == false ) {
return false
}
}
return true
}

Definition: lib/stdlib.rgr, line 826

any

receiver.any(cb) → boolean
any(cb: (fn:boolean (item:T)))

True when `cb` holds for at least one item

The compiler writes this method for every target.

Test the items of an array and take a part of it

docs/examples/lib/stdlib/tests-and-slice.rgr
Import "stdlib.rgr"
class Main {
sfn main:void () {
def numbers:[int] ([] _:int (1 2 3 4 5))
if (numbers.any({ return (item > 4) })) {
print "one item is greater than four"
}
if (numbers.all({ return (item > 0) })) {
print "every item is positive"
}
def part (numbers.slice(1 3))
print ("part " + (array_length part))
}
}
main function, JavaScript
const numbers = [1, 2, 3, 4, 5];
if ( operatorsOf.any_2(numbers, ((item) => {
return item > 4;
})) ) {
console.log("one item is greater than four");
}
if ( operatorsOf.all_2(numbers, ((item) => {
return item > 0;
})) ) {
console.log("every item is positive");
}
const part = operatorsOf.slice_3(numbers, 1, 3);
console.log("part " + part.length);
The complete file
class Main {
constructor() {
}
}
class operatorsOf {
constructor() {
}
}
operatorsOf.any_2 = function(__self, cb) {
for ( const it of __self) {
if ( cb(it) ) {
return true;
}
}
return false;
};
operatorsOf.all_2 = function(__self, cb) {
for ( const it_1 of __self) {
if ( cb(it_1) == false ) {
return false;
}
}
return true;
};
operatorsOf.slice_3 = function(__self, start, end) {
let res = [];
const __len = __self.length;
let from = start;
let to = end;
if ( from < 0 ) {
from = 0;
}
if ( to > __len ) {
to = __len;
}
let i_2 = from;
while (i_2 < to) {
res.push(__self[i_2]);
i_2 = i_2 + 1;
};
return res;
};
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const numbers = [1, 2, 3, 4, 5];
if ( operatorsOf.any_2(numbers, ((item) => {
return item > 4;
})) ) {
console.log("one item is greater than four");
}
if ( operatorsOf.all_2(numbers, ((item) => {
return item > 0;
})) ) {
console.log("every item is positive");
}
const part = operatorsOf.slice_3(numbers, 1, 3);
console.log("part " + part.length);
}
__js_main();
The Ranger source of the method
fn any:boolean (cb:(_:boolean (item:T))) @doc('True when `cb` holds for at least one item') {
for self it@(lives):T i {
if( cb(it) ) {
return true
}
}
return false
}

Definition: lib/stdlib.rgr, line 817

clone

receiver.clone() → [T]

The compiler writes this method for every target.

The Ranger source of the method
fn clone:[T] () {
def res:[T]
for self it:T i {
push res it
}
return res
}

Definition: lib/stdlib.rgr, line 776

contains

receiver.contains(cb) → boolean
contains(cb: (fn:boolean (item:T)))

The compiler writes this method for every target.

The Ranger source of the method
fn contains:boolean (cb:(_:boolean (item:T))) {
for self it@(lives):T i {
if( cb(it) ) {
return true
}
}
return false
}

Definition: lib/stdlib.rgr, line 808

count

receiver.count(cb) → int
count(cb: (fn:boolean (item:T)))

The compiler writes this method for every target.

The Ranger source of the method
fn count:int (cb:(_:boolean (item:T))) {
def res 0
for self it@(lives):T i {
if( cb(it) ) {
res = res + 1
}
}
return res
}

Definition: lib/stdlib.rgr, line 798

filter

receiver.filter(cb) → [T]
filter(cb: (fn:boolean (item:T index:int)))

The compiler writes this method for every target.

Change and select the items of an array

docs/examples/lib/stdlib/collection-methods.rgr
Import "stdlib.rgr"
class Main {
sfn main:void () {
def numbers:[int] ([] _:int (1 2 3 4 5))
def doubled (numbers.map({ return (item * 2) }))
def large (numbers.filter({ return (item > 3) }))
print ("doubled " + (array_length doubled))
print ("large " + (array_length large))
}
}
main function, JavaScript
const numbers = [1, 2, 3, 4, 5];
const doubled = operatorsOf.map_2(numbers, ((item, index) => {
return item * 2;
}));
const large = operatorsOf.filter_3(numbers, ((item, index) => {
return item > 3;
}));
console.log("doubled " + doubled.length);
console.log("large " + large.length);
The complete file
class Main {
constructor() {
}
}
class operatorsOf {
constructor() {
}
}
operatorsOf.map_2 = function(__self, cb) {
const __len = __self.length;
let res = [];
for ( let i = 0; i < __self.length; i++) {
var it = __self[i];
res.push(cb(it, i));
}
return res;
};
operatorsOf.filter_3 = function(__self, cb) {
let res_1 = [];
for ( let i_1 = 0; i_1 < __self.length; i_1++) {
var it_1 = __self[i_1];
if ( cb(it_1, i_1) ) {
res_1.push(it_1);
}
}
return res_1;
};
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const numbers = [1, 2, 3, 4, 5];
const doubled = operatorsOf.map_2(numbers, ((item, index) => {
return item * 2;
}));
const large = operatorsOf.filter_3(numbers, ((item, index) => {
return item > 3;
}));
console.log("doubled " + doubled.length);
console.log("large " + large.length);
}
__js_main();
The Ranger source of the method
fn filter:[T] (cb:(_:boolean (item:T index:int))) {
def res:[T]
for self it:T i {
if( cb(it i) ) {
push res it
}
}
return res
}

Definition: lib/stdlib.rgr, line 733

find

receiver.find(cb) → T
find(cb: (fn:boolean (item:T)))

some useful functions from scala

The compiler writes this method for every target.

The Ranger source of the method
fn find@(optional):T (cb:(_:boolean (item:T))) {
def res@(optional):T
for self it@(lives):T i {
if( cb(it) ) {
def res2@(optional):T
res2 = it
return res2
}
}
return res
}

Definition: lib/stdlib.rgr, line 786

forEach

receiver.forEach(cb) → void
forEach(cb: (fn:void (item:T index:int)))

Call `fb` for each item in array

The compiler writes this method for every target.

The Ranger source of the method
fn forEach@(pure):void (cb:(_:void (item:T index:int))) @doc('Call `fb` for each item in array') {
for self it:T i {
cb(it i)
}
}

Definition: lib/stdlib.rgr, line 704

groupBy

receiver.groupBy(cb) → [T]
groupBy(cb: (fn:string (item:T)))

The compiler writes this method for every target.

The Ranger source of the method
fn groupBy:[T] ( cb:(_:string (item:T)) ) {
def res:[T]
def mapper:[string:boolean]
for self it:T i {
def key (cb(it))
if( false == ( has mapper key ) ) {
push res it
set mapper key true
}
}
return res
}

Definition: lib/stdlib.rgr, line 763

has

receiver.has(el) → boolean
has(el: T)

The compiler writes this method for every target.

The Ranger source of the method
fn has:boolean ( el:T) {
def idx (indexOf el)
return (idx >= 0)
}

Definition: lib/stdlib.rgr, line 710

map

receiver.map(cb to) → [S]
map(cb: (fn:S (item:T index:int)), to: [S])

The compiler writes this method for every target.

The Ranger source of the method
fn map:[S] (cb:(_:S (item:T index:int)) to@(noeval):[S] ) {
def len (array_length self)
def res:[S]
for self it:T i {
push res (cb(it i))
}
return res
}

Definition: lib/stdlib.rgr, line 724

map

receiver.map(cb) → [T]
map(cb: (fn:T (item:T index:int)))

The compiler writes this method for every target.

Change and select the items of an array

docs/examples/lib/stdlib/collection-methods.rgr
Import "stdlib.rgr"
class Main {
sfn main:void () {
def numbers:[int] ([] _:int (1 2 3 4 5))
def doubled (numbers.map({ return (item * 2) }))
def large (numbers.filter({ return (item > 3) }))
print ("doubled " + (array_length doubled))
print ("large " + (array_length large))
}
}
main function, JavaScript
const numbers = [1, 2, 3, 4, 5];
const doubled = operatorsOf.map_2(numbers, ((item, index) => {
return item * 2;
}));
const large = operatorsOf.filter_3(numbers, ((item, index) => {
return item > 3;
}));
console.log("doubled " + doubled.length);
console.log("large " + large.length);
The complete file
class Main {
constructor() {
}
}
class operatorsOf {
constructor() {
}
}
operatorsOf.map_2 = function(__self, cb) {
const __len = __self.length;
let res = [];
for ( let i = 0; i < __self.length; i++) {
var it = __self[i];
res.push(cb(it, i));
}
return res;
};
operatorsOf.filter_3 = function(__self, cb) {
let res_1 = [];
for ( let i_1 = 0; i_1 < __self.length; i_1++) {
var it_1 = __self[i_1];
if ( cb(it_1, i_1) ) {
res_1.push(it_1);
}
}
return res_1;
};
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const numbers = [1, 2, 3, 4, 5];
const doubled = operatorsOf.map_2(numbers, ((item, index) => {
return item * 2;
}));
const large = operatorsOf.filter_3(numbers, ((item, index) => {
return item > 3;
}));
console.log("doubled " + doubled.length);
console.log("large " + large.length);
}
__js_main();
The Ranger source of the method
fn map:[T] (cb:(_:T (item:T index:int))) {
def len (array_length self)
def res:[T]
for self it:T i {
push res (cb(it i))
}
return res
}

Definition: lib/stdlib.rgr, line 715

reduce

receiver.reduce(cb initialValue) → K
reduce(cb: (fn:K (left:K right:T index:int)), initialValue: K)

The compiler writes this method for every target.

The Ranger source of the method
fn reduce@(weak):K (cb:(_:K (left:K right:T index:int)) initialValue:K) {
def len (array_length self)
def res initialValue
if( len >= 1 ) {
for self it:T i {
res = ( cb ( res it i ))
}
}
return res
}

Definition: lib/stdlib.rgr, line 752

reduce

receiver.reduce(cb initialValue) → T
reduce(cb: (fn:T (left:T right:T index:int)), initialValue: T)

The compiler writes this method for every target.

The Ranger source of the method
fn reduce@(weak):T (cb:(_:T (left:T right:T index:int)) initialValue:T) {
def len (array_length self)
def res:T initialValue
if( len >= 1 ) {
for self it:T i {
res = ( cb ( res it i))
}
}
return res
}

Definition: lib/stdlib.rgr, line 742

slice

receiver.slice(start end) → [T]
slice(start: int, end: int)

Items from `start` up to but not including `end`

The compiler writes this method for every target.

Test the items of an array and take a part of it

docs/examples/lib/stdlib/tests-and-slice.rgr
Import "stdlib.rgr"
class Main {
sfn main:void () {
def numbers:[int] ([] _:int (1 2 3 4 5))
if (numbers.any({ return (item > 4) })) {
print "one item is greater than four"
}
if (numbers.all({ return (item > 0) })) {
print "every item is positive"
}
def part (numbers.slice(1 3))
print ("part " + (array_length part))
}
}
main function, JavaScript
const numbers = [1, 2, 3, 4, 5];
if ( operatorsOf.any_2(numbers, ((item) => {
return item > 4;
})) ) {
console.log("one item is greater than four");
}
if ( operatorsOf.all_2(numbers, ((item) => {
return item > 0;
})) ) {
console.log("every item is positive");
}
const part = operatorsOf.slice_3(numbers, 1, 3);
console.log("part " + part.length);
The complete file
class Main {
constructor() {
}
}
class operatorsOf {
constructor() {
}
}
operatorsOf.any_2 = function(__self, cb) {
for ( const it of __self) {
if ( cb(it) ) {
return true;
}
}
return false;
};
operatorsOf.all_2 = function(__self, cb) {
for ( const it_1 of __self) {
if ( cb(it_1) == false ) {
return false;
}
}
return true;
};
operatorsOf.slice_3 = function(__self, start, end) {
let res = [];
const __len = __self.length;
let from = start;
let to = end;
if ( from < 0 ) {
from = 0;
}
if ( to > __len ) {
to = __len;
}
let i_2 = from;
while (i_2 < to) {
res.push(__self[i_2]);
i_2 = i_2 + 1;
};
return res;
};
/* static JavaSript main routine at the end of the JS file */
function __js_main() {
const numbers = [1, 2, 3, 4, 5];
if ( operatorsOf.any_2(numbers, ((item) => {
return item > 4;
})) ) {
console.log("one item is greater than four");
}
if ( operatorsOf.all_2(numbers, ((item) => {
return item > 0;
})) ) {
console.log("every item is positive");
}
const part = operatorsOf.slice_3(numbers, 1, 3);
console.log("part " + part.length);
}
__js_main();
The Ranger source of the method
fn slice:[T] (start:int end:int) @doc('Items from `start` up to but not including `end`') {
def res:[T]
def len (array_length self)
def from start
def to end
if( from < 0 ) {
from = 0
}
if( to > len ) {
to = len
}
def i from
while( i < to ) {
push res (itemAt self i)
i = i + 1
}
return res
}

Definition: lib/stdlib.rgr, line 835

Method Arguments Gives Targets
forEach cb: (fn:void (item:T index:K)) S every target
get key: K T every target
has key: K boolean every target
keys [K] every target
set key: K, value: T S every target

forEach

receiver.forEach(cb) → S
forEach(cb: (fn:void (item:T index:K)))

The compiler writes this method for every target.

The Ranger source of the method
fn forEach@(weak):S ( cb:(_:void (item:T index:K)) ) {
def keys (keys self.elements)
for keys key:K i {
cb( (unwrap (get self.elements key)) key )
}
return self
}

Definition: lib/stdlib.rgr, line 1182

get

receiver.get(key) → T
get(key: K)

The compiler writes this method for every target.

The Ranger source of the method
fn get@(optional):T (key:K) {
return (get self.elements key)
}

Definition: lib/stdlib.rgr, line 1163

has

receiver.has(key) → boolean
has(key: K)

The compiler writes this method for every target.

The Ranger source of the method
fn has:boolean (key:K) {
return (has self.elements key)
}

Definition: lib/stdlib.rgr, line 1166

keys

receiver.keys() → [K]

The compiler writes this method for every target.

The Ranger source of the method
fn keys:[K] () {
return (keys self.elements)
}

Definition: lib/stdlib.rgr, line 1160

set

receiver.set(key value) → S
set(key: K, value: T)

The compiler writes this method for every target.

The Ranger source of the method
fn set:S (key:K value:T) {
def c (new S)
def keys ( keys self.elements)
for keys k:string i {
if(k==key) {
} {
set c.elements k (unwrap (get self.elements k))
}
}
set c.elements key value
return c
}

Definition: lib/stdlib.rgr, line 1169

Method Arguments Gives Targets
array_length int every target
at idx: int T every target
clear idx: int T every target
forEach cb: (fn:void (item:T)) void every target
forUntil cb: (fn:boolean (item:T)) void every target
indexOf elem: T int every target
itemAt idx: int T every target
last T every target
length int every target
map cb: (fn:K (item:T)), to: [K] [K] every target
map cb: (fn:T (item:T)) S every target
push item: T S every target
size int every target

array_length

receiver.array_length() → int

The compiler writes this method for every target.

The Ranger source of the method
fn array_length:int () {
return (self.count())
}

Definition: lib/stdlib.rgr, line 1099

at

receiver.at(idx) → T
at(idx: int)

The compiler writes this method for every target.

The Ranger source of the method
fn at:T (idx:int) {
def val (self.get(idx))
return val
}

Definition: lib/stdlib.rgr, line 1089

clear

receiver.clear(idx) → T
clear(idx: int)

The compiler writes this method for every target.

The Ranger source of the method
fn clear:T (idx:int) {
; def val (self.get(idx))
return (new T)
}

Definition: lib/stdlib.rgr, line 1080

forEach

receiver.forEach(cb) → void
forEach(cb: (fn:void (item:T)))

The compiler writes this method for every target.

The Ranger source of the method
fn forEach:void (cb:(_:void (item:T))) {
def cnt (self.count())
def i 0
while( i < cnt ) {
def item (itemAt self i)
cb(item)
i = i + 1
}
}

Definition: lib/stdlib.rgr, line 1108

forUntil

receiver.forUntil(cb) → void
forUntil(cb: (fn:boolean (item:T)))

The compiler writes this method for every target.

The Ranger source of the method
fn forUntil:void (cb:(_:boolean (item:T))) {
def cnt (self.count())
def i 0
while( i < cnt ) {
def item (itemAt self i)
if( (cb(item)) == false ) {
return
}
i = i + 1
}
}

Definition: lib/stdlib.rgr, line 1118

indexOf

receiver.indexOf(elem) → int
indexOf(elem: T)

TODO: strict type checking for this fn for (iIndex@(ignore):T iteIndex@(ignore):int code:block ) ( "def " (e 3) ":int 0" nl "def loop_cnt (" (e 1) ".count())" nl "while( " (e 3) " < loop_cnt ) {" nl I "def " (e 2) " (" (e 1) ".get(" (e 3) "))" nl (block 4) (e 3) " = " (e 3) " + 1" nl i "}" nl )

The compiler writes this method for every target.

The Ranger source of the method
fn indexOf:int ( elem:T ) {
def cnt (self.count())
def i 0
while( i < cnt ) {
def item (itemAt self i)
if(item == elem) {
return i
}
i = i + 1
}
return -1
}

Definition: lib/stdlib.rgr, line 1068

itemAt

receiver.itemAt(idx) → T
itemAt(idx: int)

The compiler writes this method for every target.

The Ranger source of the method
fn itemAt:T (idx:int) {
def val (self.get(idx))
return val
}

Definition: lib/stdlib.rgr, line 1085

last

receiver.last() → T

The compiler writes this method for every target.

The Ranger source of the method
fn last:T () {
return (itemAt self ((self.count()) - 1) )
}

Definition: lib/stdlib.rgr, line 1096

length

receiver.length() → int

The compiler writes this method for every target.

The Ranger source of the method
fn length:int () {
return (self.count())
}

Definition: lib/stdlib.rgr, line 1102

map

receiver.map(cb to) → [K]
map(cb: (fn:K (item:T)), to: [K])

TODO: faster version ....

The compiler writes this method for every target.

The Ranger source of the method
fn map:[K] (cb:(_:K (item:T)) to@(noeval):[K] ) {
def res:[K]
def cnt (self.count())
def i 0
while( i < cnt ) {
def item (itemAt self i)
push res (cb(item))
i = i + 1
}
return res
}

Definition: lib/stdlib.rgr, line 1144

map

receiver.map(cb) → S
map(cb: (fn:T (item:T)))

The compiler writes this method for every target.

The Ranger source of the method
fn map:S ( cb:(_:T (item:T)) ) {
def res (new S)
def cnt (self.count())
def i 0
while( i < cnt ) {
def item (itemAt self i)
def value (cb(item))
res = res.add(value)
i = i + 1
}
return res
}

Definition: lib/stdlib.rgr, line 1130

push

receiver.push(item) → S
push(item: T)

The compiler writes this method for every target.

The Ranger source of the method
fn push:S (item:T) {
return (self.add(item))
}

Definition: lib/stdlib.rgr, line 1093

size

receiver.size() → int

The compiler writes this method for every target.

The Ranger source of the method
fn size:int () {
return (self.count())
}

Definition: lib/stdlib.rgr, line 1105

Ranger 3.5.1 · commit f323fd4 · development build