@@ -161,7 +161,8 @@ pub fn from_pairs(self: Pairs<ByteArray, value>) -> Dict<key, value> {
161
161
fn do_from_pairs(xs: Pairs<ByteArray, value>) -> Pairs<ByteArray, value> {
162
162
when xs is {
163
163
[] -> []
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())
165
166
}
166
167
}
167
168
@@ -729,37 +730,25 @@ test insert_2() {
729
730
/// to the merge function, and the new value is passed as the third argument.
730
731
///
731
732
/// ```aiken
732
- /// let sum =
733
- /// fn (_k, a, b) { Some(a + b) }
733
+ /// use aiken/collection/dict/union
734
734
///
735
735
/// let result =
736
736
/// 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)
740
740
/// |> dict.to_pairs()
741
741
///
742
742
/// result == [Pair("a", 4), Pair("b", 2)]
743
743
/// ```
744
744
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(
756
745
self: Dict<key, value>,
757
746
key k: ByteArray,
758
747
value v: value,
759
748
with: UnionStrategy<ByteArray, value>,
760
749
) -> Dict<key, value> {
761
750
Dict {
762
- inner: do_insert_with_alt (
751
+ inner: do_insert_with (
763
752
self.inner,
764
753
k,
765
754
v,
@@ -769,49 +758,33 @@ pub fn insert_with_alt(
769
758
}
770
759
771
760
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() {
785
761
let result =
786
762
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())
789
765
|> to_pairs()
790
766
791
767
result == [Pair("bar", 2), Pair("foo", 1)]
792
768
}
793
769
794
770
test insert_with_2() {
795
- let sum =
796
- fn(_k, a, b) { Some(a + b) }
797
-
798
771
let result =
799
772
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() )
803
776
|> to_pairs()
804
777
805
778
result == [Pair("bar", 2), Pair("foo", 4)]
806
779
}
807
780
808
781
test insert_with_3() {
809
782
let with =
810
- fn(k, a, _b) {
783
+ fn(k, a, _b, keep, discard ) {
811
784
if k == "foo" {
812
- Some (a)
785
+ keep (a)
813
786
} else {
814
- None
787
+ discard()
815
788
}
816
789
}
817
790
@@ -1036,70 +1009,26 @@ test union_4() {
1036
1009
/// result == [Pair("a", 250), Pair("b", 200), Pair("c", 300)]
1037
1010
/// ```
1038
1011
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(
1047
1012
left: Dict<key, value>,
1048
1013
right: Dict<key, value>,
1049
1014
with: UnionStrategy<ByteArray, value>,
1050
1015
) -> 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) }
1052
1017
}
1053
1018
1054
1019
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(
1067
1020
left: Pairs<ByteArray, value>,
1068
1021
right: Pairs<ByteArray, value>,
1069
1022
with: UnionStrategy<ByteArray, value>,
1070
1023
) -> Pairs<ByteArray, value> {
1071
1024
when left is {
1072
1025
[] -> right
1073
1026
[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)
1075
1028
}
1076
1029
}
1077
1030
1078
1031
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(
1103
1032
self: Pairs<ByteArray, value>,
1104
1033
key k: ByteArray,
1105
1034
value v: value,
@@ -1120,7 +1049,7 @@ fn do_insert_with_alt(
1120
1049
fn() { rest },
1121
1050
)
1122
1051
} else {
1123
- [Pair(k2, v2), ..do_insert_with_alt (rest, k, v, with)]
1052
+ [Pair(k2, v2), ..do_insert_with (rest, k, v, with)]
1124
1053
}
1125
1054
}
1126
1055
}
@@ -1136,7 +1065,7 @@ test union_with_1() {
1136
1065
|> insert(bar, 42)
1137
1066
|> insert(foo, 1337)
1138
1067
1139
- let result = union_with(left, right, with: fn(_, l, r) { Some(l + r) } )
1068
+ let result = union_with(left, right, with: union.sum() )
1140
1069
1141
1070
result == from_pairs([Pair(foo, 1351), Pair(bar, 42)])
1142
1071
}
0 commit comments