From 22aaf0c05ab2ab2b271ba46ee925cfcf49e62ae9 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 15 Mar 2014 20:50:35 +0100 Subject: fix compose specs and implement it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- examples/lisp-esque | 61 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 13 deletions(-) (limited to 'examples/lisp-esque') diff --git a/examples/lisp-esque b/examples/lisp-esque index 079d018..9c093a5 100644 --- a/examples/lisp-esque +++ b/examples/lisp-esque @@ -138,6 +138,9 @@ if isinstance(conf[0], str) and not conf[0].startswith(':'): # Available outputs screens = list_screens('drm' if ttymode else 'randr') +# Map of composed function +composed = {} + ## For the following functions, the type of args is the type of args ## after it has been evaluated, they may be functions inside that @@ -385,6 +388,50 @@ def _transfrom(mods, args): print('Selected transition from method: %s' % ', '.join(args)) +def _compose(mods, args): + ''' + Compose a function + + @param mods:[] Not used + @param args:list> The name of the function follow by the wrapped function and + parameters wrappers: 'as-is' for identity, 'yes' for tautology, + 'no' for contradiction, and functions names for functions, or + a composition + ''' + new_function = args[0] + old_function = composed[args[1]] if args[1] in composed else eval(args[1]) + arguments = [[arg] if isinstance(arg, str) else arg for arg in args[2:]] + + wrapping = [] + for arg in arguments: + composite = lambda x : x() + for f_ in arg: + if f_ == 'as-is': + continue + elif f_ == 'yes': + composite = lambda x : True + elif f_ == 'no': + composite = lambda x : False + else: + composite_ = composite + f = composed[f_] if f_ in composed else eval(f_) + composite = lambda x : f(composite_(x)) + wrapping.append(composite) + + def F_new(*args): + arg_ptr = -1 + def arg_itr(): + nonlocal arg_ptr + arg_ptr += 1 + return args[arg_ptr] + evaled = [] + for wrap in wrapping: + evaled.append(wrap(arg_itr)) + return old_function(*evaled) + + composed[new_function] = F_new + + def _negative(mods, args): ''' Add negative image adjustment @@ -396,7 +443,7 @@ def _negative(mods, args): monitor (or all of them) on whether to apply negative image, 'yes' implied for all monitors if empty ''' - pass + else def _invert(mods, args): @@ -424,18 +471,6 @@ def _temperature(mods, args): pass -def _compose(mods, args): - ''' - Compose a function - - @param mods:[] Not used - @param args:list> The name of the function follow by parameters wrappers: - 'as-is' for unmodified, 'yes' for tautology, 'no' for contradiction, - and functions names for functions, or a composition - ''' - pass - - def _current(mods, args): ''' Add adjustments applied when Blueshift starts -- cgit v1.2.3-70-g09d2