Skip to content

Commit 40c2ae9

Browse files
committed
Make _with_alt versions the new default.
1 parent 51bc926 commit 40c2ae9

File tree

2 files changed

+24
-100
lines changed

2 files changed

+24
-100
lines changed

lib/aiken/collection/dict.ak

Lines changed: 19 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ pub fn from_pairs(self: Pairs<ByteArray, value>) -> Dict<key, value> {
161161
fn do_from_pairs(xs: Pairs<ByteArray, value>) -> Pairs<ByteArray, value> {
162162
when xs is {
163163
[] -> []
164-
[Pair(k, v), ..rest] -> do_insert(do_from_pairs(rest), k, v)
164+
[Pair(k, v), ..rest] ->
165+
do_insert_with(do_from_pairs(rest), k, v, union.keep_left())
165166
}
166167
}
167168

@@ -729,37 +730,25 @@ test insert_2() {
729730
/// to the merge function, and the new value is passed as the third argument.
730731
///
731732
/// ```aiken
732-
/// let sum =
733-
/// fn (_k, a, b) { Some(a + b) }
733+
/// use aiken/collection/dict/union
734734
///
735735
/// let result =
736736
/// dict.empty
737-
/// |> dict.insert_with(key: "a", value: 1, with: sum)
738-
/// |> dict.insert_with(key: "b", value: 2, with: sum)
739-
/// |> dict.insert_with(key: "a", value: 3, with: sum)
737+
/// |> dict.insert_with(key: "a", value: 1, with: union.sum)
738+
/// |> dict.insert_with(key: "b", value: 2, with: union.sum)
739+
/// |> dict.insert_with(key: "a", value: 3, with: union.sum)
740740
/// |> dict.to_pairs()
741741
///
742742
/// result == [Pair("a", 4), Pair("b", 2)]
743743
/// ```
744744
pub fn insert_with(
745-
self: Dict<key, value>,
746-
key k: ByteArray,
747-
value v: value,
748-
with: fn(ByteArray, value, value) -> Option<value>,
749-
) -> Dict<key, value> {
750-
Dict {
751-
inner: do_insert_with(self.inner, k, v, fn(k, v1, v2) { with(k, v2, v1) }),
752-
}
753-
}
754-
755-
pub fn insert_with_alt(
756745
self: Dict<key, value>,
757746
key k: ByteArray,
758747
value v: value,
759748
with: UnionStrategy<ByteArray, value>,
760749
) -> Dict<key, value> {
761750
Dict {
762-
inner: do_insert_with_alt(
751+
inner: do_insert_with(
763752
self.inner,
764753
k,
765754
v,
@@ -769,49 +758,33 @@ pub fn insert_with_alt(
769758
}
770759

771760
test insert_with_1() {
772-
let sum =
773-
fn(_k, a, b) { Some(a + b) }
774-
775-
let result =
776-
empty
777-
|> insert_with(key: "foo", value: 1, with: sum)
778-
|> insert_with(key: "bar", value: 2, with: sum)
779-
|> to_pairs()
780-
781-
result == [Pair("bar", 2), Pair("foo", 1)]
782-
}
783-
784-
test insert_with_alt_1() {
785761
let result =
786762
empty
787-
|> insert_with_alt(key: "foo", value: 1, with: union.sum())
788-
|> insert_with_alt(key: "bar", value: 2, with: union.sum())
763+
|> insert_with(key: "foo", value: 1, with: union.sum())
764+
|> insert_with(key: "bar", value: 2, with: union.sum())
789765
|> to_pairs()
790766

791767
result == [Pair("bar", 2), Pair("foo", 1)]
792768
}
793769

794770
test insert_with_2() {
795-
let sum =
796-
fn(_k, a, b) { Some(a + b) }
797-
798771
let result =
799772
empty
800-
|> insert_with(key: "foo", value: 1, with: sum)
801-
|> insert_with(key: "bar", value: 2, with: sum)
802-
|> insert_with(key: "foo", value: 3, with: sum)
773+
|> insert_with(key: "foo", value: 1, with: union.sum())
774+
|> insert_with(key: "bar", value: 2, with: union.sum())
775+
|> insert_with(key: "foo", value: 3, with: union.sum())
803776
|> to_pairs()
804777

805778
result == [Pair("bar", 2), Pair("foo", 4)]
806779
}
807780

808781
test insert_with_3() {
809782
let with =
810-
fn(k, a, _b) {
783+
fn(k, a, _b, keep, discard) {
811784
if k == "foo" {
812-
Some(a)
785+
keep(a)
813786
} else {
814-
None
787+
discard()
815788
}
816789
}
817790

@@ -1036,70 +1009,26 @@ test union_4() {
10361009
/// result == [Pair("a", 250), Pair("b", 200), Pair("c", 300)]
10371010
/// ```
10381011
pub fn union_with(
1039-
left: Dict<key, value>,
1040-
right: Dict<key, value>,
1041-
with: fn(ByteArray, value, value) -> Option<value>,
1042-
) -> Dict<key, value> {
1043-
Dict { inner: do_union_with(left.inner, right.inner, with) }
1044-
}
1045-
1046-
pub fn union_with_alt(
10471012
left: Dict<key, value>,
10481013
right: Dict<key, value>,
10491014
with: UnionStrategy<ByteArray, value>,
10501015
) -> Dict<key, value> {
1051-
Dict { inner: do_union_with_alt(left.inner, right.inner, with) }
1016+
Dict { inner: do_union_with(left.inner, right.inner, with) }
10521017
}
10531018

10541019
fn do_union_with(
1055-
left: Pairs<ByteArray, value>,
1056-
right: Pairs<ByteArray, value>,
1057-
with: fn(ByteArray, value, value) -> Option<value>,
1058-
) -> Pairs<ByteArray, value> {
1059-
when left is {
1060-
[] -> right
1061-
[Pair(k, v), ..rest] ->
1062-
do_union_with(rest, do_insert_with(right, k, v, with), with)
1063-
}
1064-
}
1065-
1066-
fn do_union_with_alt(
10671020
left: Pairs<ByteArray, value>,
10681021
right: Pairs<ByteArray, value>,
10691022
with: UnionStrategy<ByteArray, value>,
10701023
) -> Pairs<ByteArray, value> {
10711024
when left is {
10721025
[] -> right
10731026
[Pair(k, v), ..rest] ->
1074-
do_union_with_alt(rest, do_insert_with_alt(right, k, v, with), with)
1027+
do_union_with(rest, do_insert_with(right, k, v, with), with)
10751028
}
10761029
}
10771030

10781031
fn do_insert_with(
1079-
self: Pairs<ByteArray, value>,
1080-
key k: ByteArray,
1081-
value v: value,
1082-
with: fn(ByteArray, value, value) -> Option<value>,
1083-
) -> Pairs<ByteArray, value> {
1084-
when self is {
1085-
[] -> [Pair(k, v)]
1086-
[Pair(k2, v2), ..rest] ->
1087-
if builtin.less_than_bytearray(k, k2) {
1088-
[Pair(k, v), ..self]
1089-
} else {
1090-
if k == k2 {
1091-
when with(k, v, v2) is {
1092-
Some(combined) -> [Pair(k, combined), ..rest]
1093-
None -> rest
1094-
}
1095-
} else {
1096-
[Pair(k2, v2), ..do_insert_with(rest, k, v, with)]
1097-
}
1098-
}
1099-
}
1100-
}
1101-
1102-
fn do_insert_with_alt(
11031032
self: Pairs<ByteArray, value>,
11041033
key k: ByteArray,
11051034
value v: value,
@@ -1120,7 +1049,7 @@ fn do_insert_with_alt(
11201049
fn() { rest },
11211050
)
11221051
} else {
1123-
[Pair(k2, v2), ..do_insert_with_alt(rest, k, v, with)]
1052+
[Pair(k2, v2), ..do_insert_with(rest, k, v, with)]
11241053
}
11251054
}
11261055
}
@@ -1136,7 +1065,7 @@ test union_with_1() {
11361065
|> insert(bar, 42)
11371066
|> insert(foo, 1337)
11381067

1139-
let result = union_with(left, right, with: fn(_, l, r) { Some(l + r) })
1068+
let result = union_with(left, right, with: union.sum())
11401069

11411070
result == from_pairs([Pair(foo, 1351), Pair(bar, 42)])
11421071
}

lib/cardano/assets.ak

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub fn from_asset_list(xs: Pairs<PolicyId, Pairs<AssetName, Int>>) -> Value {
7878
expect Pair(p, [_, ..] as x) = inner
7979
x
8080
|> from_ascending_pairs_with(fn(v) { v != 0 })
81-
|> dict.insert_with_alt(acc, p, _, union.expect_no_duplicate())
81+
|> dict.insert_with(acc, p, _, union.expect_no_duplicate())
8282
},
8383
)
8484
|> Value
@@ -551,12 +551,7 @@ pub fn add(
551551
let helper =
552552
fn(_, left, _right, keep, discard) {
553553
let inner_result =
554-
dict.insert_with_alt(
555-
left,
556-
asset_name,
557-
quantity,
558-
union.sum_if_non_zero(),
559-
)
554+
dict.insert_with(left, asset_name, quantity, union.sum_if_non_zero())
560555

561556
if dict.is_empty(inner_result) {
562557
discard()
@@ -566,7 +561,7 @@ pub fn add(
566561
}
567562

568563
Value(
569-
dict.insert_with_alt(
564+
dict.insert_with(
570565
self.inner,
571566
policy_id,
572567
dict.from_ascending_pairs([Pair(asset_name, quantity)]),
@@ -618,11 +613,11 @@ test add_5() {
618613
/// Combine two `Value` together.
619614
pub fn merge(left v0: Value, right v1: Value) -> Value {
620615
Value(
621-
dict.union_with_alt(
616+
dict.union_with(
622617
v0.inner,
623618
v1.inner,
624619
fn(_, a0, a1, keep, discard) {
625-
let result = dict.union_with_alt(a0, a1, union.sum_if_non_zero())
620+
let result = dict.union_with(a0, a1, union.sum_if_non_zero())
626621
if dict.is_empty(result) {
627622
discard()
628623
} else {

0 commit comments

Comments
 (0)