suned / pfun

Functional, composable, asynchronous, type-safe Python.
https://pfun.dev/
MIT License
151 stars 14 forks source link

Suggest PEP for mappable and chainable protocols and do expressions #32

Open suned opened 4 years ago

suned commented 4 years ago

A mappable type is something that implemets __map__(self, f). A chainable type is something that implements __chain__(self, f).

A type T that implements both can be used in do expressions as

fullname = do user_id('sad') as uid, first_name(uid) as first, last_name(uid) as last in f'{first} {last}'

Which will translate to

fullname = user_id('sad').__chain__(lambda uid: first_name(uid).__chain__(lambda first: last_name(uid).__map__(lambda last: f'{first} {last}')))