Data Structure Functions#

enumerate - Enumerate Lists#

Combines the elements of a list with their index.

import ison

dicData = {
    "__locals__": {
        "lA": ["a", "b", "c"],
    },
    "result": "$enumerate{$lA}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": [
        [
            0,
            "a"
        ],
        [
            1,
            "b"
        ],
        [
            2,
            "c"
        ]
    ]
}

This can be used in the context of the $!foreach{} function.

import ison

dicData = {
    "__func_locals__": {
        "func": "$L{%0: >%1<}",
    },

    "__locals__": {
        "lA": ["a", "b", "c"]
    },

    "result": "$!foreach{$func, *$enumerate{$lA}}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": [
        "0: >a<",
        "1: >b<",
        "2: >c<"
    ]
}

group - Group of lists combination#

Combine the elements of a number of lists w.r.t. their index in tuples. Especially useful in combination with the $foreach{} function.

import ison

dicData = {
    "__func_locals__": {
        "func": "$L{%0: >%1< - %2}",
    },

    "__locals__": {
        "lA": ["a", "b", "c"],
        "lB": [1,  2, 3],
        "lC": ["x", "y", "z"]
    },

    "result": "$!foreach{$func, *$group{$lA, $lB, $lC}}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": [
        "a: >1< - x",
        "b: >2< - y",
        "c: >3< - z"
    ]
}

range - List of indices#

Generates a list of indices in the given range. Here are typical examples:

  • $range{5}: A list with 5 elements starting at 0, [0, 1, 2, 3, 4]

  • $range{0, 5}: A list with first element 0 and last element 5, [0, 1, 2, 3, 4, 5]

  • $range{0, 5, 2}: As above with increment 2, [0, 2, 4]

import ison

dicData = {
    "range(5)": "$range{5}",
    "range(0, 5)": "$range{0, 5}",
    "range(0, 5, 2)": "$range{0, 5, 2}"
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "range(5)": [
        0,
        1,
        2,
        3,
        4
    ],
    "range(0, 5)": [
        0,
        1,
        2,
        3,
        4,
        5
    ],
    "range(0, 5, 2)": [
        0,
        2,
        4
    ]
}

sort - Sort a List#

This sorts a list in ascending or descending order. The default is ascending order. If the second argument is true, the order is reversed.

import ison

dicData = {
    "__locals__": {
        "lA": ["b", "a", "c"],
    },

    "result 1": "$sort{$lA}",
    "result 2": "$sort{$lA, true}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result 1": [
        "a",
        "b",
        "c"
    ],
    "result 2": [
        "c",
        "b",
        "a"
    ]
}

union - Union of lists or dictionaries#

This concatenates a group of lists into one list, or combines a group of dictionaries in a single one.

import ison

dicData = {
    "__locals__": {
        "lA": ["a", "b", "c"],
        "lB": [1,  2, 3],
        "lC": ["x", "y", "z"]
    },

    "result": "$union{$lA, $lB, $lC}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": [
        "a",
        "b",
        "c",
        1,
        2,
        3,
        "x",
        "y",
        "z"
    ]
}

When using union on dictionaries, the order is also important, as the later dictionaries will overwrite elements of the previous dictionaries.

import ison

dicData = {
    "__locals__": {
        "dicA": {"a": 1, "b": 2, "c": 3},
        "dicB": {"x": 4, "a": 5, "y": 6},
    },

    "result": "$union{$dicA, $dicB}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": {
        "a": 5,
        "b": 2,
        "c": 3,
        "x": 4,
        "y": 6
    }
}

len - Length of a List#

Gives the lenght of a list.

import ison

dicData = {
    "__locals__": {
        "lA": ["a", "b", "c"]
    },

    "result": "$len{$lA}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": 3
}

circularselect - List element selection based on modulo circle.#

Selects a list element based on a modulo circle. Cannot go out of bounds as long as the index is an integer.

import ison

dicData = {
    "__locals__": {
        "lA": ["a", "b", "c", "d", "e"]
    },

    "result1": "$circularselect{$lA, -2}",
    "result2": "$circularselect{$lA, 2}",
    "result3": "$circularselect{$lA, 11}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result1": "d",
    "result2": "c",
    "result3": "b"
}