functional
Contains functions for operating data in functional style
Constants
IDENTITY : function = def(x) = x
function which returns passed argument
Functions
chain(data, functions...)
combine(functions...) — combines functions
use functional
def f1() = 2
def f2(a) = a*2
def f3(a) = a/4
f = combine(::f1, ::f2, ::f3)
println f() // 1
// same as
f = def() = f3(f2(f1()))
println f() // 1
dropwhile(data, predicate) — skips elements while predicate function returns true
filter(data, predicate) — filters array or object.
predicate is a function which takes one argument for arrays or two arguments for objects
use functional
nums = [1,2,3,4,5]
print filter(nums, def(x) = x % 2 == 0) // [2, 4]
flatmap(array, mapper) — converts each element of an array to other array
use functional
nums = [1,2,3,4]
print flatmap(nums, def(x) {
  arr = newarray(x)
  for i = 0, i < x, i++
    arr[i] = x
  return arr
}) // [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
foreach(data, consumer) — invokes function consumer for each element of array or map data
If data - массив, то в функции consumer необходим один параметр, если объект - два (ключ и значение).
use functional
foreach([1, 2, 3], def(v) { print v })
foreach({"key": 1, "key2": "text"}, def(key, value) {
  print key + ": " + value
})
map(data, mapper...) — converts elements of array or map. If data is array - mapper converts his elements, if data is object - you need to pass keyMapper - converts keys and valueMapper - converts values
use functional
nums = [3,4,5]
print map(nums, def(x) = x * x) // [9, 16, 25]
reduce(data, identity, accumulator) — converts elements of an array or a map to one value, e.g. sum of elements or concatenation string. accumulator takes one argument for array and two arguments for object (key and value).
use functional
nums = [1,2,3,4,5]
print reduce(nums, 0, def(x, y) = x + y) // 15
sortby(array, function) — sorts elements of an array or a map by function result
use functional
data = [
  {"k1": 2, "k2": "x"},
  {"k1": 7, "k2": "d"},
  {"k1": 4, "k2": "z"},
  {"k1": 5, "k2": "p"},
]
println sortby(data, def(v) = v.k1) // [{k1=2, k2=x}, {k1=4, k2=z}, {k1=5, k2=p}, {k1=7, k2=d}]
println sortby(data, def(v) = v.k2) // [{k1=7, k2=d}, {k1=5, k2=p}, {k1=2, k2=x}, {k1=4, k2=z}]
groupby(data, function) 2.0.0 — groups elements of an array or a map by function result
use functional
data = [
  {"k1": 2, "k2": "x"},
  {"k1": 4, "k2": "z"},
  {"k1": 5, "k2": "p"},
]
println groupby(data, def(e) = e.k1) // {"2"=[{k1=2, k2=x}], "4"=[{k1=4, k2=z}], "5"=[{k2=p, k1=5}]}
println groupby(data, def(e) = e.k2) // {"x"=[{k1=2, k2=x}], "z"=[{k1=4, k2=z}], "p"=[{k2=p, k1=5}]}
tomap(data, keyMapper, valueMapper = def(v) = v, merger = def(oldValue, newValue) = newValue) 2.0.0 — converts elements of an array or a map to a map based on keyMapper and valueMapper functions result. merger function resolves collisions
use functional
data = ["apple", "banana"]
println tomap(data, def(str) = str.substring(0, 1)) // {"a": "apple", "b": "banana"}
println tomap(data, def(str) = str.substring(0, 1), ::toUpperCase) // {"a": "APPLE", "b": "BANANA"}
stream(data) — creates stream from data and returns StreamValue
takewhile(data, predicate) — takes elements while predicate function returns true
Types
 StreamValue
Functions
filter(func) — filters elements based on predicate function result (true - remain, false - drop)
filterNot(func) 2.0.0 — filters elements based on negated predicate function result (false - remain, true - drop)
map(func) — converts each element
flatMap(func) — converts each element to array
sorted(func) — sorts elements with comparator function
sortBy(func) — applies function, then sorts elements
groupBy(func) 2.0.0 — groups elements based on function result
takeWhile(func) — takes elements while predicate function returns true
dropWhile(func) — skips elements while predicate function returns true, returns remaining elements
peek(func) — executes function for each element and returns stream
skip(count) — skips count elements
limit(count) — limits elements size
custom(func) — performs custom operation
use std, functional
println stream([1, 2, 3, 4])
 .custom(::reverse)
 .toArray()
def reverse(container) {
  size = length(container)
  result = newarray(size)
  for i : range(size) {
    result[size - i - 1] = container[i]
  }
  return result
}
reduce(func) — converts elements to one value
forEach(func) — executes function for each element
forEachIndexed(func) 2.0.0 — executes function for each element and its index
joining(delimiter = "", prefix = "", suffix = "") — joins elements into a string
toArray() — returns array of elements
toMap(keyMapper, valueMapper = def(v) = v, merger = def(oldValue, newValue) = newValue) 2.0.0 — converts elements to a map based on keyMapper and valueMapper functions result. merger function resolves collisions
anyMatch(predicate) 2.0.0 — returns true if there is any element matching the given predicate, otherwise returns false
allMatch(predicate) 2.0.0 — returns true if all elements match the given predicate, otherwise returns false
noneMatch(predicate) 2.0.0 — returns true if no elements match the given predicate, otherwise returns false
count() — returns the elements count