Skip to content

Offering: Add array support to format function #47

@jdhedden

Description

@jdhedden

The current format function only support single-valued inputs. The below permits it to take arrays as inputs:

For ft/ft/functions.py:

@register("format")
@typed(None, T_STRING)
def format(format_str, inp):
    try:
        if type(inp) == list:
            args = list(map(lambda v: v.value, inp))
            return format_str.value.format(*args)
        else:
            return format_str.value.format(inp)
    except ValueError:
        panic("Incorrect format string '{}' for input '{}'.".format(format_str.value, inp))
    except IndexError:
        if type(inp) == list:
            count = len(inp)
        else:
            count = 1
        panic("Not enough arguments (only {} given) for format string '{}'.".format(count, format_str.value))

Example:

> echo -e '1\tone' | map format "'{}' is spelled '{}'"
'1' is spelled 'one'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions