예제 #1
0
 def _preprocess(self, bindings):
     return pipe(
         deepcopy,
         partial(lflatten, follow=isa(list, tuple, Keymap)),
         self._apply_common_context,
         self._apply_default_match_all
     )(bindings)
예제 #2
0
    def default(self, obj):
        ordered_attrs = pipe(
            partial(map, lambda attr: (attr, getattr(obj, attr))),
            partial(remove_values, isnone),
            partial(remove_values, all_fn(isa(list, dict), isempty)),
            partial(walk_values, iffy(isa(dict), sort_dict)),
            OrderedDict)

        if isinstance(obj, Context):
            return ordered_attrs(['key', 'operator', 'operand', 'match_all'])
        elif isinstance(obj, Binding):
            return ordered_attrs(['keys', 'command', 'args', 'context'])
        else:
            return super().default(obj)
예제 #3
0
stub_list = lambda *x, **kw: list()
stub_dict = lambda *x, **kw: dict()
blank_class = lambda *x, **kw: x[0].__class__()
from functools import partial
# fn utils
# versions of curry that truncate to correct number of positional args
curryn = lambda n, fn: lambda *a, **kw: fn(*a, **kw) if len(
    a) >= n else curryn(n - len(a), lambda *b, **kwb: fn(*a, *b, **kw, **kwb))
curry2 = lambda fn: curryn(2, lambda *a, **kw: fn(a[0], a[1], **kw))
curry3 = lambda fn: curryn(3, lambda *a, **kw: fn(a[0], a[1], a[2], **kw))
ensureListArg = lambda fn: lambda *args, **kw: fn(args[0], **kw) if (len(
    args) == 1) else fn(args, **kw)

#function manipulation

compose = lambda *fns: pipe(*reversed(fns))
fmap = lambda fn: lambda data: (fn(d) for d in data)
mapPipe = compose(fmap, pipe)
spread = lambda fn: lambda iterableArg: fn(*iterableArg)

nthArg = lambda argNum: lambda *args: args[argNum]
negate = lambda fn: lambda *args, **kwargs: not fn(*args, **kwargs)
over = lambda fns: lambda *args, **kwargs: tuple(
    f(*args, **kwargs) for f in fns)
forkjoin = lambda *fns: lambda data: fns[-1](*[fn(data) for fn in fns[0:-2]])

get_kv_iter = lambda x: x.items() if hasattr(x, 'items') else enumerate(x)


@curry3
def reduce_to(get_result, fn, coll):