对抽象过滤器进行操作(列表理解):组合两个过滤器

发布于 2024-12-21 16:17:45 字数 1463 浏览 1 评论 0原文

短而尖锐:
给定两个布尔语句,在像 Lua 这样的语言中计算它们的交集方程的最简单方法是什么?

维恩图
(红色 = 过滤器 1,蓝色 = 过滤器 2,紫色 = 交叉区域)

又长又感叹:

  • 过滤器 Aobject.ID < 300

  • 过滤器 Bobject.ID < 600

过滤器 A过滤器 B子集,即:过滤器 B 将包含所有内容与过滤器 A 匹配,加上 0 个或多个对象。 在维恩图上,过滤器 A 将位于过滤器 B 内部。

如何计算交叉面积方程?

更复杂的示例:

  • 过滤器 Xobject.Col == 'GREEN' and (object.ID == 2 or object.ID == 64 or object.ID > 9001)
  • 过滤 Y(object.Col == 'RED' 或 object.Col == 'GREEN') and (object.ID == 3 或 object.ID > 22)

过滤器 A过滤器 B 相交。 在维恩图上,它们会重叠。 重叠面积的方程为:
object.Col == 'GREEN' and (object.ID == 64 or object.ID > 9001)

这个方程如何用 Python 或 Haskell 等语言计算?

我希望最终用 Lua 实现这一点,但如果 Python、Haskell 或其他语言提供了该功能,我将能够查看源代码并将其转换过来。

以下是我在 Lua 中表示过滤器的方式:

filter = DataFilter(
    {"and",
        {"or",
            {"==", "Col", "RED"},
            {"==", "Col", "GREEN"},
        },
        {"or",
            {"==", "ID", 3},
            {">" , "ID", 22},
        },
    }
)

请为我指出正确的方向。

Short and sharp:
Given two Boolean statements, what is the easiest way to calculate the equation of their intersection in a language like Lua?

Venn Diagram
(Red = Filter 1, Blue = Filter 2, Purple = Area of intersection)

Long and lamenting:

  • Filter A: object.ID < 300

  • Filter B: object.ID < 600

Filter A is a subset of Filter B, that is: Filter B will contain everything matched by Filter A, plus 0 or more objects.
On a Venn diagram, Filter A would be inside Filter B.

How can I calculate the equation of the area of intersection?

A more complicated example:

  • Filter X: object.Col == 'GREEN' and (object.ID == 2 or object.ID == 64 or object.ID > 9001)
  • Filter Y: (object.Col == 'RED' or object.Col == 'GREEN') and (object.ID == 3 or object.ID > 22)

Filter A intersects with Filter B.
On a Venn Diagram, they would overlap.
The equation for the overlapping area would be:
object.Col == 'GREEN' and (object.ID == 64 or object.ID > 9001)

How would this equation be calculated in a language such as Python or Haskell?

I wish to eventually make this in Lua, but if Python, Haskell or another language provided the functionality, I would be able to look at the source code and convert it over.

Here is how I am representing filters in Lua:

filter = DataFilter(
    {"and",
        {"or",
            {"==", "Col", "RED"},
            {"==", "Col", "GREEN"},
        },
        {"or",
            {"==", "ID", 3},
            {">" , "ID", 22},
        },
    }
)

Please point me in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃扇骨 2024-12-28 16:17:45

疯狂猜测:将“过滤器”引入析取范式并使用适当的方法进行归约(x == 8 包含在 x > 5 中)。

Wild guess: Bring the "Filters" into disjunctive normal form and reduce using appropriate methods (x == 8 contained in x > 5).

单调的奢华 2024-12-28 16:17:45

这是你可以通过某种方式实现的。自注释代码将帮助您理解该方法

#Create a Class Foo with attributes id and col
class Foo:
    def __init__(this,ID,COL):
        this.id=ID
        this.col=COL



#Dataset
data=["VIOLET","INDIGO","BLUE","GREEN","YELLOW","ORANGE","RED"]
ObjList=[Foo(random.randint(1,70),random.choice(data)) for i in xrange(1,10000)]

#Create the Filter Functions
def FilterX(obj):
    return obj.col == 'GREEN'  and (obj.id == 2 or obj.id == 64 or obj.id > 9001)

