Copyright | © 2018-2021 IOHK 2025 GYELD GMBH |
---|---|
License | Apache-2.0 |
Safe Haskell | Safe-Inferred |
Language | GHC2021 |
GeniusYield.Transaction.CoinSelection.Numeric
Description
Modified by: GeniusYield
Originally from: Cardano.Numeric.Util
.
Since this module does not depend upon any deps outside hackage, we could have very well obtained it from remotely from cardano-wallet. However, as part of 3rd Atlas milestone, we have explicitly specified that we do not depend upon cardano-wallet. Once cardano-numeric
is on hackage or CHaP, this module can be removed.
Synopsis
- padCoalesce ∷ ∀ m a. (Monoid m, Ord m) ⇒ NonEmpty m → NonEmpty a → NonEmpty m
- equipartitionNatural ∷ HasCallStack ⇒ Natural → NonEmpty a → NonEmpty Natural
- partitionNatural ∷ Natural → NonEmpty Natural → Maybe (NonEmpty Natural)
- unsafePartitionNatural ∷ HasCallStack ⇒ Natural → NonEmpty Natural → NonEmpty Natural
- power ∷ Integral a ⇒ a → a → a
Coalescing values
Adjusts the source list so that its length is the same as the target list, either by padding the list, or by coalescing a subset of the elements, while preserving the total sum.
If the source list is shorter than the target list, this function repeatedly
inserts mempty
into the list until the desired length has been reached.
If the source list is longer than the target list, this function repeatedly
coalesces the smallest pair of elements with <>
until the desired length
has been reached.
The resulting list is guaranteed to be sorted into ascending order, and the sum of the elements is guaranteed to be the same as the sum of elements in the source list.
Examples (shown with ordinary list notation):
>>> padCoalesce [Sum 1] (replicate 4 ())
[Sum 0, Sum 0, Sum 0, Sum 1]
>>> padCoalesce [Sum (-1)] (replicate 4 ())
[Sum (-1), Sum 0, Sum 0, Sum 0]
>>> padCoalesce [Sum 8, Sum 4, Sum 2, Sum 1] (replicate 3 ())
@[Sum 3, Sum 4, Sum 8]
>>> padCoalesce [Sum 8, Sum 4, Sum 2, Sum 1] (replicate 2 ())
@[Sum 7, Sum 8]
>>> padCoalesce [Sum 8, Sum 4, Sum 2, Sum 1] (replicate 1 ())
[Sum 15]
Partitioning natural numbers
Arguments
∷ HasCallStack | |
⇒ Natural | The natural number to be partitioned. |
→ NonEmpty a | Represents the number of portions in which to partition the number. |
→ NonEmpty Natural | The partitioned numbers. |
Computes the equipartition of a natural number into n
smaller numbers.
An equipartition of a natural number n
is a partition of that number
into n
smaller numbers whose values differ by no more than 1.
The resultant list is sorted in ascending order.
Arguments
∷ Natural | Natural number to partition |
→ NonEmpty Natural | List of weights |
→ Maybe (NonEmpty Natural) |
Partitions a natural number into a number of parts, where the size of each part is proportional to the size of its corresponding element in the given list of weights, and the number of parts is equal to the number of weights.
Examples:
>>>
partitionNatural 9 (1 :| [1, 1])
Just (3 :| [3,3])
>>>
partitionNatural 10 (1 :| [])
Just (10 :| [])
>>>
partitionNatural 30 (1 :| [2, 4, 8])
Just (2 :| [4,8,16])
Pre-condition: there must be at least one non-zero weight.
If the pre-condition is not satisfied, this function returns Nothing
.
If the pre-condition is satisfied, this function guarantees that:
The length of the resulting list is identical to the length of the specified list:
fmap length (partitionNatural n weights) == Just (length weights)
The sum of elements in the resulting list is equal to the original natural number:
fmap sum (partitionNatural n weights) == Just n
- The size of each element in the resulting list is within unity of the ideal proportion.
Arguments
∷ HasCallStack | |
⇒ Natural | Natural number to partition |
→ NonEmpty Natural | List of weights |
→ NonEmpty Natural |
Partitions a natural number into a number of parts, where the size of each part is proportional to the size of its corresponding element in the given list of weights, and the number of parts is equal to the number of weights.
Throws a run-time error if the sum of weights is equal to zero.