Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Computation Using Two X Objects, is it possible? #28

Open
tinhb opened this issue Jun 10, 2023 · 1 comment
Open

Computation Using Two X Objects, is it possible? #28

tinhb opened this issue Jun 10, 2023 · 1 comment

Comments

@tinhb
Copy link

tinhb commented Jun 10, 2023

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]) }
  1. 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})
  1. 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})
  1. 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
@0101
Copy link
Owner

0101 commented Jun 11, 2023

That's correct, you can't have multiple Xs 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 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants