Skip to content

Commit add8d0a

Browse files
committed
improve module comments around dict and assets strategies.
1 parent 2247de4 commit add8d0a

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
### Added
2626

2727
- [`aiken/cbor.{deserialise}`](https://aiken-lang.github.io/stdlib/aiken/cbor.html#deserialise): to recover `Data` from CBOR bytes.
28+
2829
- [`aiken/collection/pairs.{insert_with_by_ascending_key}`](https://aiken-lang.github.io/stdlib/aiken/collection/pairs.html#insert_with_by_ascending_key): for inserting in pairs while specifying how to combine values on key conflict.
2930

3031
## v2.1.0 - 2024-09-14

lib/aiken/collection/dict/strategy.ak

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//// This module contains strategies used to transform [`Dict`](../dict.html#Dict). You can safely ignore this module if you aren't using [`union_with`](../dict.html#union_with) nor [`insert_with`](../dict.html#insert_with).
2+
13
/// A strategy for combining two values in a dictionnary that belong to the same key.
24
pub type UnionStrategy<key, value> =
35
fn(key, value, value, KeepValue<key, value>, DiscardValue<key, value>) ->

lib/cardano/assets.ak

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,9 +790,11 @@ pub fn flatten(self: Value) -> List<(PolicyId, AssetName, Int)> {
790790
)
791791
}
792792

793-
/// Flatten a `Value` as a list of results, possibly discarding some along the way.
793+
/// Flatten a `Value` as a list of results, possibly discarding some along the way. In particular, we have:
794794
///
795-
/// When the transform function returns `None`, the result is discarded altogether.
795+
/// ```aiken
796+
/// flatten(value) === flatten_with(value, strategy.triple())
797+
/// ```
796798
pub fn flatten_with(self: Value, with: FlattenStrategy<result>) -> List<result> {
797799
dict.foldr(
798800
self.inner,

lib/cardano/assets/strategy.ak

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//// This module contains strategies used to transform [`Value`](../assets.html#Value). You can safely ignore this module if you aren't using [`flatten_with`](../assets.html#flatten_with).
2+
13
use aiken/crypto.{Blake2b_224, Hash, Script}
24

35
/// A strategy for flattening an asset list.

lib/cardano/transaction.ak

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ pub type Redeemer =
128128
/// validator {
129129
/// spend(datum, redeemer, my_output_reference, self) {
130130
/// expect Some(input) =
131-
/// self.inputs
132-
/// |> transaction.find_input(my_output_reference)
131+
/// self.inputs |> transaction.find_input(my_output_reference)
133132
/// }
134133
/// }
135134
/// ```
@@ -141,6 +140,29 @@ pub fn find_input(
141140
|> list.find(fn(input) { input.output_reference == output_reference })
142141
}
143142

143+
/// Find the output corresponding to an output reference in a list of inputs.
144+
/// **Fails** when no matching output is found.
145+
///
146+
/// ```aiken
147+
/// validator {
148+
/// spend(datum, redeemer, my_output_reference, self) {
149+
/// let output =
150+
/// self.inputs |> transaction.resolve_input(my_output_reference)
151+
/// }
152+
/// }
153+
/// ```
154+
pub fn resolve_input(inputs: List<Input>, output_reference: OutputReference) -> Output {
155+
when inputs is {
156+
[] -> fail
157+
[head, ..tail] ->
158+
if head.output_reference == output_reference {
159+
head.output
160+
} else {
161+
resolve_input(tail, output_reference)
162+
}
163+
}
164+
}
165+
144166
/// Find a [`Datum`](#Datum) by its hash, if present. The function looks first for
145167
/// datums in the witness set, and then for inline datums if it doesn't find any in
146168
/// witnesses.

0 commit comments

Comments
 (0)