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

Example

f = combine(::f1, ::f2, ::f3)
// same as
f = def(f1, f2, f3) = f3(f2(f1))

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

Example

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

Example

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 необходим один параметр, если объект - два (ключ и значение).

Example

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

Example

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

Example

nums = [1,2,3,4,5]
print reduce(nums, 0, def(x, y) = x + x) // 15

sortby(array, function) - sorts elements of an array or an object by function result

Example

data = [
 {"k1": 2, "k2": "x"},
 {"k1": 7, "k2": "d"},
 {"k1": 4, "k2": "z"},
 {"k1": 5, "k2": "p"},
]
print sortby(data, def(v) = v.k1) // [{k1=2, k2=x}, {k1=4, k2=z}, {k1=5, k2=p}, {k1=7, k2=d}]
print sortby(data, def(v) = v.k2) // [{k1=7, k2=d}, {k1=5, k2=p}, {k1=2, k2=x}, {k1=4, k2=z}]

stream(data) - creates stream from data and returns StreamValue

StreamValue functions:

  • filter(func) - filters elements

  • map(func) - converts each element

  • flatMap(func) - converts each element to array

  • sortBy(func) - sorts elements by comparator function

  • takeWhile(func) - takes elements while predicate function returns true

  • dropWhile(func) - skips elements while predicate function returns true

  • skip(count) - skips count elements

  • limit(count) - limits elements size

  • custom(func) - performs custom operation

  • reduce(func) - converts elements to one value

  • forEach(func) - executes function for each element

  • toArray() - returns array of elements

  • count() - returns count of elements

takewhile(data, predicate) - takes elements while predicate function returns true

results matching ""

    No results matching ""