Skip to content

Commit a843b33

Browse files
committed
Add a few scott encoding instead of option functions to dict and try it out in assets
1 parent 36a1467 commit a843b33

File tree

2 files changed

+128
-38
lines changed

2 files changed

+128
-38
lines changed

lib/aiken/collection/dict.ak

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,24 @@ pub fn insert_with(
751751
}
752752
}
753753

754+
pub fn insert_with_alt(
755+
self: Dict<key, value>,
756+
key k: ByteArray,
757+
value v: value,
758+
with: fn(ByteArray, value, value) ->
759+
fn(fn(value) -> Pairs<ByteArray, value>, fn() -> Pairs<ByteArray, value>) ->
760+
Pairs<ByteArray, value>,
761+
) -> Dict<key, value> {
762+
Dict {
763+
inner: do_insert_with_alt(
764+
self.inner,
765+
k,
766+
v,
767+
fn(k, v1, v2) { with(k, v2, v1) },
768+
),
769+
}
770+
}
771+
754772
test insert_with_1() {
755773
let sum =
756774
fn(_k, a, b) { Some(a + b) }
@@ -764,6 +782,19 @@ test insert_with_1() {
764782
result == [Pair("bar", 2), Pair("foo", 1)]
765783
}
766784

785+
test insert_with_alt_1() {
786+
let sum =
787+
fn(_k, a, b) { fn(som, _non) { som(a + b) } }
788+
789+
let result =
790+
empty
791+
|> insert_with_alt(key: "foo", value: 1, with: sum)
792+
|> insert_with_alt(key: "bar", value: 2, with: sum)
793+
|> to_pairs()
794+
795+
result == [Pair("bar", 2), Pair("foo", 1)]
796+
}
797+
767798
test insert_with_2() {
768799
let sum =
769800
fn(_k, a, b) { Some(a + b) }
@@ -1016,6 +1047,16 @@ pub fn union_with(
10161047
Dict { inner: do_union_with(left.inner, right.inner, with) }
10171048
}
10181049

1050+
pub fn union_with_alt(
1051+
left: Dict<key, value>,
1052+
right: Dict<key, value>,
1053+
with: fn(ByteArray, value, value) ->
1054+
fn(fn(value) -> Pairs<ByteArray, value>, fn() -> Pairs<ByteArray, value>) ->
1055+
Pairs<ByteArray, value>,
1056+
) -> Dict<key, value> {
1057+
Dict { inner: do_union_with_alt(left.inner, right.inner, with) }
1058+
}
1059+
10191060
fn do_union_with(
10201061
left: Pairs<ByteArray, value>,
10211062
right: Pairs<ByteArray, value>,
@@ -1028,6 +1069,20 @@ fn do_union_with(
10281069
}
10291070
}
10301071

1072+
fn do_union_with_alt(
1073+
left: Pairs<ByteArray, value>,
1074+
right: Pairs<ByteArray, value>,
1075+
with: fn(ByteArray, value, value) ->
1076+
fn(fn(value) -> Pairs<ByteArray, value>, fn() -> Pairs<ByteArray, value>) ->
1077+
Pairs<ByteArray, value>,
1078+
) -> Pairs<ByteArray, value> {
1079+
when left is {
1080+
[] -> right
1081+
[Pair(k, v), ..rest] ->
1082+
do_union_with_alt(rest, do_insert_with_alt(right, k, v, with), with)
1083+
}
1084+
}
1085+
10311086
fn do_insert_with(
10321087
self: Pairs<ByteArray, value>,
10331088
key k: ByteArray,
@@ -1052,6 +1107,32 @@ fn do_insert_with(
10521107
}
10531108
}
10541109

1110+
fn do_insert_with_alt(
1111+
self: Pairs<ByteArray, value>,
1112+
key k: ByteArray,
1113+
value v: value,
1114+
with: fn(ByteArray, value, value) ->
1115+
fn(fn(value) -> Pairs<ByteArray, value>, fn() -> Pairs<ByteArray, value>) ->
1116+
Pairs<ByteArray, value>,
1117+
) -> Pairs<ByteArray, value> {
1118+
when self is {
1119+
[] -> [Pair(k, v)]
1120+
[Pair(k2, v2), ..rest] ->
1121+
if builtin.less_than_bytearray(k, k2) {
1122+
[Pair(k, v), ..self]
1123+
} else {
1124+
if k == k2 {
1125+
with(k, v, v2)(
1126+
fn(combined) { [Pair(k, combined), ..rest] },
1127+
fn() { rest },
1128+
)
1129+
} else {
1130+
[Pair(k2, v2), ..do_insert_with_alt(rest, k, v, with)]
1131+
}
1132+
}
1133+
}
1134+
}
1135+
10551136
test union_with_1() {
10561137
let left =
10571138
empty

lib/cardano/assets.ak

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub fn from_asset_list(xs: Pairs<PolicyId, Pairs<AssetName, Int>>) -> Value {
7777
expect Pair(p, [_, ..] as x) = inner
7878
x
7979
|> from_ascending_pairs_with(fn(v) { v != 0 })
80-
|> dict.insert_with(
80+
|> dict.insert_with_alt(
8181
acc,
8282
p,
8383
_,
@@ -556,29 +556,34 @@ pub fn add(
556556
} else {
557557
let helper =
558558
fn(_, left, _right) {
559-
let inner_result =
560-
dict.insert_with(
561-
left,
562-
asset_name,
563-
quantity,
564-
fn(_k, ql, qr) {
565-
let q = ql + qr
566-
if q == 0 {
567-
None
568-
} else {
569-
Some(q)
570-
}
571-
},
572-
)
573-
if dict.is_empty(inner_result) {
574-
None
575-
} else {
576-
Some(inner_result)
559+
fn(som, non) {
560+
let inner_result =
561+
dict.insert_with_alt(
562+
left,
563+
asset_name,
564+
quantity,
565+
fn(_k, ql, qr) {
566+
fn(som, non) {
567+
let q = ql + qr
568+
if q == 0 {
569+
non()
570+
} else {
571+
som(q)
572+
}
573+
}
574+
},
575+
)
576+
577+
if dict.is_empty(inner_result) {
578+
non()
579+
} else {
580+
som(inner_result)
581+
}
577582
}
578583
}
579584

580585
Value(
581-
dict.insert_with(
586+
dict.insert_with_alt(
582587
self.inner,
583588
policy_id,
584589
dict.from_ascending_pairs([Pair(asset_name, quantity)]),
@@ -630,27 +635,31 @@ test add_5() {
630635
/// Combine two `Value` together.
631636
pub fn merge(left v0: Value, right v1: Value) -> Value {
632637
Value(
633-
dict.union_with(
638+
dict.union_with_alt(
634639
v0.inner,
635640
v1.inner,
636641
fn(_, a0, a1) {
637-
let result =
638-
dict.union_with(
639-
a0,
640-
a1,
641-
fn(_, q0, q1) {
642-
let q = q0 + q1
643-
if q == 0 {
644-
None
645-
} else {
646-
Some(q)
647-
}
648-
},
649-
)
650-
if dict.is_empty(result) {
651-
None
652-
} else {
653-
Some(result)
642+
fn(som, non) {
643+
let result =
644+
dict.union_with_alt(
645+
a0,
646+
a1,
647+
fn(_, q0, q1) {
648+
fn(som, non) {
649+
let q = q0 + q1
650+
if q == 0 {
651+
non()
652+
} else {
653+
som(q)
654+
}
655+
}
656+
},
657+
)
658+
if dict.is_empty(result) {
659+
non()
660+
} else {
661+
som(result)
662+
}
654663
}
655664
},
656665
),

0 commit comments

Comments
 (0)