Import .ipynb files as modules in the system path.
Project description
composites
compose complex functions
composites
are untyped functional programming objects in Python with all the side effects. composites
make it easier to compose/pipeline/chain callables, classes, and other objects into higher-order functions.
pip install git+https://github.com/tonyfast/composites
compose functions with a
, an
, the
, or λ
from composites import *; assert a is an is the
A basic example, enumerate a range and create a __dict__ionary.
f = the[range][reversed][enumerate][dict]
f(3), f
({0: 2, 1: 1, 2: 0}, <composites.Function at 0x10e0eba68>)
Each [bracket]
may accept a callable or iterable. In either case,
a callable is appended to the composition. Compositions are immutable and may have
arbitrary complexity.
g = f.copy() # copy f from above so it remains unchanged.
g[type, len]
g[{'foo': a.do(print).len(), 'bar': the.identity()}]
<composites.Function at 0x10e0eba68>
Brackets juxtapose iterable objects.
the[range, type], the[[range, type]], the[{range, type}], the[{'x': range, 'y': type}]
(<composites.Function at 0x10e0eed68>,
<composites.Function at 0x10e12d048>,
<composites.Function at 0x10e12d108>,
<composites.Function at 0x10e12d1c8>)
Each each composition is immutable.
assert f[len] is f; f
<composites.Function at 0x10e0eba68>
compose functions with attributes
Each composition has an extensible attribution system. Attributes can be accessed in a shallow or verbose way.
a.range() == a.builtins.range() == a[range]
False
compose functions with symbols
assert a / range == a.map(range)
assert a // range == a.filter(range)
assert a @ range == a.groupby(range)
assert a % range == a.reduce(range)
combine item getters, attributes, symbols, and other compositions to express complex ideas.
f = a['test', 5, {42}] \
/ (a**str&[str.upper, str.capitalize]|a**int&a.range().map(
a.range(2).len()
).list()|a**object&type) \
* list
f()
use compositions recursively
f = a[:]
f[a**a.gt(5)*range | a**a.le(5)*a.add(1)[f]](4)
False
Why functional programming with composites
?
Functional programming often generates less code, or text, to express operations on complex data structures. A declarative, functional style of programming approach belies Python's imperative, object-oriented (OO) nature. Python provides key functional programming elements that are used interchangeably with OO code.
toolz
, the nucleus for composites
, extends Python's functional programming with a set of
un-typed, lazy, pure, and composable functions. The functions in toolz
look familiar
to pandas.DataFrame methods, or underscorejs and d3js in Javascript.
An intermediate user of toolz
will use
toolz.pipe
,
toolz.juxt
,
and toolz.compose
to create reusable,
higher-order functions. These patterns allow the programmer to express complex concepts
with less typing/text over a longer time. Repetitive patterns should occupy
less screen space; composites;
helps compose functions with less text.
A successful implementation of composites should compose un-typed, lazy, and serializable Python functions that allow recursion.
Syntax
A core property of composites
is that it will not modify Python's abstract syntax tree, rather it expresses
a large portion of Python's magic methods in the data model. It considers Python's
order of operations in the api design. composites
provides symbolic expressions for common higher-order
function operations like map
, filter
, groupby
, and reduce
. The attributes can access any of the sys.modules;
with tab completion.
The efficiency of computing will continue to improve. In modern collaborative development environments we must consider the efficiency of the programmer. Programming is a repetitive process requiring physical work from a person. composites speed up the creation and reading repetitive and complex tasks.
composites
structure
Development
if __name__== '__main__':
!jupyter nbconvert --to markdown --TemplateExporter.exclude_input=True readme.ipynb
!jupyter nbconvert --to markdown --execute composites.ipynb
!python -m doctest composites.py
!echo complete
[NbConvertApp] Converting notebook readme.ipynb to markdown
[NbConvertApp] Writing 5152 bytes to readme.md
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.