From c33694ac035fe2915c3aa4d39f430ad7d0de753b Mon Sep 17 00:00:00 2001 From: Jan Pokorný Date: Mon, 27 Jan 2014 23:04:12 +0100 Subject: command: implement "shapes" match for filter/format chain MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan Pokorný --- utils.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'utils.py') diff --git a/utils.py b/utils.py index 785bdad..c457bc1 100644 --- a/utils.py +++ b/utils.py @@ -45,6 +45,23 @@ apply_intercalate = apply_aggregation_preserving_depth( i, []) ) +zipped_outlier = type('zipped_outlier', (tuple, ), {}) +# NOTE: automatically shortens the longer counterpart in the pair +# to the length of the bigger one +apply_loose_zip_preserving_depth = \ + lambda a, b: \ + (type(a) if type(a) == type(b) else type(a))( + [apply_loose_zip_preserving_depth(*p) for p in zip(a, b)] + ) if isinstance(a, (tuple, list)) == isinstance(b, (tuple, list)) \ + == True else zipped_outlier([a, b]) +# as previous, but with length checking of some sort +apply_strict_zip_preserving_depth = \ + lambda a, b: \ + (type(a) if type(a) == type(b) else type(a))( + [apply_strict_zip_preserving_depth(*p) for p in zip(a, b)] + ) if isinstance(a, (tuple, list)) == isinstance(b, (tuple, list)) \ + == True and len(a) == len(b) else zipped_outlier([a, b]) + def which(name, *where): """Mimic `which' UNIX utility""" -- cgit