Skip to content

Commit e5fde44

Browse files
authored
Merge pull request #84 from aiken-lang/micah/value_reduce
Add value.reduce into transaction/value.ak
2 parents b57389f + 28e0ead commit e5fde44

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
- name: 🧰 Install Aiken
3434
uses: aiken-lang/[email protected]
3535
with:
36-
version: v1.0.18-alpha
36+
version: v1.0.24-alpha
3737

3838
- name: 📝 Run fmt
3939
run: aiken fmt --check

lib/aiken/math/rational.ak

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ test compare_with_eq() {
644644
expect Some(x) = new(2, 3)
645645
expect Some(y) = new(3, 4)
646646

647-
!eq(x, y)? && !eq(y, x)? && eq(x, x)?
647+
!eq(x, y)? && ( !eq(y, x)? && eq(x, x)? )
648648
}
649649

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

657-
neq(x, y)? && neq(y, x)? && !neq(x, x)?
657+
neq(x, y)? && ( neq(y, x)? && !neq(x, x)? )
658658
}
659659

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

667-
!gte(x, y)? && gte(y, x)? && gte(x, x)?
667+
!gte(x, y)? && ( gte(y, x)? && gte(x, x)? )
668668
}
669669

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

677-
!gt(x, y)? && gt(y, x)? && !gt(x, x)?
677+
!gt(x, y)? && ( gt(y, x)? && !gt(x, x)? )
678678
}
679679

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

687-
lte(x, y)? && !lte(y, x)? && lte(x, x)?
687+
lte(x, y)? && ( !lte(y, x)? && lte(x, x)? )
688688
}
689689

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

697-
lt(x, y)? && !lt(y, x)? && !lt(x, x)?
697+
lt(x, y)? && ( !lt(y, x)? && !lt(x, x)? )
698698
}
699699

700700
/// Calculate the arithmetic mean between two `Rational` values.

lib/aiken/transaction/value.ak

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,59 @@ test flatten_with_2() {
378378
) == [("a", "2"), ("b", "")]
379379
}
380380

381+
/// Reduce a value into a single result
382+
///
383+
/// ```
384+
/// value.zero()
385+
/// |> value.add("a", "1", 10)
386+
/// |> value.add("b", "2", 20)
387+
/// |> value.reduce(v, 0, fn(_, _, quantity, acc) { acc + quantity })
388+
/// // 30
389+
/// ```
390+
pub fn reduce(
391+
self: Value,
392+
start: acc,
393+
with: fn(PolicyId, AssetName, Int, acc) -> acc,
394+
) -> acc {
395+
dict.foldr(
396+
self.inner,
397+
start,
398+
fn(policy_id, asset_list, result) {
399+
dict.foldr(asset_list, result, with(policy_id, _, _, _))
400+
},
401+
)
402+
}
403+
404+
test reduce_1() {
405+
let v =
406+
zero()
407+
|> add("a", "1", 10)
408+
|> add("b", "2", 20)
409+
let result = reduce(v, 0, fn(_, _, quantity, acc) { acc + quantity })
410+
result == 30
411+
}
412+
413+
test reduce_2() {
414+
let v =
415+
zero()
416+
|> add("a", "1", 5)
417+
|> add("a", "2", 15)
418+
|> add("b", "", 10)
419+
let result =
420+
reduce(
421+
v,
422+
[],
423+
fn(policy_id, asset_name, _, acc) { [(policy_id, asset_name), ..acc] },
424+
)
425+
result == [("a", "1"), ("a", "2"), ("b", "")]
426+
}
427+
428+
test reduce_3() {
429+
let v = zero()
430+
let result = reduce(v, 1, fn(_, _, quantity, acc) { acc + quantity })
431+
result == 1
432+
}
433+
381434
/// Convert the value into a dictionary of dictionaries.
382435
pub fn to_dict(self: Value) -> Dict<PolicyId, Dict<AssetName, Int>> {
383436
self.inner

0 commit comments

Comments
 (0)