Skip to content

Add value.reduce into transaction/value.ak #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 15, 2024
Merged
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ jobs:
- name: 🧰 Install Aiken
uses: aiken-lang/setup-aiken@v0.1.0
with:
version: v1.0.18-alpha
version: v1.0.24-alpha

- name: 📝 Run fmt
run: aiken fmt --check
12 changes: 6 additions & 6 deletions lib/aiken/math/rational.ak
Original file line number Diff line number Diff line change
@@ -644,7 +644,7 @@ test compare_with_eq() {
expect Some(x) = new(2, 3)
expect Some(y) = new(3, 4)

!eq(x, y)? && !eq(y, x)? && eq(x, x)?
!eq(x, y)? && ( !eq(y, x)? && eq(x, x)? )
}

test compare_with_neq() {
@@ -654,7 +654,7 @@ test compare_with_neq() {
expect Some(x) = new(2, 3)
expect Some(y) = new(3, 4)

neq(x, y)? && neq(y, x)? && !neq(x, x)?
neq(x, y)? && ( neq(y, x)? && !neq(x, x)? )
}

test compare_with_gte() {
@@ -664,7 +664,7 @@ test compare_with_gte() {
expect Some(x) = new(2, 3)
expect Some(y) = new(3, 4)

!gte(x, y)? && gte(y, x)? && gte(x, x)?
!gte(x, y)? && ( gte(y, x)? && gte(x, x)? )
}

test compare_with_gt() {
@@ -674,7 +674,7 @@ test compare_with_gt() {
expect Some(x) = new(2, 3)
expect Some(y) = new(3, 4)

!gt(x, y)? && gt(y, x)? && !gt(x, x)?
!gt(x, y)? && ( gt(y, x)? && !gt(x, x)? )
}

test compare_with_lte() {
@@ -684,7 +684,7 @@ test compare_with_lte() {
expect Some(x) = new(2, 3)
expect Some(y) = new(3, 4)

lte(x, y)? && !lte(y, x)? && lte(x, x)?
lte(x, y)? && ( !lte(y, x)? && lte(x, x)? )
}

test compare_with_lt() {
@@ -694,7 +694,7 @@ test compare_with_lt() {
expect Some(x) = new(2, 3)
expect Some(y) = new(3, 4)

lt(x, y)? && !lt(y, x)? && !lt(x, x)?
lt(x, y)? && ( !lt(y, x)? && !lt(x, x)? )
}

/// Calculate the arithmetic mean between two `Rational` values.
53 changes: 53 additions & 0 deletions lib/aiken/transaction/value.ak
Original file line number Diff line number Diff line change
@@ -378,6 +378,59 @@ test flatten_with_2() {
) == [("a", "2"), ("b", "")]
}

/// Reduce a value into a single result
///
/// ```
/// value.zero()
/// |> value.add("a", "1", 10)
/// |> value.add("b", "2", 20)
/// |> value.reduce(v, 0, fn(_, _, quantity, acc) { acc + quantity })
/// // 30
/// ```
pub fn reduce(
self: Value,
start: acc,
with: fn(PolicyId, AssetName, Int, acc) -> acc,
) -> acc {
dict.foldr(
self.inner,
start,
fn(policy_id, asset_list, result) {
dict.foldr(asset_list, result, with(policy_id, _, _, _))
},
)
}

test reduce_1() {
let v =
zero()
|> add("a", "1", 10)
|> add("b", "2", 20)
let result = reduce(v, 0, fn(_, _, quantity, acc) { acc + quantity })
result == 30
}

test reduce_2() {
let v =
zero()
|> add("a", "1", 5)
|> add("a", "2", 15)
|> add("b", "", 10)
let result =
reduce(
v,
[],
fn(policy_id, asset_name, _, acc) { [(policy_id, asset_name), ..acc] },
)
result == [("a", "1"), ("a", "2"), ("b", "")]
}

test reduce_3() {
let v = zero()
let result = reduce(v, 1, fn(_, _, quantity, acc) { acc + quantity })
result == 1
}

/// Convert the value into a dictionary of dictionaries.
pub fn to_dict(self: Value) -> Dict<PolicyId, Dict<AssetName, Int>> {
self.inner