Skip to content

Commit 25d9f1d

Browse files
committed
Add various util functions to the stdlib.
1 parent 48f0fa6 commit 25d9f1d

File tree

10 files changed

+750
-3
lines changed

10 files changed

+750
-3
lines changed

CHANGELOG.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
# Changelog
22

3-
## v2.0.0 - UNRELEASED
3+
## v2.1.0 - 2024-09-14
4+
5+
### Added
6+
7+
- [`aiken/collection/list.{for_each}`](https://aiken-lang.github.io/stdlib/aiken/collection/list.html#for_each): for performing many side-effects.
8+
- [`aiken/collection/dict.{pop}`](https://aiken-lang.github.io/stdlib/aiken/collection/dict.html#pop): for accessing and removing a value from a dictionnary in a single op.
9+
- [`aiken/primitive/bytearray.{starts_with}`](https://aiken-lang.github.io/stdlib/aiken/primitive/bytearray.html#starts_with): for matching bytearray prefixes.
10+
- [`aiken/primitive/math/rational.{pow}`](https://aiken-lang.github.io/stdlib/aiken/primitive/math/rational.html#pow): for computing (int) powers of rational numbers.
11+
- [`cardano/assets.{match}`](https://aiken-lang.github.io/stdlib/cardano/assets.html#match): efficiently compare two value-like.
12+
- [`cardano/assets.{restricted_to}`](https://aiken-lang.github.io/stdlib/cardano/assets.html#restricted_to): extracting value subsets from parent value.
13+
- [`cardano/address/credential.{compare}`](https://aiken-lang.github.io/stdlib/cardano/address/credential.html#compare): for ordering credentials.
14+
- [`cardano/governance/voter.{compare}`](https://aiken-lang.github.io/stdlib/cardano/governacen/voter.html#compare): for ordering voters.
15+
- [`cardano/transaction/output_reference.{compare}`](https://aiken-lang.github.io/stdlib/cardano/transaction/output_reference.html#compare): for ordering output references.
16+
- [`cardano/transaction/script_purpose.{compare}`](https://aiken-lang.github.io/stdlib/cardano/transaction/script_purpose.html#compare): for ordering script purpose.
17+
18+
### Changed
19+
20+
- N/A
21+
22+
### Removed
23+
24+
- N/A
25+
26+
## v2.0.0 - 2024-09-01
427

528
> [!NOTE]
629
> Significant performance improvements (mostly on CPU) across all boards mostly due to the integration of Plutus V3.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ aiken add aiken-lang/stdlib --version v2
1818

1919
aiken's version | stdlib's version(s)
2020
--- | ---
21-
`v1.1.0` | `>= 2.0.0`
21+
`v1.1.*` | `>= 2.0.0`
2222
`v1.0.29-alpha` | `>= 1.9.0` && `< 2.0.0`
2323
`v1.0.28-alpha` | `>= 1.9.0` && `< 2.0.0`
2424
`v1.0.26-alpha` | `<= 1.8.0` && `< 1.9.0`

aiken.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "aiken-lang/stdlib"
22
version = "main"
3-
compiler = "v1.1.0"
3+
compiler = "v1.1.2"
44
plutus = "v3"
55
description = "The Aiken Standard Library"
66

lib/aiken/collection/dict.ak

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,91 @@ test map_2() {
850850
get(result, foo) == Some(43) && size(result) == size(fixture_1)
851851
}
852852

853+
/// Remove a key-value pair from the dictionary and return its value. If the key is not found, no changes are made.
854+
///
855+
/// ```aiken
856+
/// let (value, _) =
857+
/// dict.empty
858+
/// |> dict.insert(key: "a", value: 100)
859+
/// |> dict.insert(key: "b", value: 200)
860+
/// |> dict.pop(key: "a")
861+
///
862+
/// result == 100
863+
/// ```
864+
pub fn pop(
865+
self: Dict<key, value>,
866+
key: ByteArray,
867+
) -> (Option<value>, Dict<key, value>) {
868+
do_pop(self.inner, key, fn(value, inner) { (value, Dict { inner }) })
869+
}
870+
871+
fn do_pop(
872+
self: Pairs<ByteArray, value>,
873+
key k: ByteArray,
874+
return: fn(Option<value>, Pairs<ByteArray, value>) -> result,
875+
) -> result {
876+
when self is {
877+
[] -> return(None, [])
878+
[Pair(k2, v2), ..rest] ->
879+
if builtin.less_than_equals_bytearray(k, k2) {
880+
if k == k2 {
881+
return(Some(v2), rest)
882+
} else {
883+
return(None, self)
884+
}
885+
} else {
886+
do_pop(
887+
rest,
888+
k,
889+
fn(value, inner) { return(value, [Pair(k2, v2), ..inner]) },
890+
)
891+
}
892+
}
893+
}
894+
895+
test pop_1() {
896+
pop(empty, foo) == (None, empty)
897+
}
898+
899+
test pop_2() {
900+
let m =
901+
empty
902+
|> insert(foo, 14)
903+
pop(m, foo) == (Some(14), empty)
904+
}
905+
906+
test pop_3() {
907+
let m =
908+
empty
909+
|> insert(foo, 14)
910+
pop(m, bar) == (None, m)
911+
}
912+
913+
test pop_4() {
914+
let m =
915+
empty
916+
|> insert(foo, 14)
917+
|> insert(bar, 14)
918+
pop(m, foo) == (Some(14), empty |> insert(bar, 14))
919+
}
920+
921+
test pop_6() {
922+
let m =
923+
empty
924+
|> insert("aaa", 1)
925+
|> insert("bbb", 2)
926+
|> insert("ccc", 3)
927+
|> insert("ddd", 4)
928+
|> insert("eee", 5)
929+
|> insert("fff", 6)
930+
|> insert("ggg", 7)
931+
|> insert("hhh", 8)
932+
|> insert("iii", 9)
933+
|> insert("jjj", 10)
934+
935+
pop(m, "bcd") == (None, m)
936+
}
937+
853938
// ## Combining
854939

855940
/// Combine two dictionaries. If the same key exist in both the left and

lib/aiken/collection/list.ak

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,25 @@ test flat_map_2() {
893893
flat_map([1, 2, 3], fn(a) { [a, a] }) == [1, 1, 2, 2, 3, 3]
894894
}
895895

896+
/// Perform an action for each element of a list.
897+
///
898+
/// ```aiken
899+
/// list.for_each(labels, do: fn(lbl) { trace lbl Void })
900+
/// ```
901+
fn for_each(self: List<a>, do: fn(a) -> Void) -> Void {
902+
foldr(self, Void, fn(x, _) { do(x) })
903+
}
904+
905+
test for_each_1() {
906+
for_each(
907+
[@"hello", @"world"],
908+
do: fn(lbl) {
909+
trace lbl
910+
Void
911+
},
912+
)
913+
}
914+
896915
/// List [`map`](#map) but provides the position (0-based) of the elements while iterating.
897916
///
898917
/// ```aiken

lib/cardano/address/credential.ak

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use aiken/primitive/bytearray
2+
use cardano/address.{Credential, Script, VerificationKey}
3+
4+
pub fn compare(left: Credential, right: Credential) -> Ordering {
5+
when left is {
6+
Script(left) ->
7+
when right is {
8+
Script(right) -> bytearray.compare(left, right)
9+
_ -> Less
10+
}
11+
VerificationKey(left) ->
12+
when right is {
13+
Script(_) -> Greater
14+
VerificationKey(right) -> bytearray.compare(left, right)
15+
}
16+
}
17+
}
18+
19+
test compare_matrix() {
20+
and {
21+
(compare(Script(""), Script("")) == Equal)?,
22+
(compare(VerificationKey(""), VerificationKey("")) == Equal)?,
23+
(compare(Script(""), VerificationKey("")) == Less)?,
24+
(compare(VerificationKey(""), Script("")) == Greater)?,
25+
(compare(Script("01"), Script("02")) == Less)?,
26+
(compare(Script("02"), Script("01")) == Greater)?,
27+
(compare(VerificationKey("01"), VerificationKey("02")) == Less)?,
28+
(compare(VerificationKey("02"), VerificationKey("01")) == Greater)?,
29+
}
30+
}

0 commit comments

Comments
 (0)