Skip to content

Instantly share code, notes, and snippets.

@fumieval
Created January 29, 2012 13:35
Show Gist options
  • Save fumieval/1698856 to your computer and use it in GitHub Desktop.
Save fumieval/1698856 to your computer and use it in GitHub Desktop.
主語を透過的に増やして量化する
class Quantify(object):
"""
メソッドを量化する。
量化子はクラス属性のqで指定する。
"""
def __init__(self, iterable):
self.iterable = iterable
def __lt__(self, other):
return self.__class__.q(el < other for el in self.iterable)
def __gt__(self, other):
return self.__class__.q(el > other for el in self.iterable)
def __eq__(self, other):
return self.__class__.q(el == other for el in self.iterable)
def __ne__(self, other):
return self.__class__.q(el != other for el in self.iterable)
def __le__(self, other):
return self.__class__.q(el <= other for el in self.iterable)
def __ge__(self, other):
return self.__class__.q(el >= other for el in self.iterable)
def __getattr__(self, name):
def _(*args, **kwargs):
return self.__class__.q(getattr(el, name)(*args, **kwargs) for el in self.iterable)
return _
class Every(Quantify):
"""
全称量化を行う。
メソッドをP、引数をxとしたとき「すべてのiterableの要素iに対してi.P(x)が成り立つ」という式を表現する。
>>> Every([1,2,3]) < 5
True
>>> Every("abc0xyz").isalpha()
False
"""
q = all
class Some(Quantify):
"""
存在量化を行う。
メソッドをP、引数をxとしたとき、「少なくとも一つのiterableの要素iに対してi.P(x)が成り立つ」という式を表現する。
>>> Some([1,2,3]) > 3
False
>>> Some("spam0").isdigit()
True
"""
q = any
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment