Skip to content

Pydian partials: have nicer ways to work with lists #12

@ericpan64

Description

@ericpan64

Problem

Working on a case where the result of a get is a list[tuple] where I want to do something to each tuple in the list while keeping the list structure, so the apply= parameter is kind of useless because it forces me to apply to the list first (which makes sense)

Can get around this

Requested feature

Maybe a foreach function? E.g.

from typing import Any
from pydian import get
import pydian.partials as p

def swap_tup_order(t: tuple[Any, Any]) -> tuple:
    return (t[1], t[0])

data = { 'list_tups': [(1, 2), (3, 4), (5, 6)] }
print(get(data, 'list_tups[::-1]', apply=p.foreach(swap_tup_order))) # Expect: `[(6, 5), (4, 3), (2, 1)]`

Alternatives considered

  • Maybe just have people use list comprehensions (more "pythonic"), though could get hard for nested cases

Additional context

  • From working with OpenAI Whisper model -- audio chunks are passed as tuple of timestamps and corresponding text

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