packages feed

kempe-0.2.0.8: prelude/arith.kmp

import "prelude/fn.kmp"
import "lib/maybe.kmp"

; like haskell; % is rem and mod is... mod
;
; from here: https://hackage.haskell.org/package/ghc-prim-0.7.0/docs/src/GHC-Classes.html#modInt%23
modInt : Int Int -- Int
       =: [ dup2 dup2
            0 < dip(0 >) &
            dip(0 > dip(0 <) &)
            ||
            dip(dup dip(%))
            ; FIXME: hits the second branch when 2, -3
            if( dip(dip(dup) 0 !=) swap if(+, nip)
              , drop
              )
          ]

divInt : Int Int -- Int
       =: [ dup2
            0 < dip(0 >) &
            if( dip(1 -) / 1 -
              , dup2
                0 < dip(0 >) &
                if( dip(1 +) / 1 -
                  ; FIXME: hits second branch when 2, -3
                  , /
                  )
              )
          ]

succInt : Int -- Int
        =: [ 1 + ]

predInt : Int -- Int
        =: [ 1 - ]

isZeroInt : Int -- Bool
          =: [ 0 = ]

absInt : Int -- Int
       =: [ dup 0 <
            if (~ ,)
          ]

; More from Mirth
maxInt : Int Int -- Int
       =: [ dup2 < if(nip, drop) ]

minInt : Int Int -- Int
       =: [ dup2 < if(drop, nip) ]

; checks for division by zero
safeQuot : Int Int -- (Maybe Int)
         =: [ dup isZeroInt
              if( drop2 Nothing
                , / Just
                )
            ]

safeRem : Int Int -- (Maybe Int)
        =: [ dup isZeroInt
             if( drop2 Nothing
               , % Just
               )
           ]