pipetools
pipetools copied to clipboard
Computation Using Two X Objects, is it possible?
I was wondering if computation using two X objects is possible, such as in the following situation:
import itertools
from pipetools import *
# given 4 sets: a, b, c, d, find their intersections
abcd = {
'a': set([1,2,2,8,3]),
'b': set([1,3,5,7]),
'c': set([1,1,2,3,5,8]),
'd': set([4,5]) }
- combination is okay, foreach generates 3-tuple nicely
abcd > X.items() | (itertools.combinations, X, 2) | foreach(('{0[0]}&{1[0]}', X[0][1], X[1][1])) | foreach_do(print)
# ('a&b', {8, 1, 2, 3}, {1, 3, 5, 7})
# ('a&c', {8, 1, 2, 3}, {1, 2, 3, 5, 8})
# ('a&d', {8, 1, 2, 3}, {4, 5})
# ('b&c', {1, 3, 5, 7}, {1, 2, 3, 5, 8})
# ('b&d', {1, 3, 5, 7}, {4, 5})
# ('c&d', {1, 2, 3, 5, 8}, {4, 5})
- intersection with lambda is okay
abcd > X.items() | (itertools.combinations, X, 2) | foreach(('{0[0]}&{1[0]}', (lambda t: t[0][1] &t[1][1]))) | foreach_do(print)
# ('a&b', {1, 3})
# ('a&c', {8, 1, 2, 3})
# ('a&d', set())
# ('b&c', {1, 3, 5})
# ('b&d', {5})
# ('c&d', {5})
- intersection with X does not, is this the limitation mentioned in the document?
abcd > X.items() | (itertools.combinations, X, 2) | foreach(('{0[0]}&{1[0]}', X[0][1] & X[1][1])) | foreach_do(print)
# ('a&b', X[1] | X[1] | {8, 1, 2, 3} & X)
# ('a&c', X[1] | X[1] | {8, 1, 2, 3} & X)
# ('a&d', X[1] | X[1] | {8, 1, 2, 3} & X)
# ('b&c', X[1] | X[1] | {1, 3, 5, 7} & X)
# ('b&d', X[1] | X[1] | {1, 3, 5, 7} & X)
# ('c&d', X[1] | X[1] | {1, 2, 3, 5, 8} & X)
I also tried something simpler as follows. It seems that X cannot be used twice, is that correct?
pipe_square = pipe | X ** 2
pipe_XbyX = pipe | X * X
pipe_square < 7 # 49
pipe_XbyX < 7 # 7 * X
That's correct, you can't have multiple X
s in a single expression. It would probably be possible to add this functionality, but it hasn't been implemented. You can take a stab at it if you wish, or stick to lambda
in this case 🙂