Data Structures¶
This page describes the available data structures in Scolvo script language.
Collections¶
It represents a group of objects, known as its elements.
1var collection = [
2 {
3 "key": "value1"
4 },
5 {
6 "key": "value2"
7 }];
Filtering¶
The filtering of a collection can be done using an inline function. The function has to return true on items to be kept, and it returns a new collection consisting of the kept items.
1var collection = [
2 {
3 "itemName": "something"
4 },
5 {
6 "itemName": "other"
7 }];
8var result = collection.filter(function(current) {
9 return current.itemName == "something";
10});
Iterating¶
The iteration on the items of a collection can be done using an inline function. The function receives the current item, and it can execute the business logic.
1var result = 0;
2[1, 2, 3, 4].each(function(current) {
3 result += current;
4});
It is also possible to use a predefined function when iterating through the items:
1var result = "";
2var collection = [
3 {
4 "itemName": "something"
5 },
6 {
7 "itemName": "other"
8 }];
9
10function append(current) {
11 result += current.itemName;
12};
13
14collection.each(append);
Iterating over a variable containing JsonArray using inline function declaration (works with any collection type).
1var result = "";
2var collection = [
3 {
4 "itemName": "something"
5 },
6 {
7 "itemName": "other"
8 }];
9
10collection.each(function(current){
11 result += current.itemName;
12});
The each function returns a collection, so the function calls can be chained. Each one returns the same collection, so if an item is modified, it will be reflected on original instance as well (Consider using clones if required).
1var result = "";
2var collection = [
3 {
4 "itemName": "something"
5 },
6 {
7 "itemName": "other"
8 }];
9
10function extend(current) {
11 current.put("itemDescription", "newValue");
12}
13
14collection.each(extend).each(function(currentItem) {
15 result += currentItem.itemDescription;
16});
Sorting¶
The sort function of a collection returns a java.util.List. The function has 1 parameter - a sorting function with 2 items as parameter returning -1, 0, 1 values according to the sorting logic. It works as the Comparable interface of Java.
1var collection = [2, 3, 1, 4, 5].sort(function(a, b) {
2 if (a < b) {
3 return -1;
4 } else if (a > b) {
5 return 1;
6 } else {
7 return 0;
8 }
9});
Joining¶
The join function takes the itmes of the collection and returns a concatenated String by taking the collection items’ toString() method and the separator parameter.
1var fullContent = [1, 2, 3, 4].join(" ");
Slicing¶
Slicing is a concept in Python for taking a part of a collection using one or two parameters. If one parameter is given, then it returns elements from 0 position until the given position parameter. When 2 parameters are defined then it returns the items from the first position parameter to the second position parameter. If negative parameters are given, then the index value is calculated to left out the given number of elements (size of the collection + given negative number -1).
1// [1, 2, 3] items from 0th through 2nd position
2var result = [1, 2, 3, 4, 5].slice(2);
3
4// [2, 3, 4] items from 1st through 3rd
5result = [1, 2, 3, 4, 5].slice(1, 3);
6
7// [1, 2] items from 0th through (size -3 -1)th
8result = [1, 2, 3, 4, 5].slice(-3);
9
10// [1, 2, 3, 4, 5] no array index out of bounds exception
11result = [1, 2, 3, 4, 5].slice(7);
12
13// no array index out of bounds exception
14result = [1, 2, 3, 4, 5].slice(-7);
15
16//[4, 5] items from 3rd to last
17result = [1, 2, 3, 4, 5].slice(3, null);
ToString¶
The toString method returns JSON string representation of underlying collection object.
1var result = [1, 2, 3, 4, 5].toString();
Deep clone¶
The deepClone method copies all fields, and makes copies of dynamically allocated memory pointed to by the fields.
1var collection = [
2 {
3 "itemName": "something"
4 },
5 {
6 "itemName": "other"
7 }];
8
9var newCollection = collection.deepClone();
Remove by index¶
The removeByIdx method removes an element by index and returns it.
1var collection = ["one", "two", "three"];
2
3// removedElement value is "two"
4var removedElement = collection.removeByIdx(1);
Maps¶
It represents a key-value map like the java.util.Map. The interface changes in Java8 edition are separately implemented. All methods stated below conform with the definition of corresponding java.util.Map javadoc of Java7, where it is not stated otherwise.
1var map = {};
Get¶
The get method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. It is also possible to get the appropriate value using the key as property.
1var map = {
2 "myKey": "myValue"
3};
4
5var myValue = map.get("myKey");
6var myValueByProperty = map.myKey;
GetOrDefault¶
The getOrDefault method is not part of method Java 7 map implementation, this function is implemented according to the Java 8 Map API. It returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
1var map = {
2 "myKey": "myValue"
3};
4
5var value = map.getOrDefault("nonExistingKey", "default value");
Put¶
The method associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)
1var data = {};
2data.put("data","stringData");
3data.put("statusCode", 400);
4var result = data.statusCode;
Remove¶
It removes the mapping for a key from this map if it is present (optional operation). Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.
1var data = {
2 "data": "stringData",
3 "statusCode": 400
4};
5
6var statusCode = data.remove("statusCode");
ContainsKey¶
It returns true if this map contains a mapping for the specified key.
1var data = {
2 "existing": "value"
3};
4
5var isThereExisting = data.containsKey("existing");
6var isThereNonExisting = data.containsKey("non-existing");
Size¶
It returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
1var data = {
2 "existing": "value"
3};
4
5var mapSize = data.size();
IsEmpty¶
It returns true if this map contains no key-value mappings.
1var data = {
2 "existing": "value"
3};
4
5if (data.isEmpty()) {
6 info("Data is empty");
7} else {
8 info("Data has " + data.size() + " key(s).");
9}
Clear¶
It removes all of the mappings from this map.
1var data = {
2 "existing": "value"
3};
4
5data.clear();
KeySet¶
Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
1var data = {
2 "existing": "value"
3};
4
5if (data.keySet().containsKey("exisiting")) {
6 info("Key is existing by keySet()");
7}
Values¶
Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa.
1var data = {
2 "existing": "value"
3};
4
5var values = data.values();
Equals¶
Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings.
1var data = {
2 "existing": "value"
3};
4var otherData = {
5 "existing": "value"
6};
7
8var areEqual = data.equals(otherData);
HashCode¶
It returns the hash code value for the map.
Map ToString¶
It returns the Json representation of the map.
1var data = {
2 "existing": "value"
3};
4
5info("The content of the map: " + data.toString());
Map DeepClone¶
It creates a clone from the map using Json serialization/deserialization.
1var user = {"key": "value"};
2var userClone = user.deepClone();
3user.remove("key");