Skip to main content

Collection Methods

Collection Methods apply the following data types:

  • Tuple, Map, List of Primitives, Map of Primitives, JsonArray and JsonObj
  • All collection method take a lambda as argument
  • (k, v) ⇒ {} for map type collections
  • (i, v) ⇒ {} for list type collections.

Map

  • Map()
    • return a new list populated with the results of calling a provided function on every element in the calling collection.
let arr = [1, 2, 3]
let arr2 = arr.Map((k, v) => v + 1)
printf("%v", arr2) // prints [2,3,4]

Filter

  • Filter()
    • return a new list, only keeping elements that return true by the provided lambda
let arr = [1, 2, 3]
let arr2 = arr.Filter((k, v) => (v % 2 == 1))
printf("%v", arr2) // prints [1,3]

Some

  • Some()
    • return true if one of the element return true
let arr = [1, 2, 3, 4, 5]
let t = arr.Some((_,e) => e == 5)
printf("%v", t) // prints true as arr contains 5
let f = arr.Some((_,e) => e == 6)
printf("%v", f) // prints false as arr does not contain 6

Find

  • Find()
    • return the value element that return true by the provided lambda
    • return undefined otherwise
let arr = [
{ID: "a", Col1: "x"},
{ID: "b", Col1: "y"}
]
let t = arr.Find((_,e) => e.Col1 == "x")
if (t) {
printf("Find: %v", f.ID) // prints "a"
}

Table

  • Table()
    • create a table object.
let s = `
[
{"ID": "a", "Col1":"foo" },
{"ID": "b", "Col2":"bar" }
]`
let j = parseJson(s)
let t1 = j.Table( (_, obj) => {
return {
ID: obj.ID,
Col1: obj.Col1,
Col2: obj.Col2
}
})

Join

  • Join(delim)
    • only apply to list type collections
let arr = [1, 2, 3]
let s = arr.Join(",") // s is now the string "1,2,3"