def FilterY(obj):
    return (obj.col == 'RED' or obj.col == 'GREEN') and (obj.id == 3  or obj.id > 22)

def FilterZ(obj):
    return obj.col == 'GREEN'  and (obj.id > 50)

#Create a list of filter functions
filters=[FilterX,FilterY,FilterZ]

#Create a set result (that will hold the intersected data) and assign the result of
#applying the First Filter on ObjList
result=set(filter(filters[0],ObjList))

#For the Rest of the filter's apply them on the ObjList, and then intersect
#the resultant set with the result
for s in (set(filter(foo,ObjList)) for foo in filters[1:]):
    result=result.intersection(s)

#Finally Display the result
[(obj.id,obj.col) for obj in result]

This is somehow you can achieve this. The self commented code would help you understand the approach

#Create a Class Foo with attributes id and col
class Foo:
    def __init__(this,ID,COL):
        this.id=ID
        this.col=COL



#Dataset
data=["VIOLET","INDIGO","BLUE","GREEN","YELLOW","ORANGE","RED"]
ObjList=[Foo(random.randint(1,70),random.choice(data)) for i in xrange(1,10000)]

#Create the Filter Functions
def FilterX(obj):
    return obj.col == 'GREEN'  and (obj.id == 2 or obj.id == 64 or obj.id > 9001)

def FilterY(obj):
    return (obj.col == 'RED' or obj.col == 'GREEN') and (obj.id == 3  or obj.id > 22)

def FilterZ(obj):
    return obj.col == 'GREEN'  and (obj.id > 50)

#Create a list of filter functions
filters=[FilterX,FilterY,FilterZ]

#Create a set result (that will hold the intersected data) and assign the result of
#applying the First Filter on ObjList
result=set(filter(filters[0],ObjList))

#For the Rest of the filter's apply them on the ObjList, and then intersect
#the resultant set with the result
for s in (set(filter(foo,ObjList)) for foo in filters[1:]):
    result=result.intersection(s)

#Finally Display the result
[(obj.id,obj.col) for obj in result]
北方的韩爷 2024-12-28 16:17:45

我不知道我是否遗漏了重要的一点。看来您的过滤器只是根据“对象”的质量返回一个布尔值。为什么不直接使用常规的“and”和“or”以及函数来组成它们呢?

这就是我在 Lua 中制作过滤器的方式:

function filterX(object)
  return object.Col == 'GREEN' and 
    (object.ID == 2 or object.ID == 64 or object.ID > 9001)
end

function filterY(object)
   return (object.Col == 'RED' or object.Col == 'GREEN') and 
     (object.ID == 3 or object.ID > 22)
end

您可以使用这些额外函数定义这些过滤器的“并集”或“交集”:

function union(f,g)
  return function(...)
    return f(...) or g(...)
  end
end

function intersection(f,g)
  return function(...)
    return f(...) and g(...)
  end
end

这是您的组合方式:

union(filterX, filterY)(object) -- returns true or false
intersection(filterX, filterY)(object) -- true or false

或者,如果您想经常重用它们:

filterXorY = union(filterX, filterY)
filterXandY = intersection(filterX, filterY)

filterXorY(object) -- true or false
filterXandY(object) -- true or false

我希望这样有帮助。

I don't know if I'm missing an important point here. It seems that your filters just return a boolean depending on the qualities of "object". Why don't you just use regular "and" and "or"s and functions to compose them?

This is how I'd make your filters in Lua:

function filterX(object)
  return object.Col == 'GREEN' and 
    (object.ID == 2 or object.ID == 64 or object.ID > 9001)
end

function filterY(object)
   return (object.Col == 'RED' or object.Col == 'GREEN') and 
     (object.ID == 3 or object.ID > 22)
end

You can define the "union" or "intersection" of those filters with these extra functions:

function union(f,g)
  return function(...)
    return f(...) or g(...)
  end
end

function intersection(f,g)
  return function(...)
    return f(...) and g(...)
  end
end

And here's how you compose:

union(filterX, filterY)(object) -- returns true or false
intersection(filterX, filterY)(object) -- true or false

Or, if you want to reuse them often:

filterXorY = union(filterX, filterY)
filterXandY = intersection(filterX, filterY)

filterXorY(object) -- true or false
filterXandY(object) -- true or false

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文