Skip to content

Commit d41432b

Browse files
committed
m: add termwise comparison support
1 parent 52102c8 commit d41432b

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

unpythonic/mathseq.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
neg as primitive_neg, pos as primitive_pos,
3636
and_ as primitive_and, xor as primitive_xor, or_ as primitive_or,
3737
lshift as primitive_lshift, rshift as primitive_rshift,
38-
invert as primitive_invert)
38+
invert as primitive_invert,
39+
lt as primitive_lt, le as primitive_le,
40+
eq as primitive_eq, ne as primitive_ne,
41+
ge as primitive_ge, gt as primitive_gt)
3942

4043
from .it import take, rev, window
4144
from .gmemo import imemoize, gmemoize
@@ -563,7 +566,28 @@ def __or__(self, other):
563566
return sor(self, other)
564567
def __ror__(self, other):
565568
return sor(other, self)
566-
# TODO: conversion (bool, complex, int, float) and comparison operators? Do we want those?
569+
# Can't do this because each of these conversion operators must return an
570+
# instance of that primitive type.
571+
# def __bool__(self):
572+
# return sbool(self)
573+
# def __complex__(self):
574+
# return scomplex(self)
575+
# def __int__(self):
576+
# return sint(self)
577+
# def __float__(self):
578+
# return sfloat(self)
579+
def __lt__(self, other):
580+
return slt(self, other)
581+
def __le__(self, other):
582+
return sle(self, other)
583+
def __eq__(self, other):
584+
return seq(self, other)
585+
def __ne__(self, other):
586+
return sne(self, other)
587+
def __ge__(self, other):
588+
return sge(self, other)
589+
def __gt__(self, other):
590+
return sgt(self, other)
567591

568592
def mg(gfunc):
569593
"""Decorator: make gfunc m() the returned generator instances.
@@ -688,6 +712,30 @@ def sround(a, *ndigits):
688712
https://docs.python.org/3/library/operator.html#operator.not_
689713
"""
690714

715+
# Can't do this because each of these conversion operators must return an
716+
# instance of that primitive type.
717+
#
718+
# sbool = _make_termwise_stream_unop(bool)
719+
# sbool.__doc__ = """Termwise bool(a) for an iterable."""
720+
# scomplex = _make_termwise_stream_unop(complex)
721+
# scomplex.__doc__ = """Termwise complex(a) for an iterable."""
722+
# sint = _make_termwise_stream_unop(int)
723+
# sint.__doc__ = """Termwise int(a) for an iterable."""
724+
# sfloat = _make_termwise_stream_unop(float)
725+
# sfloat.__doc__ = """Termwise float(a) for an iterable."""
726+
727+
slt = _make_termwise_stream_binop(primitive_lt)
728+
slt.__doc__ = """Termwise a < b when one or both are iterables."""
729+
sle = _make_termwise_stream_binop(primitive_le)
730+
sle.__doc__ = """Termwise a <= b when one or both are iterables."""
731+
seq = _make_termwise_stream_binop(primitive_eq)
732+
seq.__doc__ = """Termwise a == b when one or both are iterables."""
733+
sne = _make_termwise_stream_binop(primitive_ne)
734+
sne.__doc__ = """Termwise a != b when one or both are iterables."""
735+
sge = _make_termwise_stream_binop(primitive_ge)
736+
sge.__doc__ = """Termwise a >= b when one or both are iterables."""
737+
sgt = _make_termwise_stream_binop(primitive_gt)
738+
sgt.__doc__ = """Termwise a > b when one or both are iterables."""
691739

692740
# -----------------------------------------------------------------------------
693741

0 commit comments

Comments
 (0)