From aad2da35cfee478c510c8d11bf2722cd762b58ec Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Mon, 12 Sep 2022 16:38:14 -0700 Subject: [PATCH] divmod We don't need floor if we have only ints. Id is too easy as a definition. divmod is cool. --- implementations/Python/joy/library.py | 27 --------------------------- implementations/Python/simplejoy.py | 24 +++++++++++++++++++++++- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/implementations/Python/joy/library.py b/implementations/Python/joy/library.py index e660389..1236783 100644 --- a/implementations/Python/joy/library.py +++ b/implementations/Python/joy/library.py @@ -7,33 +7,6 @@ ALIASES = ( ('id', [u'•']), ) -def floor(n): - return int(math.floor(n)) - -floor.__doc__ = math.floor.__doc__ - - -@inscribe -@SimpleFunctionWrapper -def divmod_(S): - ''' - divmod(x, y) -> (quotient, remainder) - - Return the tuple (x//y, x%y). Invariant: q * y + r == x. - ''' - y, (x, stack) = S - q, r = divmod(x, y) - return r, (q, stack) - - -@inscribe -@SimpleFunctionWrapper -def id_(stack): - '''The identity function.''' - return stack - - - # # § Combinators # diff --git a/implementations/Python/simplejoy.py b/implementations/Python/simplejoy.py index 160be0e..a3d0241 100755 --- a/implementations/Python/simplejoy.py +++ b/implementations/Python/simplejoy.py @@ -1730,6 +1730,28 @@ def pm(stack): return m, (p, stack) +@inscribe +@SimpleFunctionWrapper +def divmod_(S): + ''' + Similarly to pm ("Plus or minus") this function computes + both the + :: + + a b divmod + --------------------- + a b div a b mod + --------------------- + q r + + Where: q * b + r == a + + ''' + y, (x, stack) = S + q, r = divmod(x, y) + return r, (q, stack) + + @inscribe def sharing(stack, expression, dictionary): '''Print redistribution information.''' @@ -2038,7 +2060,7 @@ S_cond = Symbol('cond') def cond(stack, expr, dictionary): ''' This combinator works like a case statement. It expects a single quote - on the stack that must contain zero or more condition quotes and a + on the stack that must contain zero or more condition quotes and a default quote. Each condition clause should contain a quoted predicate followed by the function expression to run if that predicate returns true. If no predicates return true the default function runs.