Skip to content
This repository was archived by the owner on Feb 24, 2021. It is now read-only.
This repository was archived by the owner on Feb 24, 2021. It is now read-only.

Candidate implementation of ConsList matching utility #8

Open
@mwchase

Description

@mwchase

I don't know if I want this, but I sketched out some classes to implement ConsList, which would be a cons-like interface to tuple objects, for matching purposes.

Convert this to a branch/pr if I decide I want it.

import dataclasses
import typing


class ConsList:
    pass


class Nil(ConsList):
    pass


NIL = Nil()


@dataclasses.dataclass(frozen=True)
class ConsCell(ConsList):

    head: object
    next_items: typing.Tuple
    end: object

    @property
    def tail(self):
        if self.next_items:
            return ConsCell(self.next_items[0], self.next_items[1:], self.end)
        return self.end


def cons_list(*args, end=NIL):
    arglist = list(args)
    while isinstance(end, ConsCell):
        arglist.append(end.head)
        end = end.tail
    if arglist:
        return ConsCell(arglist[0], tuple(arglist[1:]), end)
    return end

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions