Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aiken/collection/pairs.{merge} #93

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ariady-putra
Copy link

Add a merge function to aiken/collection/pairs, similar to assets.merge but for Pairs.

The function works with any key type, not just ByteArray. Unlike dict.from_pairs() |> dict.union_with() |> dict.to_pairs(), notice that the key is of type Data:

use aiken/builtin
use aiken/primitive/bytearray

let compare_un_b_data = fn(l, r) {
  bytearray.compare(l |> builtin.un_b_data, r |> builtin.un_b_data)
}

let result =
  []
    |> pairs.merge(key: "foo" |> builtin.b_data, value: 1, compare: compare_un_b_data, merging: builtin.add_integer)
    |> pairs.merge(key: "bar" |> builtin.b_data, value: 2, compare: compare_un_b_data, merging: builtin.add_integer)
    |> pairs.merge(key: "foo" |> builtin.b_data, value: 3, compare: compare_un_b_data, merging: builtin.add_integer)

result == [Pair("bar" |> builtin.b_data, 2), Pair("foo" |> builtin.b_data, 4)]

@ariady-putra
Copy link
Author

and a minor commit for documentation fix

/// transaction.placeholder.validity_range == interval.everything()

interval.everything is a constant

Comment on lines +611 to +612
Less ->
[Pair(k, v), ..self]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is unfortunately not possible in this context. Pairs do not make any assumption about the ordering of keys; It's fundamentally just a list. So we cannot short-circuit merging as soon as we meet a higher key; we have to traverse it in full; always.

///
/// Notice that the key is of type `Data`. The function works with any key type, not just ByteArray.
/// Unlike `dict.from_pairs() |> dict.union_with() |> dict.to_pairs()`
pub fn merge(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that since Pairs do not necessarily enforce uniqueness, this function becomes ambiguous:

  • does it apply the merge function for ALL matching key? Only the first?

This explains the choice of naming across this module like delete_first, delete_last, delete_all, to clearly indicates this behavior upfront. So in a similar spirit, I rather have a merge_first, merge_last and merge_all group.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @KtorZ , the "need" of this merge function came from here:
https://github.com/ariady-putra/tx_util/blob/1399949737ef1ea8c4d65b3cff7d601c25bcd725/lib/tx_util/builder/txn.ak#L484-L495
and even the merging logic itself took inspiration from repsert_by_ascending_key, but instead of replacing, we can provide our logics.

So the idea is to enable people to do something like:

let tx = transaction.placeholder
    |> add_withdrawal(cred1, v1)

...some other logics

let tx' = tx
    |> add_withdrawal(cred1, v1_additional)

What's your opinion on this? Or alternative? How about merge_by_ascending_key, since the logics are the same except for the replacing part?

PS. Idk if that makes sense to do that, but idk could be handy for certain scenarios

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or, add a replacing fn param at repsert_by_ascending_key?

pub fn repsert_by_ascending_key(
  self: Pairs<key, value>,
  key k: key,
  value v: value,
  compare: fn(key, key) -> Ordering,
  replace: fn(value, value) -> value
) -> Pairs<key, value> {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants