Skip to content

Commit

Permalink
Add function TokenBundle.equipartitionAssets.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Mar 2, 2021
1 parent ead92ac commit 27d043c
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/core/src/Cardano/Wallet/Primitive/Types/TokenBundle.hs
Expand Up @@ -53,6 +53,7 @@ module Cardano.Wallet.Primitive.Types.TokenBundle
, removeQuantity

-- * Partitioning
, equipartitionAssets
, equipartitionQuantitiesWithUpperBound

-- * Policies
Expand Down Expand Up @@ -348,6 +349,29 @@ removeQuantity b a = b { tokens = TokenMap.removeQuantity (tokens b) a }
-- Partitioning
--------------------------------------------------------------------------------

-- | Partitions a token bundle into 'n' smaller bundles, where the asset sets
-- of the resultant bundles are disjoint.
--
-- In the resultant bundles, the smallest asset set size and largest asset set
-- size will differ by no more than 1.
--
-- The ada 'Coin' quantity is equipartitioned across the resulting bundles.
--
-- The quantities of each non-ada asset are unchanged.
--
equipartitionAssets
:: TokenBundle
-- ^ The token bundle to be partitioned.
-> NonEmpty a
-- ^ Represents the number of portions in which to partition the bundle.
-> NonEmpty TokenBundle
-- ^ The partitioned bundles.
equipartitionAssets (TokenBundle c m) count =
NE.zipWith TokenBundle cs ms
where
cs = Coin.equipartition c count
ms = TokenMap.equipartitionAssets m count

-- | Partitions a token bundle into 'n' smaller bundles, where the quantity of
-- each token is equipartitioned across the resultant bundles, with the goal
-- that no token quantity in any of the resultant bundles exceeds the given
Expand Down

0 comments on commit 27d043c

Please sign in to comment.