@@ -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
@@ -681,27 +682,7 @@ pub fn insert(
681
682
key k: ByteArray,
682
683
value v: value,
683
684
) -> Dict<key, value> {
684
- Dict { inner: do_insert(self.inner, k, v) }
685
- }
686
-
687
- fn do_insert(
688
- self: Pairs<ByteArray, value>,
689
- key k: ByteArray,
690
- value v: value,
691
- ) -> Pairs<ByteArray, value> {
692
- when self is {
693
- [] -> [Pair(k, v)]
694
- [Pair(k2, v2), ..rest] ->
695
- if builtin.less_than_bytearray(k, k2) {
696
- [Pair(k, v), ..self]
697
- } else {
698
- if k == k2 {
699
- [Pair(k, v), ..rest]
700
- } else {
701
- [Pair(k2, v2), ..do_insert(rest, k, v)]
702
- }
703
- }
704
- }
685
+ insert_with(self, k, v, union.keep_right())
705
686
}
706
687
707
688
test insert_1() {
@@ -729,37 +710,25 @@ test insert_2() {
729
710
/// to the merge function, and the new value is passed as the third argument.
730
711
///
731
712
/// ```aiken
732
- /// let sum =
733
- /// fn (_k, a, b) { Some(a + b) }
713
+ /// use aiken/collection/dict/union
734
714
///
735
715
/// let result =
736
716
/// 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)
717
+ /// |> dict.insert_with(key: "a", value: 1, with: union. sum)
718
+ /// |> dict.insert_with(key: "b", value: 2, with: union. sum)
719
+ /// |> dict.insert_with(key: "a", value: 3, with: union. sum)
740
720
/// |> dict.to_pairs()
741
721
///
742
722
/// result == [Pair("a", 4), Pair("b", 2)]
743
723
/// ```
744
724
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
725
self: Dict<key, value>,
757
726
key k: ByteArray,
758
727
value v: value,
759
728
with: UnionStrategy<ByteArray, value>,
760
729
) -> Dict<key, value> {
761
730
Dict {
762
- inner: do_insert_with_alt (
731
+ inner: do_insert_with (
763
732
self.inner,
764
733
k,
765
734
v,
@@ -769,49 +738,33 @@ pub fn insert_with_alt(
769
738
}
770
739
771
740
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
741
let result =
786
742
empty
787
- |> insert_with_alt (key: "foo", value: 1, with: union.sum())
788
- |> insert_with_alt (key: "bar", value: 2, with: union.sum())
743
+ |> insert_with (key: "foo", value: 1, with: union.sum())
744
+ |> insert_with (key: "bar", value: 2, with: union.sum())
789
745
|> to_pairs()
790
746
791
747
result == [Pair("bar", 2), Pair("foo", 1)]
792
748
}
793
749
794
750
test insert_with_2() {
795
- let sum =
796
- fn(_k, a, b) { Some(a + b) }
797
-
798
751
let result =
799
752
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)
753
+ |> insert_with(key: "foo", value: 1, with: union. sum() )
754
+ |> insert_with(key: "bar", value: 2, with: union. sum() )
755
+ |> insert_with(key: "foo", value: 3, with: union. sum() )
803
756
|> to_pairs()
804
757
805
758
result == [Pair("bar", 2), Pair("foo", 4)]
806
759
}
807
760
808
761
test insert_with_3() {
809
762
let with =
810
- fn(k, a, _b) {
763
+ fn(k, a, _b, keep, discard ) {
811
764
if k == "foo" {
812
- Some (a)
765
+ keep (a)
813
766
} else {
814
- None
767
+ discard()
815
768
}
816
769
}
817
770
@@ -970,17 +923,7 @@ pub fn union(
970
923
left: Dict<key, value>,
971
924
right: Dict<key, value>,
972
925
) -> Dict<key, value> {
973
- Dict { inner: do_union(left.inner, right.inner) }
974
- }
975
-
976
- fn do_union(
977
- left: Pairs<ByteArray, value>,
978
- right: Pairs<ByteArray, value>,
979
- ) -> Pairs<ByteArray, value> {
980
- when left is {
981
- [] -> right
982
- [Pair(k, v), ..rest] -> do_union(rest, do_insert(right, k, v))
983
- }
926
+ Dict(do_union_with(left.inner, right.inner, union.keep_left()))
984
927
}
985
928
986
929
test union_1() {
@@ -1036,70 +979,26 @@ test union_4() {
1036
979
/// result == [Pair("a", 250), Pair("b", 200), Pair("c", 300)]
1037
980
/// ```
1038
981
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
982
left: Dict<key, value>,
1048
983
right: Dict<key, value>,
1049
984
with: UnionStrategy<ByteArray, value>,
1050
985
) -> Dict<key, value> {
1051
- Dict { inner: do_union_with_alt (left.inner, right.inner, with) }
986
+ Dict { inner: do_union_with (left.inner, right.inner, with) }
1052
987
}
1053
988
1054
989
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
990
left: Pairs<ByteArray, value>,
1068
991
right: Pairs<ByteArray, value>,
1069
992
with: UnionStrategy<ByteArray, value>,
1070
993
) -> Pairs<ByteArray, value> {
1071
994
when left is {
1072
995
[] -> right
1073
996
[Pair(k, v), ..rest] ->
1074
- do_union_with_alt (rest, do_insert_with_alt (right, k, v, with), with)
997
+ do_union_with (rest, do_insert_with (right, k, v, with), with)
1075
998
}
1076
999
}
1077
1000
1078
1001
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
1002
self: Pairs<ByteArray, value>,
1104
1003
key k: ByteArray,
1105
1004
value v: value,
@@ -1120,7 +1019,7 @@ fn do_insert_with_alt(
1120
1019
fn() { rest },
1121
1020
)
1122
1021
} else {
1123
- [Pair(k2, v2), ..do_insert_with_alt (rest, k, v, with)]
1022
+ [Pair(k2, v2), ..do_insert_with (rest, k, v, with)]
1124
1023
}
1125
1024
}
1126
1025
}
@@ -1136,7 +1035,7 @@ test union_with_1() {
1136
1035
|> insert(bar, 42)
1137
1036
|> insert(foo, 1337)
1138
1037
1139
- let result = union_with(left, right, with: fn(_, l, r) { Some(l + r) } )
1038
+ let result = union_with(left, right, with: union.sum() )
1140
1039
1141
1040
result == from_pairs([Pair(foo, 1351), Pair(bar, 42)])
1142
1041
}
0 commit comments