-
Notifications
You must be signed in to change notification settings - Fork 139
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 key iteration function to dictionaries #1938
Comments
To clarify, did you mean a signature closer to fun forEachKey(_ f: (K): Bool): () ? |
@dreamsmasher ah, right, the parameter should be a function! Fixed the signature in the description |
Maybe we should open a FLIP to change the syntax for function types to something more clear, like |
for N keys:
This is a big problem actually, makes iterator a bit pointless. ( for large collections ) |
No, a user would not call The documentation should maybe describe this and provide an example to avoid misunderstandings. |
@turbolent: yeah I meant |
The difference is direct iteration vs creating an intermediate array then iterating over that. This reduces the amount of requests to the storage layer if you exit early. |
Yeah this: "if you are lucky", is not a so good idea in my opinion. Isn't it better to fail always vs fail rarely ? |
You could imagine a caller chunking the keys into smaller parts and returning them individually, vs returning all keys at once, which might be prohibitively expensive. For example, to skip the first few keys, then collect some, and finally ignore the rest: let dictionary: {UInt64: String} = // ...
let someKeys: [UInt64] = []
var i = 0
dictionary.forEachKey(fun (key: UInt64): Bool {
if i < 10 {
i = i + 1
return true
} else if i < 20 {
i = i + 1
someKeys.append(key)
return true
} else {
return false
}
}) |
What do you mean by this? I don't quite follow. |
if we have "keys" that will cause computation limit to exceed, then also if sum of all key iteration will be same computation cost, if I am searching for a key, it will fail in some cases and succeed in others ( depending on random key ordering ) |
This is super bad example though, considering ordering of the keys is random. But I mean it can be used for iteration yes. On a second thought, if you are running script very valuable, but on transactions it is a bit dead end. |
Issue To Be Solved
Dictionaries currently have a
let keys: [K]
field (whereK
is the type of the keys), which returns all keys of the dictionary.Dictionaries may stores a lot of elements, so the resulting keys array might be very expensive to produce.
Suggested Solution
Similar to the new storage iteration functions, add a new function to
Dictionary
which allows iterating over keys of the dictionary:The
Bool
return value of the iteration functionf
determines whether iteration continues;true
will proceed to the next key, whilefalse
will terminate iteration. The specific order in which the objects are iterated over is undefined, as is the behavior when a key is added or removed from the dictionary.The text was updated successfully, but these errors were encountered: