Skip to content

Dictionaries

Dictionaries in RuLA are collections of key-value pairs. Each key is unique and maps to a value. Keys are strings, while values can be any type.

Creating Dictionaries

Use curly braces with key: value pairs:

{'name': 'John', 'age': 30}
{'x': 1, 'y': 2, 'z': 3}
{}                                      # Empty dictionary
{'items': ['a', 'b'], 'count': 2}       # Nested list

Info

New in Atfinity 17: A bare identifier in a dictionary literal is shorthand for key: 'key'. See Dictionary shorthand.

{a, b}                  // => {'a': 'a', 'b': 'b'}
{a, b: 'two', c}        // => {'a': 'a', 'b': 'two', 'c': 'c'}

Accessing Dictionary Values

Access values by their key using square brackets:

person = {'name': 'John', 'age': 30}
person['name']          # Returns 'John'
person['age']           # Returns 30

Dictionary Manipulation

Checking for Keys

person = {'name': 'John', 'age': 30}

'name' in person        # Returns True
'address' in person     # Returns False
'address' not in person # Returns True

Getting All Keys

person = {'name': 'John', 'age': 30}
person.keys()           # Returns ['name', 'age']

Counting Entries

COUNT(person)           # Returns 2 (number of key-value pairs)

Useful Dictionary Functions

Function Description Example
keys() Get all keys as a list {'a': 1, 'b': 2}.keys() → ['a', 'b']
COUNT Number of key-value pairs COUNT({'a': 1, 'b': 2}) → 2
to_dict (new in Atfinity 17) Build a dictionary from selected instance attributes p.to_dict({first_name, last_name})
pick (new in Atfinity 17) Return a dictionary with only the selected keys (optionally renamed) {'a': 1, 'b': 2}.pick({a}) → {'a': 1}
merge_dicts (or \|) (new in Atfinity 17) Merge any number of dictionaries (later wins) {'a': 1} \| {'b': 2} → {'a': 1, 'b': 2}

Dictionary Operations

Working with Lists of Dictionaries

people = [
    {'name': 'Alice', 'age': 30},
    {'name': 'Bob', 'age': 25}
]

# Extract a field from each dictionary
MAP(people, p => p['name'])     # Returns ['Alice', 'Bob']

# Filter by a field
FILTER(people, p => p['age'] > 25)  # Returns [{'name': 'Alice', 'age': 30}]

# Sort by a field
SORT(people, 'age')            # Returns [{'name': 'Bob', ...}, {'name': 'Alice', ...}]

Checking Dictionary Contents

person = {'name': 'John', 'city': 'Zurich'}

# Check if key exists
'name' in person                # True
'email' not in person           # True

# Use in conditions
if 'city' in person then person['city'] else 'Unknown' end

Nested Structures

Dictionaries can contain other complex types:

order = {
    'id': 123,
    'items': [
        {'product': 'Book', 'price': 15},
        {'product': 'Pen', 'price': 3}
    ],
    'customer': {
        'name': 'Jane',
        'email': 'jane@example.com'
    }
}

# Access nested values
order['items'][0]['product']    # Returns 'Book'
order['customer']['name']       # Returns 'Jane'

# Calculate total
prices = MAP(order['items'], item => item['price'])
SUM(prices)                     # Returns 18