Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test(arrow):
if not isinstance(arrow, FunctionArrow):
raise ValueError("Must be a FunctionArrow")
# (a &&& arr id) >>> arr (\(b, x) = if b then Left x else Right x)
return (arrow & FunctionArrow(lambda x: x)) >> FunctionArrow(lambda t: Left(t[1]) if t[0] else Right(t[1]))
def if_maker(predicate_func, then_func, else_func):
return test(FunctionArrow(predicate_func)) >> (FunctionArrowChoice(then_func) | FunctionArrowChoice(else_func))
def test(arrow):
if not isinstance(arrow, FunctionArrow):
raise ValueError("Must be a FunctionArrow")
# (a &&& arr id) >>> arr (\(b, x) = if b then Left x else Right x)
return (arrow & FunctionArrow(lambda x: x)) >> FunctionArrow(lambda t: Left(t[1]) if t[0] else Right(t[1]))
# (at your option) any later version.
#
# Pypeline is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pypeline. If not, see .
#
from pypeline.core.arrows.arrow import Arrow, ArrowChoice
from pypeline.core.arrows.function_arrow import FunctionArrow
from pypeline.core.types.either import *
class FunctionArrowChoice(ArrowChoice, FunctionArrow):
def __init__(self, f):
ArrowChoice.__init__(self)
FunctionArrow.__init__(self, f)
#
# left :: a b c -> a (Either b d) (Either c d)
#
# +-------------------+
# | /--- f ---\ |
# Either b d --->+---( )---+---> Either c d
# | \---------/ |
# +-------------------+
def left(self):
def left_func(either):
if isinstance(either, Left):
return Left(self._func(either.val))
def __or__(self, other):
if not isinstance(other, FunctionArrowChoice):
raise ValueError("Must be a FunctionArrowChoice")
def merge(either):
if not isinstance(either, Either):
raise ValueError("Must be an Either")
return either.val
return (self + other) >> FunctionArrow(merge)
def __init__(self, f):
ArrowChoice.__init__(self)
FunctionArrow.__init__(self, f)