U UniCore Community Public content rendered for search, speed, and sharing

Advanced Usage

Method Chaining

Chainable patterns for concise data transformations.

Get familiar and learn how to code faster with UniCoreFW.

from unicorefw import _

# Chaining example
result = (_([1, 2, 3, 4, 5])
            .map(lambda x: x * 2)
            .filter(lambda x: x > 5)
            .value())

print(f"Chained result: {result}")  # Output: [6, 8, 10]

# Static method example
template = "Name: <%= name %>, Age: <%= age %>"
context = {"name": "Alice", "age": 25}
print(f"Template result: {_.template(template, context)}")
# Output: "Name: Alice, Age: 25"

# Multiple operations example
data = [
    {"name": "Alice", "age": 25, "active": True},
    {"name": "Bob", "age": 30, "active": False},
    {"name": "Charlie", "age": 35, "active": True}
]

active_names = (_(data)
                .filter(lambda x: x.get("active", False))
                .pluck("name")
                .value())
print(f"Active users: {active_names}")  # Output: ['Alice', 'Charlie']

# Demonstrate static vs chained methods
array = [1, 2, 3, 4, 5]
print(f"Static map: {_.map(array, lambda x: x * 3)}")
print(f"Chained map: {_(array).map(lambda x: x * 3).value()}")