Quick actions

cmd+k|ctrl+k

Navigation

Languages

Lazy lists

Snippet info

Language

Python

Visibility

public

Author

rightfold

Created

2017-01-20T09:03:48Z

Updated

2017-02-27T10:40:16Z

class Lazy:
    __slots__ = ('thunk', 'result')

    def __init__(self, thunk):
        self.thunk = thunk

    def force(self):
        if self.thunk is not None:
            self.result = self.thunk()
            self.thunk = None
        return self.result

def eager(lazy_list):
    result = []
    while True:
        cell = lazy_list.force()
        if cell is None:
            return result
        result.append(cell[0])
        lazy_list = cell[1]

lazy_list = Lazy(lambda: (1, Lazy(lambda: (2, Lazy(lambda: (3, Lazy(lambda: None)))))))
print(eager(lazy_list))
print(eager(lazy_list))
print(eager(lazy_list))
INFO