Skip to content

Commit

Permalink
Add doc comments for equipartition functions
Browse files Browse the repository at this point in the history
  • Loading branch information
errfrom committed Sep 23, 2022
1 parent 25a24c6 commit 7b961f2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Cardano/Types/Value.purs
Expand Up @@ -8,7 +8,6 @@ module Cardano.Types.Value
, coinToValue
, currencyMPSHash
, eq
, equipartitionAssetsWithTokenQuantityUpperBound
, equipartitionValueWithTokenQuantityUpperBound
, filterNonAda
, flattenNonAdaValue
Expand Down Expand Up @@ -297,6 +296,12 @@ instance Equipartition NonAdaAsset where
map (mkSingletonNonAdaAsset cs tn)
(equipartition tokenQuantity numParts)

-- | Partitions a `NonAdaAsset` into smaller `NonAdaAsset`s, where the
-- | quantity of each token is equipartitioned across the resultant
-- | `NonAdaAsset`s, with the goal that no token quantity in any of the
-- | resultant `NonAdaAsset`s exceeds the given upper bound.
-- | Taken from cardano-wallet:
-- | https://github.com/input-output-hk/cardano-wallet/blob/d4b30de073f2b5eddb25bf12c2453abb42e8b352/lib/wallet/src/Cardano/Wallet/Primitive/Types/TokenMap.hs#L780
equipartitionAssetsWithTokenQuantityUpperBound
:: NonAdaAsset -> BigInt -> NonEmptyArray NonAdaAsset /\ Int
equipartitionAssetsWithTokenQuantityUpperBound nonAdaAssets maxTokenQuantity =
Expand Down Expand Up @@ -433,6 +438,12 @@ instance Equipartition Value where
(equipartition coin numParts)
(equipartition nonAdaAssets numParts)

-- | Partitions a `Value` into smaller `Value`s, where the Ada amount and the
-- | quantity of each token is equipartitioned across the resultant `Value`s,
-- | with the goal that no token quantity in any of the resultant `Value`s
-- | exceeds the given upper bound.
-- | Taken from cardano-wallet:
-- | https://github.com/input-output-hk/cardano-wallet/blob/d4b30de073f2b5eddb25bf12c2453abb42e8b352/lib/wallet/src/Cardano/Wallet/Primitive/Types/TokenBundle.hs#L381
equipartitionValueWithTokenQuantityUpperBound
:: Value -> BigInt -> NonEmptyArray Value
equipartitionValueWithTokenQuantityUpperBound value maxTokenQuantity =
Expand Down
8 changes: 8 additions & 0 deletions src/Equipartition.purs
Expand Up @@ -17,9 +17,15 @@ import Data.BigInt (BigInt)
import Data.BigInt (fromInt) as BigInt
import Data.Maybe (Maybe(Just, Nothing))

-- | Represents types whose values can be equally divided into several parts.
class Equipartition a where
equipartition :: a -> Int -> NonEmptyArray a

-- | Computes the equipartition of a `BigInt` into `numParts` smaller `BigInt`s
-- | whose values differ by no more than 1. The resultant array is sorted in
-- | ascending order.
-- | Taken from cardano-wallet:
-- | https://github.com/input-output-hk/cardano-wallet/blob/d4b30de073f2b5eddb25bf12c2453abb42e8b352/lib/numeric/src/Cardano/Numeric/Util.hs#L127
instance Equipartition BigInt where
equipartition bi numParts
| numParts <= one =
Expand All @@ -28,6 +34,8 @@ instance Equipartition BigInt where
mapLastN (bi `mod` BigInt.fromInt numParts) (add one) $
NEArray.replicate numParts (bi / BigInt.fromInt numParts)
where
-- | Applies the passed function to the last `n` elements of the
-- | `NonEmptyArray`.
mapLastN
:: forall (a :: Type) (n :: Type)
. Ring n
Expand Down

0 comments on commit 7b961f2

Please sign in to comment.