packages feed

kempe-0.2.0.7: 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(%))
            if( dip(dip(dup) 0 !=) swap if(+, nip)
              , drop
              )
          ]


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
safeDiv : Int Int -- (Maybe Int)
        =: [ dup isZeroInt
             if( drop2 Nothing
               , / Just
               )
           ]

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