Skip to content

Commit

Permalink
Move equipartitionCoin to Coin module.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Mar 2, 2021
1 parent 6b77d29 commit c113cb5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
Expand Up @@ -1199,22 +1199,6 @@ makeChangeForCoin targets excess =
--
--------------------------------------------------------------------------------

-- | Computes the equipartition of a coin into 'n' smaller coins.
--
equipartitionCoin
:: HasCallStack
=> Coin
-- ^ The coin to be partitioned.
-> NonEmpty a
-- ^ Represents the number of portions in which to partition the coin.
-> NonEmpty Coin
-- ^ The partitioned coins.
equipartitionCoin c =
-- Note: the natural-to-coin conversion is safe, as equipartitioning always
-- guarantees to produce values that are less than or equal to the original
-- value.
fmap unsafeNaturalToCoin . equipartitionNatural (coinToNatural c)

-- | Computes the equipartition of a token map into 'n' smaller maps.
--
-- Each asset is partitioned independently.
Expand Down Expand Up @@ -1271,7 +1255,7 @@ equipartitionTokenBundleWithMaxQuantity
equipartitionTokenBundleWithMaxQuantity b maxQuantity =
NE.zipWith TokenBundle cs ms
where
cs = equipartitionCoin (view #coin b) ms
cs = Coin.equipartition (view #coin b) ms
ms = equipartitionTokenMapWithMaxQuantity (view #tokens b) maxQuantity

-- | Applies 'equipartitionTokenBundleWithMaxQuantity' to a list of bundles.
Expand Down
29 changes: 28 additions & 1 deletion lib/core/src/Cardano/Wallet/Primitive/Types/Coin.hs
Expand Up @@ -25,11 +25,14 @@ module Cardano.Wallet.Primitive.Types.Coin
, subtractCoin
, sumCoins
, distance
, equipartition

) where

import Prelude

import Cardano.Numeric.Util
( equipartitionNatural )
import Control.DeepSeq
( NFData (..) )
import Control.Monad
Expand All @@ -38,6 +41,8 @@ import Data.Foldable
( foldl' )
import Data.Hashable
( Hashable )
import Data.List.NonEmpty
( NonEmpty (..) )
import Data.Quantity
( Quantity (..) )
import Data.Text.Class
Expand Down Expand Up @@ -111,6 +116,9 @@ coinToInteger = fromIntegral . unCoin
coinToNatural :: Coin -> Natural
coinToNatural = fromIntegral . unCoin

unsafeNaturalToCoin :: Natural -> Coin
unsafeNaturalToCoin = Coin . fromIntegral

{-------------------------------------------------------------------------------
Checks
-------------------------------------------------------------------------------}
Expand Down Expand Up @@ -146,7 +154,26 @@ addCoin (Coin a) (Coin b) = Coin (a + b)
sumCoins :: Foldable t => t Coin -> Coin
sumCoins = foldl' addCoin (Coin 0)


-- | Absolute difference between two coin amounts. The result is never negative.
distance :: Coin -> Coin -> Coin
distance (Coin a) (Coin b) = if a < b then Coin (b - a) else Coin (a - b)

-- | Computes the equipartition of a coin into 'n' smaller coins.
--
-- An /equipartition/ of a coin is a /partition/ of that coin into 'n' smaller
-- coins whose values differ by no more than 1.
--
-- The resultant list is sorted in ascending order.
--
equipartition
:: Coin
-- ^ The coin to be partitioned.
-> NonEmpty a
-- ^ Represents the number of portions in which to partition the coin.
-> NonEmpty Coin
-- ^ The partitioned coins.
equipartition c =
-- Note: the natural-to-coin conversion is safe, as equipartitioning always
-- guarantees to produce values that are less than or equal to the original
-- value.
fmap unsafeNaturalToCoin . equipartitionNatural (coinToNatural c)

0 comments on commit c113cb5

Please sign in to comment.